diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index a55b3a375fa22d7..dfdbb3aff89859c 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -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() @@ -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) @@ -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. diff --git a/Misc/NEWS.d/next/Library/2021-05-22-01-12-14.bpo-44212.RXiAsf.rst b/Misc/NEWS.d/next/Library/2021-05-22-01-12-14.bpo-44212.RXiAsf.rst new file mode 100644 index 000000000000000..1883b6f9ebcf636 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-22-01-12-14.bpo-44212.RXiAsf.rst @@ -0,0 +1,2 @@ +asyncio: signal handlers registered by add_signal_handler() now call +previously registered handlers (if any).