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.Event.wait hangs when interrupted by signal that sets the event #85772

Closed
salvatoreingala mannequin opened this issue Aug 21, 2020 · 3 comments
Closed
Labels
3.8 (EOL) end of life topic-multiprocessing type-bug An unexpected behavior, bug, or error

Comments

@salvatoreingala
Copy link
Mannequin

salvatoreingala mannequin commented Aug 21, 2020

BPO 41606
Files
  • event-test.py: Test code for multiprocessing.Event
  • 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 = None
    created_at = <Date 2020-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 = <Date 2020-08-21.08:01:43.342>
    user = 'https://bugs.python.org/salvatoreingala'

    bugs.python.org fields:

    activity = <Date 2020-08-21.08:01:43.342>
    actor = 'salvatore.ingala'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2020-08-21.08:01:43.342>
    creator = 'salvatore.ingala'
    dependencies = []
    files = ['49419']
    hgrepos = []
    issue_num = 41606
    keywords = []
    message_count = 1.0
    messages = ['375745']
    nosy_count = 1.0
    nosy_names = ['salvatore.ingala']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41606'
    versions = ['Python 3.8']

    @salvatoreingala
    Copy link
    Mannequin Author

    salvatoreingala mannequin commented 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

    @salvatoreingala salvatoreingala mannequin added 3.8 (EOL) end of life type-bug An unexpected behavior, bug, or error labels Aug 21, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @marcos4758
    Copy link

    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:

    1. In the interrupt handler, call threading.Thread(target=exit_event.set).start()
    2. Change multiprocessing.Event to threading.Event

    Solution for Linux with spawn start method

    1. In the interrupt handler, call threading.Thread(target=exit_event.set).start()

    Windows:

    1. Instead of using signal.signal() to change the interrupt handler, the solution was to use:
    import win32api
    win32api.SetConsoleCtrlHandler(handler, add)
    

    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).

    1. (Not a great solution) In the interrupt handler, call threading.Thread(target=exit_event.set).start()
      • This solves the hang issue, but raises an InterruptedError exception

    @gpshead
    Copy link
    Member

    gpshead commented Nov 14, 2024

    Duplicate of #126434, lets deal with that there. This issue has great context though. (see also #95826).

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life topic-multiprocessing type-bug An unexpected behavior, bug, or error
    Projects
    Development

    No branches or pull requests

    3 participants