Skip to content

Commit 5fa35ec

Browse files
committed
pythongh-94732: Fix KeyboardInterrupt race in asyncio run_forever()
This addresses issue python#94732, ensuring 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 c9118af commit 5fa35ec

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
@@ -593,12 +593,13 @@ def run_forever(self):
593593
self._check_closed()
594594
self._check_running()
595595
self._set_coroutine_origin_tracking(self._debug)
596-
self._thread_id = threading.get_ident()
597596

598597
old_agen_hooks = sys.get_asyncgen_hooks()
599-
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
600-
finalizer=self._asyncgen_finalizer_hook)
601598
try:
599+
self._thread_id = threading.get_ident()
600+
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
601+
finalizer=self._asyncgen_finalizer_hook)
602+
602603
events._set_running_loop(self)
603604
while True:
604605
self._run_once()

0 commit comments

Comments
 (0)