zeromq / pyzmq

PyZMQ: Python bindings for zeromq
http://zguide.zeromq.org/py:all
BSD 3-Clause "New" or "Revised" License
3.66k stars 638 forks source link

BUG: asyncio: memory leak in socket.poll #1888

Closed detlef-beckmann closed 1 year ago

detlef-beckmann commented 1 year ago

What pyzmq version?

25.1.0

What libzmq version?

4.3.4

Python version (and how it was installed)

3.11.4

OS

openSUSE 15.4

What happened?

Futures returned by async socket.poll are not removed from socket._recv_futures if the future is cancelled before the event arrives. This is similar to #968, just in socket rather than poller. The sample code counts up to 10.

Code to reproduce bug

import zmq
import zmq.asyncio
import asyncio

context = zmq.asyncio.Context()

async def leak():
    socket = context.socket(zmq.REP)
    socket.bind('inproc://#1')
    for _ in range(10):
        future = socket.poll(flags=zmq.POLLIN)
        future.cancel()
        await asyncio.sleep(0)  # allow callbacks to run
        print(len(socket._recv_futures))

asyncio.run(leak())

Traceback, if applicable

No response

More info

No response

minrk commented 1 year ago

Thanks! Same fix should apply as well. #1889