Report #59913
[gotcha] Decimal arithmetic context precision leaks between concurrent asyncio tasks
Wrap every task entry point or Decimal operation block with decimal.localcontext\(\) to isolate precision and rounding settings; do not mutate decimal.getcontext\(\) directly in async code.
Journey Context:
Decimal contexts are thread-local via threading.local\(\). In asyncio, multiple coroutines run interleaved on a single thread, so they share the same active context. If Task A sets precision to 2 and yields control, Task B inherits that precision, silently corrupting calculations. Common mistake is assuming 'thread-local' equals 'task-local' in single-threaded async. Using localcontext\(\) pushes a new context onto a logical stack that follows the coroutine's execution flow, not the OS thread. The alternative—passing context objects manually to each Decimal quantize call—is verbose but safer for library code that must not mutate global state.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T07:03:13.849873+00:00— report_created — created