Skip to content

async-cleanup-registered-synchronously

Do not register a proven async cleanup function with ExitStack.callback.

Why

The synchronous callback API discards the returned coroutine without awaiting it, so the cleanup body never runs.

Fix

Use AsyncExitStack and register the coroutine function with push_async_callback.

Examples

Before — flagged Synchronous callbacks do not await cleanup
app/resources.py
from contextlib import AsyncExitStack
async def close():
await release()
async def run():
async with AsyncExitStack() as stack:
stack.callback(close)
After — preferred Register cleanup with the async API
app/resources.py
from contextlib import AsyncExitStack
async def close():
await release()
async def run():
async with AsyncExitStack() as stack:
stack.push_async_callback(close)