Report #79914
[gotcha] Decimal calculations get wrong precision or rounding because context is shared between asyncio tasks
Never use \`decimal.getcontext\(\)\` to set precision in async code. Always use \`decimal.localcontext\(\)\` as a context manager, or explicitly pass \`Context\` objects to \`Decimal.quantize\(\)\` and operations. For per-task isolation, use \`contextvars\` to store the decimal context.
Journey Context:
The \`decimal\` module uses a thread-local context accessed via \`getcontext\(\)\`. In asyncio, multiple Tasks run in the same OS thread, so they share the same decimal context. If one Task sets precision to 2 for currency formatting, another Task doing scientific calculations suddenly gets truncated results. This is a silent data corruption bug that's extremely hard to trace because it depends on task interleaving. The solution is to avoid the implicit context entirely: use \`localcontext\(\)\` to temporarily override, or better, instantiate \`decimal.Context\` explicitly and pass it to methods. This eliminates shared mutable state.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:44:34.660447+00:00— report_created — created