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=Noneclosed_at=Nonecreated_at=<Date2011-07-04.11:43:29.105>labels= ['type-bug', '3.8', '3.9', '3.10', 'docs']
title='multiprocessing.Connection does not communicate pipe closure between parent and child'updated_at=<Date2020-11-14.11:11:23.404>user='https://bugs.python.org/lcampagn'
I have found that when using multiprocessing.Connection objects to pass data between two processes, closing one end of the pipe is not properly communicated to the other end. My expectation was that when calling recv() on the remote end, it should raise EOFError if the pipe has been closed. Instead, the remote recv() blocks indefinitely. This behavior exists on Linux and Cygwin, but NOT on native Windows.
Example:
importmultiprocessingasmdeffn(pipe):
print"recv:", pipe.recv()
print"recv:", pipe.recv()
if__name__=='__main__':
p1, p2=m.Pipe()
pr=m.Process(target=fn, args=(p2,))
pr.start()
p1.send(1)
p1.close() ## should generate EOFError in remote process
It is also a bit awkward..
Might I recommend adding a note to the documentation? It is not really
intuitive that each child should need to close the end of the pipe it isn't
using (especially since it is possible to create a child that has no
explicit access to that end of the pipe, even though it has inherited the
file descriptor).
Well, in this regard it behaves like a Unix pipe/socket (in the duplex case it's implemented with a Unix domain socket), so I find it quite natural (of course, you have to know about FD inheritance upon fork()).
I'm not convinced it's necessary, Antoine any thought on that?
Well, I think it deserves a comment in the documentation that behaviour of Pipes and Queues when one of the process terminates is undefined and implementation-dependent.
By the way, if you don't want children processes to continue running when the master exits, just make them daemonic processes (by adding "daemon=True" to the Process() constructor call).
syeberman commented on Apr 25, 2012
syebermanmannequin
Mannequin
More actions
This issue _does_ exist on Windows, and is not limited to the case where the master process exits before its children. The following code, which is almost exactly that from the 2.7.3 documentation, deadlocks on Win7 (Py3.2 and 2.7) and WinXP (Py3.2 and 2.6):
The problem is still out there under Linux. At least python3.10 and python3.11 are affected.
It works perfectly under Windows, but under Linux child copy of pipe object never knows that the parent process has closed the pipe.
See the code and output for both Windows and Linux below.
There is also a similar problem in python3.12.4 (linux, x86, NixOS) and python3.11.5 (MacOS, x86) with .recv() on a Connection:
importtimefrommultiprocessingimportProcess, Pipedeff(conn):
conn.send([42, None, 'hello'])
conn.close()
if__name__=='__main__':
parent_conn, child_conn=Pipe(duplex=False) # same problem no matter if duplex or notp=Process(target=f, args=(child_conn,))
p.start()
print(parent_conn.recv()) # prints "[42, None, 'hello']"time.sleep(2)
print("reading...")
parent_conn.recv() # this should do the broken Pipe thing, but it just blocks indefinitelyp.join()
I also tried to close both ends in the child process (in duplex mode) but it didn't help at all.
But the docs promise an EOFError.
Activity
lcampagn commented on Jul 4, 2011
I have found that when using multiprocessing.Connection objects to pass data between two processes, closing one end of the pipe is not properly communicated to the other end. My expectation was that when calling recv() on the remote end, it should raise EOFError if the pipe has been closed. Instead, the remote recv() blocks indefinitely. This behavior exists on Linux and Cygwin, but NOT on native Windows.
Example:
neologix commented on Jul 4, 2011
That's because the other end of the pipe (p1) is open in the child process (FDs are inherited on fork()).
Just add
p1.close()
at the beginning of fn() and you'll get EOF.
Closing as invalid.
lcampagn commented on Jul 4, 2011
That's interesting, thanks for your response.
It is also a bit awkward..
Might I recommend adding a note to the documentation? It is not really
intuitive that each child should need to close the end of the pipe it isn't
using (especially since it is possible to create a child that has no
explicit access to that end of the pipe, even though it has inherited the
file descriptor).
2011/7/4 Charles-François Natali <report@bugs.python.org>
neologix commented on Jul 4, 2011
Well, in this regard it behaves like a Unix pipe/socket (in the duplex case it's implemented with a Unix domain socket), so I find it quite natural (of course, you have to know about FD inheritance upon fork()).
I'm not convinced it's necessary, Antoine any thought on that?
pitrou commented on Jul 4, 2011
Well, I think it deserves a comment in the documentation that behaviour of Pipes and Queues when one of the process terminates is undefined and implementation-dependent.
By the way, there's internal support in 3.3 to reliably detect killed children, and it's used by concurrent.futures: http://docs.python.org/dev/library/concurrent.futures.html#concurrent.futures.BrokenProcessPool. However, I'm not sure there's an easy way to detect a killed master process from one of the worker processes.
neologix commented on Jul 4, 2011
Alright.
Luke, if you're motivated, feel free to provide a patch.
The relevant file is Doc/library/multiprocessing.rst.
pitrou commented on Jul 4, 2011
By the way, if you don't want children processes to continue running when the master exits, just make them daemonic processes (by adding "daemon=True" to the Process() constructor call).
syeberman commented on Apr 25, 2012
This issue _does_ exist on Windows, and is not limited to the case where the master process exits before its children. The following code, which is almost exactly that from the 2.7.3 documentation, deadlocks on Win7 (Py3.2 and 2.7) and WinXP (Py3.2 and 2.6):
If you "uncomment first", recv raises an EOFError; if you also "uncomment second", recv succeeds.
If this behaviour is the same on other platforms, then it seems all that is required is to update the documentation.
darksidds commented on May 26, 2023
The problem is still out there under Linux. At least python3.10 and python3.11 are affected.
It works perfectly under Windows, but under Linux child copy of pipe object never knows that the parent process has closed the pipe.
See the code and output for both Windows and Linux below.
Output under Windows (python3.10)
Output under Linux (Ubuntu 22.04LTS, python3.10 and python3.11)
Could you guide me through the problem? Maybe there is a workaround to that problem?
nonchris commented on Sep 11, 2024
There is also a similar problem in python3.12.4 (linux, x86, NixOS) and python3.11.5 (MacOS, x86) with .recv() on a Connection:
I also tried to close both ends in the child process (in duplex mode) but it didn't help at all.
But the docs promise an EOFError.