[New-bugs-announce] [issue22018] signal: accept socket for signal.set_wakeup_fd()

STINNER Victor report at bugs.python.org
Sun Jul 20 21:47:13 CEST 2014

New submission from STINNER Victor:


I'm working on asyncio, someone asked why asyncio cannot be interrupted by CTRL+c (SIGINT):

On Windows, select.select() is not interrupted by CTRL+c. To get a reliable behaviour, interrupt select.select() on a signal, we can use signal.set_wakeup_fd(). New problem: on Windows, select.select() only supports sockets.

I propose to modify signal.set_wakeup_fd() to not only support files (use write), but also sockets (use send). Attached patch implements that.

This issue is part of a global PEP to modify how Python handles EINTR. I'm writing a PEP on that with Charles-François Natali. The idea to retry on EINTR in some cases, like write(), so we need a way to wakeup the code on a signal, on all platforms.


- I had to modify the pythoncore Visual Studio project to add a dependency to the WinSock library ("ws2_32.lib"). Is it a bad thing? We may split the _signal module into a new project, to only put the dependency there. What do you think? I'm not sure that it makes sense because the _signal module is always imported at Python startup, by initsigs() (search for the call to PyOS_InitInterrupts()).

- PySignal_SetWakeupFd() returns the old file descriptor, which is -1 by default. The API is not written to report errors. I chose to return -2 and clear the Python exception. Should we add a new function raising a Python exception on error? Ex: "int PySignal_SetWakeupFdWithError(int fd, int *old_fd)" returns 0 on success, -1 on error.

+        /* Import the _socket module to call WSAStartup() */
+        mod = PyImport_ImportModuleNoBlock("_socket");

I chose to import the _socket module because calling WSAStartup() requires also to call WSACleanup() at exit. I don't want to have two modules responsible for that.

I'm using "getsockopt(fd, SOL_SOCKET, SO_ERROR, ...)" to check if fd is a socket. Is there a better function to check if a file descriptor or handle is a socket?

According to the documentation, "GetFileType(handle) == FILE_TYPE_PIPE" is true for sockets, but also for (named and anonymous) pipes.

components: Windows, asyncio
files: signal_socket.patch
keywords: patch
messages: 223532
nosy: gvanrossum, haypo, loewis, neologix, pitrou, yselivanov
priority: normal
severity: normal
status: open
title: signal: accept socket for signal.set_wakeup_fd()
versions: Python 3.5
Added file: http://bugs.python.org/file36006/signal_socket.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list