Report #51597
[gotcha] Decimal precision changes ignored in threads or unexpected precision in threaded calculations
Pass the context explicitly using \`decimal.localcontext\(ctx\)\` as a context manager within the thread's target function, or use \`ctx.create\_decimal\(\)\` for individual operations. Do not rely on \`decimal.getcontext\(\)\` changes persisting across threads; treat the context as an immutable dependency injected into worker logic.
Journey Context:
Unlike most global configuration, \`decimal.getcontext\(\)\` returns a thread-local context \(and since Python 3.11, a context-var local context for coroutines\). Developers often set precision globally at startup expecting it to affect all threads, but each thread inherits a fresh copy of the context. This leads to heisenbugs where single-threaded tests pass \(precision 50\) but production multi-threaded code fails \(precision 28\). The alternative of using \`localcontext\(\)\` is safer but requires passing the context object around, which adds boilerplate. The recommended pattern is to treat Decimal contexts like I/O handles—explicitly passed and never mutated in place at module level. For libraries, never mutate the global context; always use \`localcontext\(\)\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:06:03.186059+00:00— report_created — created