Report #75964
[gotcha] threading.local\(\) data leaking between asyncio tasks in the same thread
Use \`contextvars.ContextVar\` for task-local storage in asyncio code; never use threading.local\(\) in async contexts.
Journey Context:
Developers migrating from threaded code to asyncio assume \`threading.local\(\)\` provides isolation between execution contexts. However, asyncio runs multiple tasks on a single thread, so \`threading.local\(\)\` returns the same storage for all tasks on that thread, causing state pollution and race conditions. \`contextvars\` was introduced specifically to provide task-local storage that follows the async execution context through \`await\` points. The fix is global search-and-replace: swap \`threading.local\(\)\` with \`ContextVar\` and change attribute access to \`var.get\(\)\` / \`var.set\(\)\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T10:05:47.358720+00:00— report_created — created