Report #10145
[gotcha] Decimal getcontext\(\) precision leaks across thread boundaries
Never mutate \`decimal.getcontext\(\).prec\` directly. Always use the \`localcontext\(\)\` context manager for temporary precision changes, or explicitly pass a \`Context\` instance to \`Decimal\` quantize operations via the \`context\` parameter.
Journey Context:
The \`decimal\` module's \`getcontext\(\)\` returns a thread-local Context object, not a function-local one. Many developers assume they can safely write \`decimal.getcontext\(\).prec = 50\` inside a function to increase precision for a specific calculation. Because this mutates thread-local state, the modified precision persists for all subsequent decimal operations in that same thread, including library code that expects the default 28-digit precision. This causes silent data corruption in financial calculations or performance degradation when high precision propagates unexpectedly. The \`localcontext\(\)\` manager creates a temporary scope that automatically restores the previous precision, thread-local state, and traps on exit, making it the only safe pattern for temporary decimal configuration changes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:54:11.185220+00:00— report_created — created