classification
Title: asyncio.ProactorEventLoop mishandles signal wakeup file descriptor
Type: behavior Stage:
Components: asyncio, Windows Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, gvanrossum, hidmic, paul.moore, steve.dower, tim.golden, yselivanov, zach.ware
Priority: normal Keywords:

Created on 2021-01-12 21:43 by hidmic, last changed 2021-01-18 18:09 by gvanrossum.

Messages (4)
msg384979 - (view) Author: Michel Hidalgo (hidmic) Date: 2021-01-12 21:43
asyncio.ProactorEventLoop uses a socket.socketpair and signal.set_wakeup_fd to wake up a loop that's polling I/O. However it does so with no consideration for file descriptors previously set (i.e. no signal number forwarding). Either by user code or by another instance of asyncio.ProactorEventLoop.

The following snippet is enough for the above to cause the loop to hang forever:

import asyncio
import gc

asyncio.set_event_loop(asyncio.ProactorEventLoop())
asyncio.set_event_loop(asyncio.ProactorEventLoop())
gc.collect()
asyncio.get_event_loop().run_forever()


The first asyncio.ProactorEventLoop instance sets a signal wakeup file descriptor on construction (see https://github.com/python/cpython/blob/187f76def8a5bd0af7ab512575cad30cfe624b05/Lib/asyncio/proactor_events.py#L632). The second instances does the same, dropping the file descriptor set by the first one (not good, not necessarily bad). When the garbage collector purges the first instance, signal wakeups are disabled completely (see https://github.com/python/cpython/blob/187f76def8a5bd0af7ab512575cad30cfe624b05/Lib/asyncio/proactor_events.py#L679). The loop cannot be interrupted with Ctrl+C anymore (bad).
msg384998 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-01-13 06:26
Looks this was not tested or thought through with multiple loops and signals (admittedly, using signals is never fun, and even less so on Windows).

Can you send a PR with a fix?
msg385198 - (view) Author: Michel Hidalgo (hidmic) Date: 2021-01-18 14:06
Sorry for taking so long to reply. 

Sure, I would be happy to contribute. We should probably take care of Unix event loops -- since I opened this ticket I found out those tend to not cooperate with other signal wakeup file descriptor users either. I am a bit short on time right now though, so it will take some time.
msg385211 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-01-18 18:09
No pressure. If there's an API change needed you will have until 3.10 beta 1. Without API changes this can be fixed any time.
History
Date User Action Args
2021-01-18 18:09:15gvanrossumsetmessages: + msg385211
2021-01-18 14:07:00hidmicsetmessages: + msg385198
2021-01-13 06:26:33gvanrossumsetnosy: + gvanrossum
messages: + msg384998
2021-01-12 21:43:53hidmiccreate