Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top