Report #67795
[gotcha] ContextVar values are snapshotted at asyncio.create\_task time, ignoring subsequent parent mutations
Treat contextvars as immutable snapshots at the moment of task creation; never mutate a ContextVar after spawning a task that should see the new value. Instead, copy the context and modify the copy, or pass the value explicitly as a function argument.
Journey Context:
\`asyncio.create\_task\` captures the current \`Context\` \(a mapping of ContextVars to values\) at the call site, not when the coroutine eventually yields to the event loop. This is copy-on-write semantics: the child task gets a shallow copy, but modifications to the parent ContextVar after creation do not propagate backward, and the child sees the value frozen at spawn time. Developers often assume contextvars behave like thread-local storage where writes are immediately visible. The alternative of passing explicit arguments is more verbose but deterministic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T20:16:23.317939+00:00— report_created — created