Report #30064
[gotcha] Decimal precision and rounding settings leaking between concurrent async tasks
Use \`decimal.localcontext\(\)\` as a context manager inside async functions to create temporary context copies, or use \`contextvars\` \(PEP 567\) to maintain per-task decimal contexts explicitly.
Journey Context:
\`decimal.getcontext\(\)\` returns a thread-local context object. In async/await concurrency, many tasks interleave on a single thread. If one task modifies precision, rounding, or traps \(e.g., \`getcontext\(\).prec = 2\`\), all subsequent tasks on that thread inherit these mutated settings. This causes silent numerical corruption in financial calculations where precision assumptions differ between modules. The thread-local model predates async Python and assumes thread-per-request isolation, which fails in single-threaded async models.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T04:51:03.638393+00:00— report_created — created