Skip to content

Commit e6d30f8

Browse files
congzhangzhclaude
andcommitted
gh-145342: Add _guest_mode flag and guest-mode signal-handler guards
In guest mode the host event loop owns signal handling: add_signal_handler() and remove_signal_handler() raise RuntimeError so that asyncio never touches the process-global signal wakeup fd. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent fddc94f commit e6d30f8

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ def __init__(self):
432432
# Identifier of the thread running the event loop, or None if the
433433
# event loop is not running
434434
self._thread_id = None
435+
# True while the loop is driven as a guest inside a host event
436+
# loop (see asyncio.start_guest_run); signal handling is then
437+
# delegated to the host.
438+
self._guest_mode = False
435439
self._clock_resolution = time.get_clock_info('monotonic').resolution
436440
self._exception_handler = None
437441
self.set_debug(coroutines._is_debug_mode())

Lib/asyncio/unix_events.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ def add_signal_handler(self, sig, callback, *args):
100100
"with add_signal_handler()")
101101
self._check_signal(sig)
102102
self._check_closed()
103+
if self._guest_mode:
104+
raise RuntimeError(
105+
"add_signal_handler() is not supported in asyncio guest "
106+
"mode; the host event loop owns signal handling -- "
107+
"forward signals to the loop with call_soon_threadsafe()")
103108
try:
104109
# set_wakeup_fd() raises ValueError if this is not the
105110
# main thread. By calling it early we ensure that an
@@ -149,6 +154,10 @@ def remove_signal_handler(self, sig):
149154
Return True if a signal handler was removed, False if not.
150155
"""
151156
self._check_signal(sig)
157+
if self._guest_mode:
158+
raise RuntimeError(
159+
"remove_signal_handler() is not supported in asyncio "
160+
"guest mode; the host event loop owns signal handling")
152161
try:
153162
del self._signal_handlers[sig]
154163
except KeyError:

0 commit comments

Comments
 (0)