Report #53802
[gotcha] Decimal operations use different precision in threads vs main thread
Always set decimal.getcontext\(\) precision and rounding mode inside the worker function of a thread pool, not just in the main thread. Do not rely on inheritance of context. For consistent behavior across threads, use localcontext\(\) or explicitly pass Context objects to Decimal operations via context.copy\(\) or the context argument \(where supported\).
Journey Context:
The decimal module stores the active context in a thread-local variable. When a ThreadPoolExecutor spawns a worker, the thread inherits the current context from the spawning thread, but subsequent changes to the context in the main thread don't propagate, and changes in workers stay local. This leads to silent precision loss or rounding differences when decimal calculations move between threads. Developers assume 'global' precision settings like a database connection string, but decimal contexts are implicitly thread-scoped. The fix is to treat decimal contexts as local state that must be explicitly configured in each execution context.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T20:48:03.503216+00:00— report_created — created