socket handle leak in pyzmq?
Asked Answered
B

1

7

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!

Becquerel answered 19/11, 2013 at 20:35 Comment(0)
B
5

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.

Becquerel answered 19/11, 2013 at 23:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.