Asyncio tasks getting cancelled

Ian Kelly ian.g.kelly at gmail.com
Tue Nov 6 17:39:24 EST 2018


n Mon, Nov 5, 2018 at 8:43 PM <ike at koeln.ccc.de> wrote:
>
> On Mon, Nov 05, 2018 at 07:15:04PM -0700, Ian Kelly wrote:
> > > For context:
> > > https://github.com/ldo/dbussy/issues/13
> > > https://gist.github.com/tu500/3232fe03bd1d85b1529c558f920b8e43
> > >
> > > It really feels like asyncio is loosing strong references to scheduled
> > > tasks, as excplicitly keeping them around helps. Also, the error
> > > messages I'm getting are the ones from here:
> > > https://github.com/python/cpython/blob/16c8a53490a22bd4fcde2efaf4694dd06ded882b/Lib/asyncio/tasks.py#L145
> > > Which indicates that the tasks actually weren't even started at all?
> >
> > No, it indicates that it was cleaned up (likely because the program
> > exited) before the task completed. Which likely implies that the loop
> > exited without waiting for it. From the stack trace, you're using
> > loop.run_until_complete(main()). Like asyncio.run, that only runs
> > until the specific thing you pass it completes, which might wait on
> > other things or might not. Anything else that's still pending is going
> > to be left up in the air unless you subsequently restart the same
> > event loop.
>
> What I meant was, the error message is specific to futures in the
> 'PENDING' state. Which should be set to 'RUNNING' before any actions
> occur. So it appears the tasks weren't started at all.

Ah. I don't think asyncio uses a RUNNING state. There's nothing about
it in the docs; tasks are either done or they're not:
https://docs.python.org/3/library/asyncio-task.html#task-object

And inspecting the current task also shows PENDING:

py> from asyncio import *
py> async def main():
...   print(current_task()._state)
...
py> run(main())
PENDING

> Also the cleanup happens instantly, ie before the loop exits (as the
> main loop has an asyncio.sleep timeout. Printing a message also clearly
> shows this happens way before main returns.

Okay, that wasn't clear from the log you posted. It's not obvious to
me why that would be happening. although it does sound like the tasks
are getting created and then immediately garbage-collected for some
reason. It could be a bug in asyncio, or it could be a bug in the way
the tasks are created, e.g. on a separate event loop that gets dropped
on the floor.



More information about the Python-list mailing list