Skip to content

unretained-async-task (EFF107)

Family: Execution Effects (EFF) · Heuristic · Opt-in · Severity error (a bug) · No automatic fix

Opt-in: an asyncio.create_task result bound to a local variable but never awaited, returned, or stored.

Heuristic

This rule is heuristic: a premise of its claim can be open ("What a report states" lists them), so a report can be wrong about the code. It runs only when select or extend-select names its code or name. It has not been measured yet. See Rule Precision.

What it does

Checks for the result of asyncio.create_task(...), asyncio.ensure_future(...), or loop.create_task(...) that is bound to a local variable but never kept: never awaited, returned, yielded, stored (in an attribute, a container, a global, or an enclosing scope), passed to a call (tasks.add(task), asyncio.gather(task)), captured by a closure, cancelled, or asked for its result.

The factories are known by what their names are bound to: an import alias (from asyncio import create_task as spawn) counts, a project function named create_task does not. loop.create_task needs a receiver known to be an event loop (asyncio.get_running_loop(), get_event_loop(), new_event_loop(), or an annotated asyncio.AbstractEventLoop).

It does not report:

  • a call whose result is discarded (asyncio.create_task(work()) as a statement): that is Ruff's RUF006;
  • asyncio.TaskGroup.create_task, which keeps its tasks;
  • receivers of unknown type;
  • an argument known not to be a coroutine, from which no new task is made: the result of a project function that is a plain def or a generator function without decorators other than staticmethod, classmethod, abstractmethod, override, or final (create_task raises TypeError on what it returns, and ensure_future returns a future it is handed unchanged), or a value typed as a future or a task (loop.run_in_executor(...), loop.create_future(), asyncio.gather(...), asyncio.wrap_future(...), executor.submit(...), or an annotated asyncio.Future). An argument of unknown kind (a parameter, an awaitable, a constructed object, the result of a decorated function) may be a coroutine and is reported. The argument is the first positional one or the one passed as coro= / coro_or_future=.

A method of the task that is passed to a call, stored, or returned keeps the task too, because the bound method holds it: self.addCleanup(task.cancel), loop.call_later(1, task.cancel), self.stop = task.cancel. Being only the receiver of add_done_callback(...) or set_name(...) does not keep the task: the callback does not hold it strongly.

What a report states

A report states that the task an asyncio.create_task (or ensure_future, or loop.create_task) call returns is bound to a local variable that nothing keeps.

Its premises:

  • Task factory: the call is one of the factories, known by what its name is bound to. Established.
  • Never kept: no use of the variable awaits, returns, yields, stores, passes, captures, cancels, or asks the task for its result. Established from the uses in the function.

Not part of the claim: whether the task is garbage collected before it ends.

Why is this bad?

The event loop keeps only a weak reference to a task. Once the function that created it returns, nothing references the task any more, and it can be garbage-collected before it finishes: the work silently stops halfway.

Example

import asyncio


async def handle(event: dict[str, str]) -> None:
    task = asyncio.create_task(notify(event))  # EFF107
    task.add_done_callback(log_failure)

Use instead:

import asyncio

background_tasks: set[asyncio.Task[None]] = set()


async def handle(event: dict[str, str]) -> None:
    task = asyncio.create_task(notify(event))
    background_tasks.add(task)
    task.add_done_callback(background_tasks.discard)

Fix safety

No automatic fix.

Options

  • lint.extend-select = ["EFF107"] (or --extend-select EFF107) enables the rule. It is opt-in: selecting the EFF prefix does not enable it.