Repository navigation
Open
Description
Bug report
Checklist
- I am confident this is a bug in CPython, not a bug in a third-party project
- I have searched the CPython issue tracker, and am confident this bug has not been reported before
A clear and concise description of the bug
Multiprocessing.dummy.Pool is not joining running threads as part of the shutdown procedure.
From what I understand, it's supposed to happen in
cpython/Lib/multiprocessing/pool.py
Lines 726 to 732 in 0bd784b
| if pool and hasattr(pool[0], 'terminate'): | |
| util.debug('joining pool workers') | |
| for p in pool: | |
| if p.is_alive(): | |
| # worker has not yet exited | |
| util.debug('cleaning up worker %d' % p.pid) | |
| p.join() |
cpython/Lib/multiprocessing/dummy/__init__.py
Lines 34 to 59 in 0bd784b
| class DummyProcess(threading.Thread): | |
| def __init__(self, group=None, target=None, name=None, args=(), kwargs={}): | |
| threading.Thread.__init__(self, group, target, name, args, kwargs) | |
| self._pid = None | |
| self._children = weakref.WeakKeyDictionary() | |
| self._start_called = False | |
| self._parent = current_process() | |
| def start(self): | |
| if self._parent is not current_process(): | |
| raise RuntimeError( | |
| "Parent is {0!r} but current_process is {1!r}".format( | |
| self._parent, current_process())) | |
| self._start_called = True | |
| if hasattr(self._parent, '_children'): | |
| self._parent._children[self] = None | |
| threading.Thread.start(self) | |
| @property | |
| def exitcode(self): | |
| if self._start_called and not self.is_alive(): | |
| return 0 | |
| else: | |
| return None | |
terminate attr, it doesn't join threads but just proceeds.
Should
cpython/Lib/multiprocessing/pool.py
Line 726 in 0bd784b
| if pool and hasattr(pool[0], 'terminate'): |
join attribute instead?
Repro
import time
from multiprocessing.dummy import Pool as ThreadPool
def sleep_and_print(i):
print("Worker start", i)
time.sleep(i/10)
print("Worker end", i)
if __name__ == "__main__":
print("** multiprocessing.dummy.Pool test")
print("before thread pool")
with ThreadPool(2) as pool:
results = [pool.apply_async(sleep_and_print, (i,)) for i in [1, 2]]
results[0].get()
print("after thread pool is destroyed")
# Give enough time for result[1] to finish
time.sleep(2)Output:
** multiprocessing.dummy.Pool test
before thread pool
Worker start 1
Worker start 2
Worker end 1
after thread pool is destroyed
Worker end 2
We can see that "worker end 2" happened after the thread pool was supposed to be destroyed, and threads properly joined.
Your environment
- CPython versions tested on:
Python 3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin - Operating system and architecture:
Darwin Marios-MacBook-Pro.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64
Reacted by Pedro Tabacof and sunmy2019
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsNo status
Activity
mariokostelac commentedon Aug 4, 2023
cc @applio @gpshead.
sunmy2019 commentedon Aug 7, 2023
Unlike process, you cannot terminate a thread.
sunmy2019 commentedon Aug 7, 2023
But I see your point here. Will you create a PR?
mariokostelac commentedon Aug 7, 2023