Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket handle leak in pyzmq?

Hi good people of StackOverflow.

I'm using pyzmq and I've got some long-running processes, which led to a discovery that socket handles are being left open. I've narrowed the offending code down to the following:

import zmq

uri = 'tcp://127.0.0.1'
sock_type = zmq.REQ
linger = 250

# Observe output of lsof -p <pid> here and see no socket handles

ctx = zmq.Context.instance()
sock = ctx.socket(sock_type)
sock.setsockopt(zmq.LINGER, linger)
port = sock.bind_to_random_port(uri)

# Observe output of lsof -p <pid> here and see many socket handles

sock.close()  # lsof -p <pid> still showing many socket handles
ctx.destroy()  # Makes no difference

pyzmq version is pyzmq-13.1.0

Either there is a bug in pyzmq, or I'm doing something incorrectly. I hope you can help me!!

Thanks!

like image 820
Cari Avatar asked Nov 19 '13 20:11

Cari


1 Answers

After a chat with pieterh and minrk on #zeromq, we found the cause.

ctx.destroy() in 13.1.0 has an indentation bug so it only calls Context.term() if there is an unclosed socket.

Workaround: call ctx.term() instead, and make sure all of your sockets are closed before you do.

like image 102
Cari Avatar answered Oct 11 '22 22:10

Cari