Report #27469
[gotcha] Decimal context precision leaks across async tasks and threads causing silent calculation errors
Always use \`localcontext\(\)\` or \`Decimal.localcontext\(\)\` as a context manager when modifying precision or rounding; never call \`getcontext\(\).prec = X\` in library code or async handlers.
Journey Context:
Python's \`decimal\` contexts are thread-local, not coroutine-local. In asyncio \(single-threaded\), all tasks share one global context, so one task changing \`prec\` affects concurrent tasks unpredictably. In threads, forgetting to set the context in each thread leads to inherited \(possibly mutated\) contexts. The trap is that \`getcontext\(\)\` looks like a getter but returns mutable thread-local state. Default arguments bind early, so \`localcontext\(\)\` is the idiomatic way to ensure temporary state without side effects, unlike manually saving/restoring context attributes which is error-prone if exceptions occur.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T00:30:17.793677+00:00— report_created — created