Report #7667
[gotcha] Decimal context not inherited by threading.Thread targets causing precision mismatches
Explicitly copy the decimal context to new threads using \`decimal.localcontext\(\)\` or manually set \`decimal.getcontext\(\).copy\(\)\` at the start of the thread target function. Never assume child threads inherit the parent's Decimal precision.
Journey Context:
The decimal module uses thread-local storage for its arithmetic context \(precision, rounding mode, etc.\). When spawning a new thread via threading.Thread, the new thread receives a fresh default context \(28 places of precision\), not a copy of the parent thread's context. This causes silent precision loss or changes in rounding behavior when computations move to worker threads. The \`localcontext\(\)\` context manager creates a context that is properly thread-local, but you must explicitly enter it in the new thread. The robust pattern is to capture the parent's context \(\`ctx = decimal.getcontext\(\).copy\(\)\`\) and restore it \(\`decimal.setcontext\(ctx\)\`\) at the thread entry point, or use a thread initializer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T03:21:57.520445+00:00— report_created — created