Skip to content

multiprocessing.dummy.Pool not joining threads on shutdown #107616

Open

Description

@mariokostelac

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

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()
, but because
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
doesn't have a terminate attr, it doesn't join threads but just proceeds.

Should

if pool and hasattr(pool[0], 'terminate'):
just check for existence of 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

Activity

  1. added
    type-bug
    on Aug 4, 2023
  2. mariokostelac commented on Aug 4, 2023

    @mariokostelac
    Author
  3. sunmy2019 commented on Aug 7, 2023

    @sunmy2019
    Member

    Unlike process, you cannot terminate a thread.

  4. sunmy2019 commented on Aug 7, 2023

    @sunmy2019
    Member

    But I see your point here. Will you create a PR?

  5. mariokostelac commented on Aug 7, 2023

    @mariokostelac
    Author
  6. added
    stdlib
    on Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions