Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
def __init__(self, selector=None):
super().__init__(selector)
self._signal_handlers = {}
self._old_handlers = {}

def close(self):
super().close()
Expand Down Expand Up @@ -113,7 +114,11 @@ def add_signal_handler(self, sig, callback, *args):
# Register a dummy signal handler to ask Python to write the signal
# number in the wakeup file descriptor. _process_self_data() will
# read signal numbers from this file descriptor to handle signals.
signal.signal(sig, _sighandler_noop)
old_handler = signal.signal(sig, _sighandler_noop)

if callable(old_handler):
# Store the previous handler as well so we can call it later.
self._old_handlers[sig] = old_handler

# Set SA_RESTART to limit EINTR occurrences.
signal.siginterrupt(sig, False)
Expand All @@ -140,6 +145,11 @@ def _handle_signal(self, sig):
else:
self._add_callback_signalsafe(handle)

old_handler = self._old_handlers.get(sig)

if old_handler:
old_handler(sig, (None, "sorry, no stack available, greetings from asyncio/unix_events"))

def remove_signal_handler(self, sig):
"""Remove a handler for a signal. UNIX only.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
asyncio: signal handlers registered by add_signal_handler() now call
previously registered handlers (if any).