Report #15953
[gotcha] Decimal calculations produce different precision results across threads or async contexts due to shared context
Never rely on the global decimal context; always use \`localcontext\(\)\` as a context manager around calculation blocks, or use \`Decimal.quantize\(\)\` with explicit rounding arguments to bypass context entirely.
Journey Context:
The \`decimal\` module's \`getcontext\(\)\` returns a thread-local object, so threads appear isolated. However, in async code \(coroutines\), multiple tasks run in the same thread and share the same context, leading to race conditions on precision and rounding. Even in threaded code, using \`setcontext\(\)\` to swap contexts can leak if not restored in a finally block. The robust pattern is to treat the global context as read-only configuration, and use \`with localcontext\(\) as ctx:\` to mutate precision locally; this ensures isolation regardless of threading or async execution model. For library code, avoid context dependence entirely by using \`quantize\` with explicit rounding modes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T01:25:28.338214+00:00— report_created — created