Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiprocessing deadlock on Mac OS X when queue collected before process terminates #51449

Closed
brianquinlan opened this issue Oct 24, 2009 · 4 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@brianquinlan
Copy link
Contributor

BPO 7200
Nosy @brianquinlan, @applio

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 2015-02-13.16:38:43.960>
created_at = <Date 2009-10-24.23:10:35.758>
labels = ['invalid', 'type-bug', 'library']
title = 'multiprocessing deadlock on Mac OS X when queue collected before process terminates'
updated_at = <Date 2015-02-13.16:38:43.957>
user = 'https://github.com/brianquinlan'

bugs.python.org fields:

activity = <Date 2015-02-13.16:38:43.957>
actor = 'davin'
assignee = 'jnoller'
closed = True
closed_date = <Date 2015-02-13.16:38:43.960>
closer = 'davin'
components = ['Library (Lib)']
creation = <Date 2009-10-24.23:10:35.758>
creator = 'bquinlan'
dependencies = []
files = []
hgrepos = []
issue_num = 7200
keywords = []
message_count = 4.0
messages = ['94440', '119498', '200327', '235902']
nosy_count = 4.0
nosy_names = ['bquinlan', 'jnoller', 'asksol', 'davin']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7200'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

@brianquinlan
Copy link
Contributor Author

This code:

import multiprocessing
import queue

def _process_worker(q):
    while True:
        try:
            something = q.get(block=True, timeout=0.1)
        except queue.Empty:
            return
        else:
            pass
            # print('Grabbed item from queue:', something)


def _make_some_processes(q):
    processes = []
    for _ in range(10):
        p = multiprocessing.Process(target=_process_worker, args=(q,))
        p.start()
        processes.append(p)
    return processes

#p = []
def _do(i):
    print('Run:', i)
    q = multiprocessing.Queue()
#    p.append(q)
    print('Created queue')
    for j in range(30):
        q.put(i*30+j)
    processes = _make_some_processes(q)
    print('Created processes')

    while not q.empty():
        pass
    print('Q is empty')

for i in range(100):
    _do(i)

Produces this output on Mac OS X (it produces the expected output on
Linux and Windows):

Run: 0
Created queue
Grabbed item from queue: 0
...
Grabbed item from queue: 29
Created processes
Q is empty
Run: 1
Created queue
Grabbed item from queue: 30
...
Grabbed item from queue: 59
Created processes
Q is empty
Run: 2
Created queue
Created processes
<no further output>

Changing the code as follows:

+ p = []
def _do(i):
print('Run:', i)
q = multiprocessing.Queue()
+ p.append(q)
print('Created queue')
for j in range(30):
q.put(i*30+j)
processes = _make_some_processes(q)
print('Created processes')

    while not q.empty():
        pass
    print('Q is empty')

fixes the deadlock. So it looks like if a multiprocessing.Queue is
collected with sub-processes still using it then calling some methods on
other multiprocessing.Queues with deadlock.

@brianquinlan brianquinlan added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 24, 2009
@BreamoreBoy BreamoreBoy mannequin assigned jnoller Jul 11, 2010
@asksol
Copy link
Mannequin

asksol mannequin commented Oct 24, 2010

Queue uses multiprocessing.util.Finalize, which uses weakrefs to track when the object is out of scope, so this is actually expected behavior.

IMHO it is not a very good approach, but changing the API to use explicit close methods is a little late at this point, I guess.

@brianquinlan
Copy link
Contributor Author

OK, working as intended.

@applio
Copy link
Member

applio commented Feb 13, 2015

This issue was marked as "not a bug" by OP a while back but for whatever reason it did not also get marked as "closed". Going ahead with closing it now.

@applio applio closed this as completed Feb 13, 2015
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants