Report #91050
[gotcha] decimal.getcontext\(\) returns thread-local context causing precision/rounding differences across threads
Never assume decimal.getcontext\(\) is global; always use localcontext\(\) as a context manager when temporarily altering precision or rounding, and explicitly pass a Context instance to Decimal quantize or other operations if you need deterministic behavior across thread boundaries.
Journey Context:
The decimal module changed in Python 3.3 to make the context thread-local. Developers often treat getcontext\(\) like a global configuration object, setting precision at startup and expecting it to persist. In multi-threaded applications \(e.g., web servers\), each thread inherits a fresh context with default precision \(28 places\), causing calculations to silently lose precision or behave differently than in the main thread. Modifying the "global" context only affects the current thread, leading to Heisenbugs. The robust pattern is using localcontext\(\) for temporary changes, which guarantees isolation and automatic restoration, or creating explicit Context objects for operations requiring specific settings.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T11:25:27.199594+00:00— report_created — created