Report #9331
[gotcha] threading.local subclass \_\_init\_\_ runs multiple times per thread
Move one-time initialization to \_\_new\_\_ or a factory function; use \_\_init\_\_ only for per-thread state defaults that must vary by thread.
Journey Context:
Subclassing threading.local to initialize resources \(e.g., database connections\) leads to \_\_init\_\_ being invoked every time a thread first accesses the local instance, not just once when the instance is constructed globally. This causes redundant side effects, connection leaks, and race conditions. The documented behavior treats \_\_init\_\_ as a per-thread initializer, but developers expect class-level initialization semantics. The fix separates thread-shared setup \(done in \_\_new\_\_ or a factory\) from thread-local state initialization.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:50:54.904513+00:00— report_created — created