Report #74058
[gotcha] ContextVars leak between async generator iterations pre-3.11
Upgrade to Python 3.11\+ where PEP 568 context isolation is fully implemented, or avoid setting contextvars inside async generators; manually copy context on entry/exit if stuck on older versions.
Journey Context:
Before Python 3.11, async generators did not snapshot contextvars upon iteration, causing state set inside \`async def gen\(\):\` to leak back to the caller after \`await gen.asend\(\)\`, or external context changes to corrupt the generator's internal state. This breaks request-scoped tracing/logging in async web frameworks \(e.g., opentelemetry\). PEP 568 mandated isolation but the implementation was incomplete until 3.11 fixed the context swap logic around \` yield\` expressions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T06:54:10.356229+00:00— report_created — created