8000 gh-94732: Fix KeyboardInterrupt race in asyncio run_forever() (#97765) · python/cpython@3a49dbb · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a49dbb

Browse files
authored
gh-94732: Fix KeyboardInterrupt race in asyncio run_forever() (#97765)
Ensure that the event loop's `_thread_id` attribute and the asyncgen hooks set by `sys.set_asyncgen_hooks()` are always restored no matter where a KeyboardInterrupt exception is raised.
1 parent 873a2f2 commit 3a49dbb

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

Lib/asyncio/base_events.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,13 @@ def run_forever(self):
606606
self._check_closed()
607607
self._check_running()
608608
self._set_coroutine_origin_tracking(self._debug)
609-
self._thread_id = threading.get_ident()
610609

611610
old_agen_hooks = sys.get_asyncgen_hooks()
612-
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
613-
finalizer=self._asyncgen_finalizer_hook)
614611
try:
612+
self._thread_id = threading.get_ident()
613+
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
614+
finalizer=self._asyncgen_finalizer_hook)
615+
615616
events._set_running_loop(self)
616617
while True:
617618
self._run_once()

0 commit comments

Comments
 (0)
0