Skip to content

Commit

Permalink
pythongh-94732: Fix KeyboardInterrupt race in asyncio run_forever() (p…
Browse files Browse the repository at this point in the history
…ython#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.
  • Loading branch information
hetmankp authored Oct 3, 2022
1 parent 873a2f2 commit 3a49dbb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,13 @@ def run_forever(self):
self._check_closed()
self._check_running()
self._set_coroutine_origin_tracking(self._debug)
self._thread_id = threading.get_ident()

old_agen_hooks = sys.get_asyncgen_hooks()
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
finalizer=self._asyncgen_finalizer_hook)
try:
self._thread_id = threading.get_ident()
sys.set_asyncgen_hooks(firstiter=self._asyncgen_firstiter_hook,
finalizer=self._asyncgen_finalizer_hook)

events._set_running_loop(self)
while True:
self._run_once()
Expand Down

0 comments on commit 3a49dbb

Please sign in to comment.