Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force-quit a CLOSE_WAIT socket in Python?

I'm using a pile of sockets in my Python script, perhaps 20/s. The are ephemeral, done in three seconds. I've tried to do a good job of sending socket.close() to everything, but for some reason CLOSE_WAIT statuses on sockets still keep piling up.

How can I force-quit these CLOSE_WAIT sockets? Or can I set a lower timeout on them? They end up piling up to the point of crash.

This is my socket code:

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.settimeout(2) # 2 seconds timeout

        l_onoff = 1
        l_linger = 0
        s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', l_onoff, l_linger)) 

        self.s = s          
        streamloop.add_callback(self.connect, s)

This is my close:

def full_close(self):
    try:
        self.stream.close()
        self.s.close()
    except Exception:
        print 'full_close err: ', sys.exc_info()[1:3]
        pass    
like image 988
knutole Avatar asked Aug 17 '26 20:08

knutole


1 Answers

Sounds like you are leaking sockets somewhere.

See https://stackoverflow.com/a/11240739/412080 for more.

like image 136
Maxim Egorushkin Avatar answered Aug 19 '26 11:08

Maxim Egorushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!