Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race with KeyboardInterrupt in asyncio loop run_forever() method #94732

Closed
hetmankp opened this issue Jul 11, 2022 · 2 comments
Closed

Race with KeyboardInterrupt in asyncio loop run_forever() method #94732

hetmankp opened this issue Jul 11, 2022 · 2 comments
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@hetmankp
Copy link
Contributor

There is a race with the KeyboardInterrupt exception in the asyncio.base_events.BaseEventLoop.run_forever() method. While an attempt is made to restore the environment to its previous state before run_forever() completes with a try...finally, a couple items find themselves outside the try...finally. Specifically, a badly timed KeyboardInterrupt will mean that, neither the self._thread_id value or the hooks set by sys.set_asyncgen_hooks() will be correctly restored. Both of these should be moved inside the try...finally.

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)

This applies to all existing version of Python on all platforms.

@hetmankp hetmankp added the type-bug An unexpected behavior, bug, or error label Jul 11, 2022
hetmankp added a commit to hetmankp/cpython that referenced this issue Jul 11, 2022
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.
@ezio-melotti ezio-melotti moved this to Todo in asyncio Sep 4, 2022
gvanrossum added a commit to hetmankp/cpython that referenced this issue Sep 14, 2022
hetmankp added a commit to hetmankp/cpython that referenced this issue Oct 3, 2022
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.
gvanrossum pushed a commit that referenced this issue Oct 3, 2022
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.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 3, 2022
…ythonGH-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]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 3, 2022
…ythonGH-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]>
miss-islington added a commit that referenced this issue Oct 3, 2022
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]>
miss-islington added a commit that referenced this issue Oct 3, 2022
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]>
@gvanrossum
Copy link
Member

Fixed!

@hetmankp: Thanks for being flexible regarding the CLA bot. I hope the problem is now fixed for you going forward?

Repository owner moved this from Todo to Done in asyncio Oct 3, 2022
carljm added a commit to carljm/cpython that referenced this issue Oct 3, 2022
* main: (2069 commits)
  pythongh-96512: Move int_max_str_digits setting to PyConfig (python#96944)
  pythongh-94808: Coverage: Check picklablability of calliter (python#95923)
  pythongh-94808: Add test coverage for PyObject_HasAttrString (python#96627)
  pythongh-94732: Fix KeyboardInterrupt race in asyncio run_forever() (python#97765)
  Fix typos in `bltinmodule.c`. (pythonGH-97766)
  pythongh-94808: `_PyLineTable_StartsLine` was not used (pythonGH-96609)
  pythongh-97681: Remove Tools/demo/ directory (python#97682)
  Fix typo in unittest docs (python#97742)
  pythongh-97728: Argument Clinic: Fix uninitialized variable in the Py_UNICODE converter (pythonGH-97729)
  pythongh-95913: Fix PEP number in PEP 678 What's New ref label (python#97739)
  pythongh-95913: Copyedit/improve New Modules What's New section (python#97721)
  pythongh-97740: Fix bang in Sphinx C domain ref target syntax (python#97741)
  pythongh-96819: multiprocessing.resource_tracker: check if length of pipe write <= 512 (python#96890)
  pythongh-97706: multiprocessing tests: Delete unused variable `rand` (python#97707)
  pythonGH-85447: Clarify docs about awaiting future multiple times (python#97738)
  [docs] Update logging cookbook with recipe for using a logger like an output… (pythonGH-97730)
  pythongh-97607: Fix content parsing in the impl-detail reST directive (python#97652)
  pythongh-95975: Move except/*/finally ref labels to more precise locations (python#95976)
  pythongh-97591: In `Exception.__setstate__()` acquire strong references before calling `tp_hash` slot (python#97700)
  pythongh-95588: Drop the safety claim from `ast.literal_eval` docs. (python#95919)
  ...
@hetmankp
Copy link
Contributor Author

hetmankp commented Oct 4, 2022

Fixed!

@hetmankp: Thanks for being flexible regarding the CLA bot. I hope the problem is now fixed for you going forward?

Thanks @gvanrossum , yes as far as CPython is concerned I should be covered (not sure why the simpler email format wasn't working but I guess it will remain a mystery for now).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants