← 输入框|github.com|刷新
Skip to content

Multiprocessing spawn/forkserver fails to pass Queues #73151

Closed

Description

@duckythescientist
BPO 28965
Nosy @pitrou, @applio, @mouse07410, @Piython, @duckythescientist

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 2017-07-22.21:26:54.918>
created_at = <Date 2016-12-14.00:51:48.321>
labels = ['type-bug', 'library']
title = 'Multiprocessing spawn/forkserver fails to pass Queues'
updated_at = <Date 2020-03-29.21:52:57.579>
user = 'https://github.com/duckythescientist'

bugs.python.org fields:

activity = <Date 2020-03-29.21:52:57.579>
actor = 'mouse07410'
assignee = 'none'
closed = True
closed_date = <Date 2017-07-22.21:26:54.918>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2016-12-14.00:51:48.321>
creator = 'Sean Murphy'
dependencies = []
files = []
hgrepos = []
issue_num = 28965
keywords = []
message_count = 6.0
messages = ['283150', '283305', '298871', '298872', '298873', '365280']
nosy_count = 5.0
nosy_names = ['pitrou', 'davin', 'mouse07410', 'kumaar.nd', 'Sean Murphy']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue28965'
versions = ['Python 3.5']

Activity

  1. duckythescientist commented on Dec 14, 2016

    duckythescientistmannequin
    MannequinAuthor

    Python fails to pass a Queue when calling Process with multiprocessing.set_start_method set to "spawn" or "forkserver".

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python3.5/multiprocessing/spawn.py", line 106, in spawn_main
        exitcode = _main(fd)
      File "/usr/lib/python3.5/multiprocessing/spawn.py", line 116, in _main
        self = pickle.load(from_parent)
      File "/usr/lib/python3.5/multiprocessing/synchronize.py", line 111, in __setstate__
        self._semlock = _multiprocessing.SemLock._rebuild(*state)
    FileNotFoundError: [Errno 2] No such file or directory

    Here is a minimized example:

    #!/usr/bin/env python3
    
    import multiprocessing
    
    def check_child(q):
        print("Queue", q)
    
    
    if __name__ == '__main__':
        multiprocessing.set_start_method('spawn')
        # multiprocessing.set_start_method('fork')
        # multiprocessing.set_start_method('forkserver')
    
        q = multiprocessing.Queue(-1)
        print("q", q)
    
        proc = multiprocessing.Process(target=check_child, args=(q,))
        proc.start()
    

    Also, this fails when the Queue is implicitly passed to the child.

    class Blerg():
        def __init__(self):
            self.q = multiprocessing.Queue(-1)
    
        def print_queue(self):
            print("Queue", self.q)
    
    
    if __name__ == '__main__':
        multiprocessing.set_start_method('spawn')
    
        blerg = Blerg()
    
        blerg.print_queue()
    
        proc = multiprocessing.Process(target=blerg.print_queue)
        proc.start()
    
    $ python3 --version
    Python 3.5.2

    Windows (which defaults to "spawn" style multiprocessing) does not seem to have this issue (at least in 2.7.12).

  2. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    stdlibStandard Library Python modules in the Lib/ directory
    on Dec 14, 2016
  3. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Dec 14, 2016
  4. piython commented on Dec 15, 2016

    piythonmannequin
    Mannequin

    hi,

    few comments on this: [when tested with 'spawn']

    1. the document(3x, 17.2.1.2) clearly mentions that 'spawn' doesnt inherit the file-handles (hence FileNotFoundError).
    2. the document's examples has spawn() followed by join() where parent waits for the Child to finish and exits gracefully. The error "OSError: [Errno 9] Bad file descriptor" is seen for Child process.
    3. Isnt exception-handling for null file descriptors sufficient (with corresponding Document updates)

    am not a core developer yet and still trying to learn and contribute.

    Thanks

  5. pitrou commented on Jul 22, 2017

    @pitrou
    Member

    Ok, this is really an error in your script. If you add p.join() to wait for the process to end, you'll see no error pops up. What really happens is that the parent process ends before the child is fully started, and the resource underlying the queue is cleaned up at this point.

  6. pitrou commented on Jul 22, 2017

    @pitrou
    Member

    More exactly, since the child process is not "daemonic", the parent process waits for it to end, but after collecting the queue's resources.

  7. pitrou commented on Jul 22, 2017

    @pitrou
    Member

    So, while it might be possible to improve on this, it would risk fragilizing other use cases, and besides, this is not a problem that is likely to happen in real usage. Therefore, I'd rather close this issue.

  8. mouse07410 commented on Mar 29, 2020

    mouse07410mannequin
    Mannequin

    On MacOS Catalina 10.15.4, I still see this problem occasionally even with p.join() added. See https://bugs.python.org/msg365251 and subsequent messages.

    Also, see https://bugs.python.org/issue40106.

  9. transferred this issue fromon Apr 10, 2022
  10. geryogam commented on May 8, 2022

    @geryogam
    Contributor

    Ok, this is really an error in your script. If you add p.join() to wait for the process to end, you'll see no error pops up. What really happens is that the parent process ends before the child is fully started, and the resource underlying the queue is cleaned up at this point.

    And if you don’t want to join the process explicitly like @pitrou suggested, here is an alternative: https://stackoverflow.com/a/72164506/2326961

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

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions