Skip to content

Commit 4420da0

Browse files
gh-94732: Fix KeyboardInterrupt race in asyncio run_forever() (GH-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. (cherry picked from commit 3a49dbb) Co-authored-by: hetmankp <[email protected]>
1 parent fe99b64 commit 4420da0

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
@@ -595,12 +595,13 @@ def run_forever(self):
595595
self._check_closed()
596596
self._check_running()
597597
self._set_coroutine_origin_tracking(self._debug)
598-
self._thread_id = threading.get_ident()
599598

600599
old_agen_hooks = sys.get_asyncgen_hooks()
601-
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
602-
finalizer=self._asyncgen_finalizer_hook)
603600
try:
601+
self._thread_id = threading.get_ident()
602+
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
603+
finalizer=self._asyncgen_finalizer_hook)
604+
604605
events._set_running_loop(self)
605606
while True:
606607
self._run_once()

0 commit comments

Comments
 (0)