python-trio / flake8-async

Highly opinionated linter for Trio code
https://flake8-async.readthedocs.io
MIT License
17 stars 2 forks source link

Checks for `asyncio.create_task()` #207

Closed Zac-HD closed 1 month ago

Zac-HD commented 4 months ago

via https://github.com/Zac-HD/flake8-async-archive/issues/5: the docs for asyncio.create_task say:

Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done.

It's really easy to miss this and try to fire-and-forget. It would be great if flake8-async detected this.


I also wonder whether we should have an optional check which recommends using TaskGroup instead of asyncio.create_task, loop.run_until_complete, etc, but on balance think it's better to have documentation explaining why you should use anyio.TaskGroup instead (e.g. better cancellation semantics as part of structured concurrency).