Skip to content
Sarj Code Standards

async-executor-shutdown

Run waiting executor shutdown outside the async event-loop thread.

Why

Executor.shutdown waits for workers by default; invoking it directly in an async function can block the event loop and delay unrelated service work or cancellation.

Fix

Await asyncio.to_thread(executor.shutdown, wait=True), or use the application's explicit nonblocking shutdown policy after arranging worker ownership and completion.

Examples

Before — flagged Waiting shutdown runs on the event-loop thread
app/lifecycle.py
from concurrent.futures import ThreadPoolExecutor
async def close():
executor = ThreadPoolExecutor()
executor.shutdown(wait=True)
After — preferred Await waiting shutdown outside the event-loop thread
app/lifecycle.py
import asyncio
from concurrent.futures import ThreadPoolExecutor
async def close():
executor = ThreadPoolExecutor()
await asyncio.to_thread(executor.shutdown, wait=True)