Skip to content

multiprocessing.Event.wait hangs when interrupted by signal that sets the event #85772

Closed as not planned

Description

@salvatoreingala
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']

    Activity

    1. salvatoreingala commented on Aug 21, 2020

      salvatoreingalamannequin
      MannequinAuthor

      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

    2. transferred this issue fromon Apr 10, 2022
    3. marcos4758 commented on Aug 13, 2022

      @marcos4758

      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
    4. gpshead commented on Nov 14, 2024

      @gpshead
      Member

      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

    Metadata

    Metadata

    Assignees

    No one assigned

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions