bpo-23057: add loop self socket as wakeup fd for signals by vladima · Pull Request #11135 · python/cpython (original) (raw)

Yes, I'll take a look at CI failures during the weekend - looks odd since all tests were passing locally.
Also after thinking about this issue a bit more I'm inclined to make implementation a bit different. Currently because of wakeup fd being set for a signal - receiving ctrl-c unblocks the proactor and allows signal processing logic in the interpreter loop to kick in and break out of run_forever. At glance it looks exactly what we need however the downside of this - it leaves event loop in an inconsistent state since we have no idea at what point KeyboardInterrupt was raised. For example:

>>> import asyncio
>>> l = asyncio.get_event_loop() # ProactorEventLoop()
>>> l.run_forever() # at this point everything is stuck
# press ctrl-c to break out of `run_forever` - works
>>> l.run_forever() # run it again using the same loop
# press ctrl-c again - now it does not work because proactor does not receive anything from the self socket

I'm not sure how important is this use-case and what are general expectations for the internal state of event loop after breaking out of run_forever but first impression of this behavior - "it does not look right". I think a better option would be instead of relying on default handler of SIGINT we need to set custom handler that will record the fact of receiving ctrl-c and raise KeyboardInterrupt at some well defined point.