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
multiprocessing.Event.wait hangs when interrupted by signal that sets the event #85772
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-08-21.08:01:43.342>labels= ['type-bug', '3.8']
title='multiprocessing.Event.wait hangs when interrupted by signal that sets the event'updated_at=<Date2020-08-21.08:01:43.342>user='https://bugs.python.org/salvatoreingala'
According to the docs, multiprocessing.Event is a clone of threading.Event. Yet, there is a strange behavior that is observed only on multiprocessing.Event. If an event.wait() is interrupted by a SIGINT and the signal handler sets the event, then the call to event.set() hangs if event is an instance of multiprocessing.Event, in what looks like a deadlock; instead, it works as expected if it's an instance of threading.Event.
I just spent a lot of time with this problem. I have a multiprocessing.Event that sleeps the MainThread of a child process and an interrupt handler that sets that event. As described here, when the interrupt triggers the event, the process hangs. Doing some research on the web, including on the cited stackoverflow topic, I got the following solution:
Solutions for Linux with fork start method:
In the interrupt handler, call threading.Thread(target=exit_event.set).start()
Change multiprocessing.Event to threading.Event
Solution for Linux with spawn start method
In the interrupt handler, call threading.Thread(target=exit_event.set).start()
Windows:
Instead of using signal.signal() to change the interrupt handler, the solution was to use:
Activity
salvatoreingala commentedon Aug 21, 2020
According to the docs, multiprocessing.Event is a clone of threading.Event. Yet, there is a strange behavior that is observed only on multiprocessing.Event. If an event.wait() is interrupted by a SIGINT and the signal handler sets the event, then the call to event.set() hangs if event is an instance of multiprocessing.Event, in what looks like a deadlock; instead, it works as expected if it's an instance of threading.Event.
See the file attached for a reproduction.
It seems to have been so for a long time, see: https://stackoverflow.com/questions/24422154/multiprocessing-event-wait-hangs-when-interrupted-by-a-signal/30831867
marcos4758 commentedon Aug 13, 2022
I just spent a lot of time with this problem. I have a multiprocessing.Event that sleeps the MainThread of a child process and an interrupt handler that sets that event. As described here, when the interrupt triggers the event, the process hangs. Doing some research on the web, including on the cited stackoverflow topic, I got the following solution:
Solutions for Linux with fork start method:
threading.Thread(target=exit_event.set).start()Solution for Linux with spawn start method
threading.Thread(target=exit_event.set).start()Windows:
Reference: https://docs.microsoft.com/en-us/windows/console/setconsolectrlhandler
This works like a charm. The key difference is that it runs the interrupt funcition on a thread (named Dummy-1).
threading.Thread(target=exit_event.set).start()gpshead commentedon Nov 14, 2024
Duplicate of #126434, lets deal with that there. This issue has great context though. (see also #95826).