Title: asyncio.ProactorEventLoop mishandles signal wakeup file descriptor
Type: behavior Stage:
Components: asyncio, Windows Versions: Python 3.8
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


The first asyncio.ProactorEventLoop instance sets a signal wakeup file descriptor on construction (see 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 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.
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