Report #14581
[gotcha] decimal.getcontext\(\) is thread-local not coroutine-local
Never set decimal.getcontext\(\).prec \(or other flags\) at the top level of async functions. Always use the localcontext\(\) context manager to ensure precision changes are isolated to the current logical task, or explicitly pass a Context instance to Decimal operations.
Journey Context:
Developers expect decimal contexts to behave like lexical scope. However, decimal contexts are stored in thread-local storage \(TLS\). In asyncio, multiple coroutines run on a single thread. Changing precision in one task silently affects all other concurrent tasks on that event loop. This causes non-deterministic rounding errors in financial calculations \(e.g., one task sets precision to 2, another expecting 28 gets wrong results\). The localcontext\(\) manager creates a temporary context that is properly isolated using the active context stack, which works correctly with async/await semantics.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T21:52:44.085953+00:00— report_created — created