Report #47350
[gotcha] Incorrect precision in Decimal calculations across async tasks or threads
Never mutate getcontext\(\).prec directly in library code. Use localcontext\(\) as a context manager to scope precision changes. In async code, be aware that localcontext\(\) is thread-safe but not task-safe; consider using decimal.setcontext\(\) with contextvars if Python 3.11\+, or explicitly pass contexts to quantize\(\) operations.
Journey Context:
decimal.getcontext\(\) returns a thread-local context. In async code, multiple tasks run on the same thread, so they share the same decimal context. If one task changes precision \(e.g., getcontext\(\).prec = 2\), all other tasks on that thread now have precision 2, causing silent calculation errors. Python 3.11 added contextvars support to decimal to fix this, but the default behavior remains thread-local, not task-local. The safe pattern is always using localcontext\(\) which properly handles restoration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T09:57:39.383698+00:00— report_created — created