You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IO streams locking can be broken after fork() with threads #84579
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=<Date2020-04-26.16:58:22.058>labels= ['3.8', 'type-bug', 'library', '3.9']
title='IO streams locking can be broken after fork() with threads'updated_at=<Date2020-04-29.16:43:20.805>user='https://github.com/Delgan'
create a new process with itself add one item to the queue
wait for the process to end
For some unknown reason, it will hangs after some time.
I know the docs said:
This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children.
That's why I added "time.sleep(1)" inside the process, to make sure all items added by the child process are consumed. You can remove it and the hang will happen faster.
I'm using Python 3.8.2 on Linux. Forcing program to terminate with Ctrl+C (twice):
^CTraceback (most recent call last):
File "bug.py", line 23, in <module>
p.join()
File "/usr/lib/python3.8/multiprocessing/process.py", line 149, in join
res =self._popen.wait(timeout)
File "/usr/lib/python3.8/multiprocessing/popen_fork.py", line 47, in waitreturnself.poll(os.WNOHANGif timeout ==0.0else0)
File "/usr/lib/python3.8/multiprocessing/popen_fork.py", line 27, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/popen_fork.py", line 27, in poll
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
Another curiosity: if 'print("Consumed:", queue.get())' is replaced by either 'print("Consumed")' or 'queue.get()', then the program keeps running without stopping. Only a combination of both makes the program to hang.
After a few runs, you'll see that the child Process hangs when trying to flush the standard streams:
Timeout (0:00:01)!
Thread 0x00007efbff6c0080 (most recent call first):
File "/home/antoine/cpython/default/Lib/multiprocessing/util.py", line 435 in _flush_std_streams
File "/home/antoine/cpython/default/Lib/multiprocessing/process.py", line 335 in _bootstrap
File "/home/antoine/cpython/default/Lib/multiprocessing/popen_fork.py", line 71 in _launch
File "/home/antoine/cpython/default/Lib/multiprocessing/popen_fork.py", line 19 in __init__
File "/home/antoine/cpython/default/Lib/multiprocessing/context.py", line 276 in _Popen
File "/home/antoine/cpython/default/Lib/multiprocessing/context.py", line 224 in _Popen
File "/home/antoine/cpython/default/Lib/multiprocessing/process.py", line 121 in start
File "/home/antoine/cpython/default/bpo40399.py", line 25 in <module>
Child process failed!
changed the title [-]Program hangs if process created right after adding object to a Queue[/-][+]IO streams locking can be broken after fork() with threads[/+]on Apr 28, 2020
changed the title [-]Program hangs if process created right after adding object to a Queue[/-][+]IO streams locking can be broken after fork() with threads[/+]on Apr 28, 2020
Delgan commented on Apr 28, 2020
Delganmannequin
MannequinAuthor
More actions
Thank you for having looked into the problem.
To be more specific, I don't generally mix threads with multiprocessing, but it's a situation where there is one global and hidden consumer thread listening to a queue for non-blocking logging.
Well, as the documentation states, QueueListener.start """starts up a background thread to monitor the queue for LogRecords to process""" :-)
Delgan commented on Apr 29, 2020
Delganmannequin
MannequinAuthor
More actions
Yeah, I just wanted to illustrate the issue with a more realistic example. The thread is often abstracted away by a class or a library. Conclusion: do not abstract it away. :)
I've noticed that the mere fact of using "sys.stderr.write()", without even involving a queue, could cause the problem.
Out of curiosity: my understanding of "sys.stderr" being a "TextIOWrapper" implies it's not thread-safe. Therefore, do you have any idea of which lock is involved in this issue?
I also encountered this issue while using Celery using python 3.11.4, which caused deadlocks in multiple processes. The reason is that the BufferedWrite protocol did not reset the internal lock when fork proccess.
Activity
Delgan commentedon Apr 26, 2020
Hi.
I have a very basic program:
For some unknown reason, it will hangs after some time.
I know the docs said:
That's why I added "time.sleep(1)" inside the process, to make sure all items added by the child process are consumed. You can remove it and the hang will happen faster.
I'm using Python 3.8.2 on Linux. Forcing program to terminate with Ctrl+C (twice):
Delgan commentedon Apr 28, 2020
I noticed the bug is reproducible even if the child process does not put object in the queue:
Delgan commentedon Apr 28, 2020
Another curiosity: if 'print("Consumed:", queue.get())' is replaced by either 'print("Consumed")' or 'queue.get()', then the program keeps running without stopping. Only a combination of both makes the program to hang.
Also reproducible on 3.5.2.
pitrou commentedon Apr 28, 2020
I can reproduce on Ubuntu 18.04 with git master.
Here is a better example which clearly shows the issue:
https://gist.github.com/pitrou/d9784d5ec679059cd02fce4b38ea2fa6
After a few runs, you'll see that the child Process hangs when trying to flush the standard streams:
Timeout (0:00:01)!
Thread 0x00007efbff6c0080 (most recent call first):
File "/home/antoine/cpython/default/Lib/multiprocessing/util.py", line 435 in _flush_std_streams
File "/home/antoine/cpython/default/Lib/multiprocessing/process.py", line 335 in _bootstrap
File "/home/antoine/cpython/default/Lib/multiprocessing/popen_fork.py", line 71 in _launch
File "/home/antoine/cpython/default/Lib/multiprocessing/popen_fork.py", line 19 in __init__
File "/home/antoine/cpython/default/Lib/multiprocessing/context.py", line 276 in _Popen
File "/home/antoine/cpython/default/Lib/multiprocessing/context.py", line 224 in _Popen
File "/home/antoine/cpython/default/Lib/multiprocessing/process.py", line 121 in start
File "/home/antoine/cpython/default/bpo40399.py", line 25 in <module>
Child process failed!
@Delgan, mixing processes and threads is problematic with the default settings. See here:
https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
"""Note that safely forking a multithreaded process is problematic."""
If you call
multiprocessing.set_start_method("forkserver")at the start of your program, the problem will disappear.[-]Program hangs if process created right after adding object to a Queue[/-][+]IO streams locking can be broken after fork() with threads[/+][-]Program hangs if process created right after adding object to a Queue[/-][+]IO streams locking can be broken after fork() with threads[/+]Delgan commentedon Apr 28, 2020
Thank you for having looked into the problem.
To be more specific, I don't generally mix threads with multiprocessing, but it's a situation where there is one global and hidden consumer thread listening to a queue for non-blocking logging.
Actually, I think the problem is reproducible using the QueueListener provided in "logging.handlers". The following snippet is inspired by this part of the documentation: https://docs.python.org/3/howto/logging-cookbook.html#dealing-with-handlers-that-block
----------------------
pitrou commentedon Apr 28, 2020
Well, as the documentation states,
QueueListener.start"""starts up a background thread to monitor the queue for LogRecords to process""" :-)Delgan commentedon Apr 29, 2020
Yeah, I just wanted to illustrate the issue with a more realistic example. The thread is often abstracted away by a class or a library. Conclusion: do not abstract it away. :)
I've noticed that the mere fact of using "sys.stderr.write()", without even involving a queue, could cause the problem.
Out of curiosity: my understanding of "sys.stderr" being a "TextIOWrapper" implies it's not thread-safe. Therefore, do you have any idea of which lock is involved in this issue?
pitrou commentedon Apr 29, 2020
The TextIOWrapper uses an underlying BufferedWriter, which is thread-safe (and therefore has an internal lock).
LuYanFCP commentedon Dec 25, 2024
I also encountered this issue while using Celery using python 3.11.4, which caused deadlocks in multiple processes. The reason is that the BufferedWrite protocol did not reset the internal lock when fork proccess.