Report #37880
[gotcha] Decimal precision or rounding changes unexpectedly in threaded code
Treat decimal.getcontext\(\) as thread-local state; do not modify it in the main thread expecting it to propagate to ThreadPoolExecutor workers. Instead, pass a Context object explicitly to Decimal.quantize or use localcontext\(\) inside the worker function.
Journey Context:
decimal.getcontext\(\) returns a thread-local Context. The main thread's precision, rounding mode, and Emin/Emax are not inherited by new threads; workers start with the default context \(28 digits, ROUND\_HALF\_EVEN\). This leads to silent precision loss or 'InvalidOperation' errors when code works in the main thread but fails in a ThreadPoolExecutor. Common mistake: setting getcontext\(\).prec = 50 at module level then using concurrent.futures. The fix isolates context management to each thread, using the context manager protocol \(localcontext\) or explicit Context objects to ensure deterministic behavior.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T18:03:46.468064+00:00— report_created — created