Skip to content

Exception in multiprocessing/context.py under load #85037

Closed

Description

@ArkadyM
mannequin
BPO 40860
Nosy @pitrou, @applio, @iritkatriel
Superseder
  • bpo-40815: Multiprocessing docs don't describe thread-safety
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-10-20.11:16:26.863>
    created_at = <Date 2020-06-04.03:06:13.020>
    labels = ['3.7', 'type-crash']
    title = 'Exception in multiprocessing/context.py under load'
    updated_at = <Date 2020-10-20.11:16:26.862>
    user = 'https://bugs.python.org/ArkadyM'

    bugs.python.org fields:

    activity = <Date 2020-10-20.11:16:26.862>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-10-20.11:16:26.863>
    closer = 'iritkatriel'
    components = []
    creation = <Date 2020-06-04.03:06:13.020>
    creator = 'Arkady M'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40860
    keywords = []
    message_count = 12.0
    messages = ['370695', '370700', '370708', '370889', '371166', '371184', '371859', '375087', '375092', '375102', '375104', '375294']
    nosy_count = 4.0
    nosy_names = ['pitrou', 'davin', 'iritkatriel', 'Arkady M']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '40815'
    type = 'crash'
    url = 'https://bugs.python.org/issue40860'
    versions = ['Python 3.7']

    Activity

    1. ArkadyM commented on Jun 4, 2020

      ArkadyMmannequin
      MannequinAuthor

      I am running an HTTP server (socketserver.ThreadingMixIn, http.server.HTTPServer) in a Docker container (FROM ubuntu:19.10)

      Occasionally I get an exception:

      Exception happened during processing of request from ('172.17.0.1', 35756)
      Traceback (most recent call last):
        File "/usr/lib/python3.7/socketserver.py", line 650, in process_request_thread
          self.finish_request(request, client_address)
        File "/usr/lib/python3.7/socketserver.py", line 360, in finish_request
          self.RequestHandlerClass(request, client_address, self)
        File "service.py", line 221, in __init__
          super(UrlExtractorServer, self).__init__(*args, **kwargs)
        File "/usr/lib/python3.7/socketserver.py", line 720, in __init__
          self.handle()
        File "/usr/lib/python3.7/http/server.py", line 426, in handle
          self.handle_one_request()
        File "/usr/lib/python3.7/http/server.py", line 414, in handle_one_request
          method()
        File "service.py", line 488, in do_POST
          self._post_extract(url)
        File "service.py", line 459, in _post_extract
          extracted_links, err_msg = self._extract_links(transaction_id, attachment_id, zip_password, data)
        File "service.py", line 403, in _extract_links
          error, results = call_timeout(process_deadline, extractor.extract_links_binary_multiprocess, args=data)
        File "service.py", line 175, in call_timeout
          manager = multiprocessing.Manager()
        File "/usr/lib/python3.7/multiprocessing/context.py", line 56, in Manager
          m.start()
        File "/usr/lib/python3.7/multiprocessing/managers.py", line 563, in start
          self._process.start()
        File "/usr/lib/python3.7/multiprocessing/process.py", line 111, in start
          _cleanup()
        File "/usr/lib/python3.7/multiprocessing/process.py", line 56, in _cleanup
          if p._popen.poll() is not None:
      AttributeError: 'NoneType' object has no attribute 'poll'

      I am in the process of preparingof of a reasonably simple piece of code demonstrating the problem.

      Meanwhile the following can be important. In the code below I ma getting the elapse < timeout (20 times from 70K). In all case psutil.Process() returned psutil.NoSuchProcess

          time_start = time.time()
          job = multiprocessing.Process(target=func, args=(args, results), kwargs=kwargs)
          job.start()
          job.join(timeout)
          elapsed = time.time()-time_start
          if job.is_alive():
              try:
                  process = psutil.Process(job.pid)
                  process_error = f"pid {job.pid} status {process.status} {process}"
              except Exception as e:
                  process_error = f"psutil.Process() failed {e}"
              if elapsed < timeout:
                  print("elapsed < timeout")
    2. added
      3.7 (EOL)
      type-crash
      on Jun 4, 2020
    3. ArkadyM commented on Jun 4, 2020

      ArkadyMmannequin
      MannequinAuthor

      This code reproduces the problem https://github.com/larytet-py/multiprocess
      I assume that my use of join() is not correct.

    4. ArkadyM commented on Jun 4, 2020

      ArkadyMmannequin
      MannequinAuthor

      The problem is likely in the call to multiprocessing.Process.join() with timeout. If I use timeout=None the code works.

    5. ArkadyM commented on Jun 7, 2020

      ArkadyMmannequin
      MannequinAuthor

      Is there any news about?

      This 50 lines sample reproduces the problem https://github.com/larytet-py/multiprocess
      Please let me know if more information is needed.

    6. ArkadyM commented on Jun 10, 2020

      ArkadyMmannequin
      MannequinAuthor

      Update. I have reproduced the problem in the code not calling Process.join() at all. It require more time and more load, but eventually Process.start() crashes.

      Posted a question on https://stackoverflow.com/questions/62276345/call-to-pythons-mutliprocessing-process-join-fails
      The problem reproduces in MacOS as well.

    7. ArkadyM commented on Jun 10, 2020

      ArkadyMmannequin
      MannequinAuthor

      A workaround is to synchronize the call to Process.start()

      diff --git a/main.py b/main.py
      index d09dc53..49d68f0 100644
      --- a/main.py
      +++ b/main.py
      @@ -26,17 +26,24 @@ def load_cpu(deadline):
      while time.time() - start < 0.2*deadline:
      math.pow(random.randint(0, 1), random.randint(0, 1))

      +def join_process(job, timeout):
      +    time_start = time.time()
      +    while time.time()-time_start < timeout and job.is_alive():
      +        time.sleep(0.1   * timeout)
      +        continue
      +
       job_counter = 0
      +lock = threading.Lock()
       def spawn_job(deadline):
           '''
           Creat a new Process, call join(), process errors
           '''    
           global job_counter
           time_start = time.time()
      -    job = multiprocessing.Process(target=load_cpu, args=(deadline, ))
      -    job.start()
      -    # timeout=None in the call to join() solves the problem
      -    job.join(deadline)
      +    with lock:
      +        job = multiprocessing.Process(target=load_cpu, args=(deadline, ))
      +        job.start()
      +    join_process(job, deadline)
    8. ArkadyM commented on Jun 19, 2020

      ArkadyMmannequin
      MannequinAuthor

      There is a memory leak every time call to join() fails (is https://bugs.python.org/issue37788 relevant?)

    9. iritkatriel commented on Aug 9, 2020

      @iritkatriel
      Member

      The source code for the Process class is here: https://github.com/python/cpython/blob/master/Lib/multiprocessing/process.py

      You can see that join and start both modify the global, non thread safe _children set. I'm guessing this is where you're seeing interference between threads.

      I'm not sure a lock on start is enough - I think you need to get the lock for the join_process(job, deadline) call as well, because join can modify _children too. (Or, alternatively, manage all processes from a single thread.)

    10. ArkadyM commented on Aug 10, 2020

      ArkadyMmannequin
      MannequinAuthor
    11. iritkatriel commented on Aug 10, 2020

      @iritkatriel
      Member

      There is an open ticket to improve documentation of thread safety of the multiprocessing module module: https://bugs.python.org/issue40815

      Is there anything remaining to do on this ticket?

    12. ArkadyM commented on Aug 10, 2020

      ArkadyMmannequin
      MannequinAuthor

      "documentation of thread safety"

      I find it surprising that a module called "multiprocessing" has not a thread safe API.

      If this is inevitable, I guess that's the life. I expect nothing less that a bold bright red font at the top of the document page.

      Protecting a call to join() with a mutex would impact latency of the API: the slowest subprocess can win the race.

    13. iritkatriel commented on Aug 13, 2020

      @iritkatriel
      Member

      I agree that it would be better not to lock join, and instead manage each process from one thread.

      I think this ticket can be closed as a duplicate of bpo-40815.

    14. transferred this issue fromon Apr 10, 2022
    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

      3.7 (EOL)type-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions