Report #4982
[gotcha] threading.local data leaking between ThreadPoolExecutor tasks
Explicitly delete threading.local attributes or use a context manager to reset state at the end of each task function; do not assume thread-local data is task-scoped.
Journey Context:
threading.local\(\) provides thread-local storage, but ThreadPoolExecutor reuses worker threads across multiple tasks. If task A sets a thread-local variable and does not clean it up, task B running on the same thread will see that stale value. This is a silent data leak that breaks isolation assumptions and can cause security issues \(session bleeding\) or logic errors. Developers often assume thread-local means 'request-local' or 'task-local', but it is 'OS-thread-local'. The fix is mandatory cleanup in a finally block or using a context manager that resets the local storage on exit. Alternatively, use contextvars \(Python 3.7\+\) which are context-local \(task-local in async\) rather than thread-local, but for threads, contextvars require explicit context copying which ThreadPoolExecutor does not do by default.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:24:47.424231+00:00— report_created — created