[issue39622] KeyboardInterrupt is ignored when await asyncio.sleep(0)

Maor Kleinberger report at bugs.python.org
Tue Feb 25 17:35:29 EST 2020


Maor Kleinberger <kmaork at gmail.com> added the comment:

Damn, good catch. How about the following idea - register a normal signal handler (using signal.signal) that does something like:

def sigint_handler():
    get_running_loop().interrupt()

# in class BaseEventLoop
def interrupt(self):
    # Might be a generally useful thread-safe way to interrupt a loop
    if self._is_inside_callback():
        _thread.interrupt_main() # All this behavior is only relevant to the main thread anyway
    else:
        self._interrupted = True

And inside BaseEventLoop._run_once() add the following check:

# in class BaseEventLoop
def _check_interrupted(self):
    # This will be called by BaseEventLoop._run_once() before calling select,
    # and before popping any handle from the ready queue
    if self._interrupted:
        raise KeyboardInterrupt

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39622>
_______________________________________


More information about the Python-bugs-list mailing list