Skip to content

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

Description

@hetmankp

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.

Activity

  1. moved this to Todo in asyncioon Sep 4, 2022
  2. added a commit that references this issue on Oct 3, 2022
  3. added a commit that references this issue on Oct 3, 2022
  4. added 4 commits that reference this issue on Oct 3, 2022
  5. gvanrossum commented on Oct 3, 2022

    @gvanrossum
    Member

    Fixed!

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

  6. Repository owner moved this from Todo to Done in asyncioon Oct 3, 2022
  7. added a commit that references this issue on Oct 3, 2022
  8. hetmankp commented on Oct 4, 2022

    @hetmankp
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions