Skip to content Skip to sidebar Skip to footer

Python Asyncio: Unreferenced Tasks Are Destroyed By Garbage Collector?

I am writing a program that accepts RPC requests over AMQP for executing network requests (CoAP). When processing RPC requests, the aioamqp callback generates tasks that are respon

Solution 1:

When a task is scheduled, it's _step callback is scheduled in the loop. That callback maintains a reference to the task through self. I have not checked the code, but I have high confidence that the loop maintains a reference to its callbacks. However, when a task awaits some awaitable or future, the _step callback is not scheduled. In that case, the task adds a done callback that retains a reference to the task, but the loop does not retain references to tasks waiting for futures.

So long as something retains a reference to the future that the task is waiting on, all is well. However, if nothing retains a hard reference to the future, then the future can get garbage collected, and when that happens the task can get garbage collected.

So, I'd look for things that your task calls where the future the task is waiting on might not be referenced. In general the future needs to be referenced so someone can set its result eventually, so it is very likely a bug if you have unreferenced futures.

Post a Comment for "Python Asyncio: Unreferenced Tasks Are Destroyed By Garbage Collector?"