diff options
| author | Michel Hidalgo <michel@ekumenlabs.com> | 2022-09-17 15:07:54 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-17 15:07:54 (GMT) |
| commit | 05878106989c6f5b9dd35a6c15a21bee59312827 (patch) | |
| tree | 8d58f02a0cc3e14d4983106033a5d97e4cd49bf1 /Lib/asyncio/unix_events.py | |
| parent | 2cd70ffb3fe22d778d0bb6ec220fdf67dccc1be6 (diff) | |
| download | cpython-05878106989c6f5b9dd35a6c15a21bee59312827.zip cpython-05878106989c6f5b9dd35a6c15a21bee59312827.tar.gz cpython-05878106989c6f5b9dd35a6c15a21bee59312827.tar.bz2 | |
gh-87079: Warn on unintended signal wakeup fd override in `asyncio` (#96807)
Warn on loop initialization, when setting the wakeup fd disturbs a previously set wakeup fd, and on loop closing, when upon resetting the wakeup fd, we find it has been changed by someone else.
Diffstat (limited to 'Lib/asyncio/unix_events.py')
| -rw-r--r-- | Lib/asyncio/unix_events.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index cf7683f..6c9a89d 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -65,7 +65,9 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): self._signal_handlers = {} def close(self): - super().close() + # remove signal handlers first to verify + # the loop's signal handling setup has not + # been tampered with if not sys.is_finalizing(): for sig in list(self._signal_handlers): self.remove_signal_handler(sig) @@ -77,6 +79,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): ResourceWarning, source=self) self._signal_handlers.clear() + super().close() def _process_self_data(self, data): for signum in data: @@ -102,7 +105,12 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): # main thread. By calling it early we ensure that an # event loop running in another thread cannot add a signal # handler. - signal.set_wakeup_fd(self._csock.fileno()) + oldfd = signal.set_wakeup_fd(self._csock.fileno()) + if oldfd != -1 and oldfd != self._csock.fileno(): + warnings.warn( + "Signal wakeup fd was already set", + ResourceWarning, + source=self) except (ValueError, OSError) as exc: raise RuntimeError(str(exc)) @@ -166,7 +174,12 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop): if not self._signal_handlers: try: - signal.set_wakeup_fd(-1) + oldfd = signal.set_wakeup_fd(-1) + if oldfd != -1 and oldfd != self._csock.fileno(): + warnings.warn( + "Got unexpected signal wakeup fd", + ResourceWarning, + source=self) except (ValueError, OSError) as exc: logger.info('set_wakeup_fd(-1) failed: %s', exc) |
