Worker management race conditions in `ThreadPoolExecutor` and `ProcessPoolExecutor`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
We have tests that ThreadPoolExecutor and ProcessPoolExecutor reuse workers. The test wait for the previous task to complete before submitting the next task.
The problem is that the tasks are potentially completed before the thread or process is marked as idle. Calling Future.result() doesn't guarantee that there is an idle worker. This happens much more frequently with the GIL disabled, but it can happen with the GIL enabled too if you have an unlucky GIL switch.
For example, you can reliably trigger the problem with the GIL enabled by adding a short time.sleep(0.01) immediately after the work_item.run() call in:
https://github.com/python/cpython/blob/29829b58a8328a7c2ccacaa74c1d7d120a5e5ca5/Lib/concurrent/futures/thread.py#L92
It's not clear to me if this is just a problem with the unit tests or an actual bug with the executor implementation.
Linked PRs
- gh-117355
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the cited reuse tests in Lib/test/test_concurrent_futures/test_thread_pool.py and the worker lifecycle code in Lib/concurrent/futures/thread.py; inspect the corresponding ProcessPoolExecutor paths as well. Determine whether the race requires stronger test synchronization or an executor change, then run the concurrent-futures test suite and ensure worker reuse is reliable under both GIL-enabled and GIL-disabled execution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, distributed-systems, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100