The task asyncio.create_task returns is not kept anywhere

tx0/python:task-reference-droppedrecommendedwarningbug

What it finds

asyncio.create_task(worker()) on a line of its own: the event loop keeps only a weak reference to its tasks, so a task that nothing else refers to can be collected before it finishes, and its exceptions are never seen. The standard library's documentation asks callers to keep a reference, for example in a set that the task removes itself from when it is done.

Tasks started through a TaskGroup, which keeps them, are not reported.

From the rule's source, task_reference_dropped.rs.

Example from the rule's tests
import asyncio  async def main(worker, group):    asyncio.create_task(worker())tx0 reports: the task from asyncio.create_task is not kept    asyncio.ensure_future(worker())tx0 reports: the task from asyncio.ensure_future is not kept    task = asyncio.create_task(worker())    group.create_task(worker())    await task
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:task-reference-dropped" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:task-reference-dropped] reason="..."

tx0 explain tx0/python:task-reference-dropped shows the same in the terminal. Found a false positive? Report it.