Report #56173
[gotcha] threading.local data leaks between tasks in ThreadPoolExecutor
Explicitly delete or reset all \`threading.local\(\)\` attributes at the start of each task function \(ideally in a \`try...finally\` block\) to ensure a clean state, or use a context manager that cleans up thread-local storage on exit.
Journey Context:
ThreadPoolExecutor maintains a pool of worker threads that persist between task submissions to avoid thread creation overhead. \`threading.local\(\)\` creates thread-local storage that persists for the lifetime of the thread. Consequently, if Task A sets \`threading.local\(\).user\_id = 'Alice'\` and the thread returns to the pool, when Task B runs on that same thread, \`threading.local\(\).user\_id\` still equals 'Alice'. This violates the assumption that each task starts with fresh state, leading to information disclosure or state corruption. The fix requires explicit cleanup: reset thread-local attributes in a \`finally\` block at the start of each task to guarantee isolation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:46:45.459694+00:00— report_created — created