altdesktop / i3ipc-python

🐍 An improved Python library to control i3wm and sway.
http://i3ipc-python.readthedocs.io
BSD 3-Clause "New" or "Revised" License
851 stars 109 forks source link

aio: keep references on tasks created by ensure_future #184

Closed vincentbernat closed 3 years ago

vincentbernat commented 3 years ago

This is done by adding them to a set and removing them when the task is done. asyncio itself only keeps a weak references to all tasks. A task can therefore be destroyed when it is not referenced anymore. When this happens, we get things like that:

task: <Task pending name='Task-326' coro=<_AIOPubSub.queue_handler.<locals>.handler_coroutine() done, defined at /usr/lib/python3/dist-packages/i3ipc/aio/connection.py:30> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f9f08a13be0>()]>>

This seldomly happens and I still have a hard time understanding the exact cause.

To workaround that, we provide a tiny wrapper around ensure_future() keeping a reference to the task and removing it once it is done. In connection.py, all ensure_future() produces a task.

acrisci commented 3 years ago

Oh ok that's weird that you have to do that.