Skip to content

Consider making Windows select.select interruptible using WSAEventSelect & WSAWaitForMultipleEvents #79115

Open

Description

@ondrejkutal
BPO 34934
Nosy @pfmoore, @giampaolo, @tjguk, @zware, @zooba

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2018-10-08.14:32:57.564>
labels = ['extension-modules', 'type-feature', '3.8', 'OS-windows']
title = 'Consider making Windows select.select interruptible using WSAEventSelect & WSAWaitForMultipleEvents'
updated_at = <Date 2018-10-09.16:13:33.120>
user = 'https://bugs.python.org/ondrejkutal'

bugs.python.org fields:

activity = <Date 2018-10-09.16:13:33.120>
actor = 'giampaolo.rodola'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Extension Modules', 'Windows']
creation = <Date 2018-10-08.14:32:57.564>
creator = 'ondrej.kutal'
dependencies = []
files = []
hgrepos = []
issue_num = 34934
keywords = []
message_count = 1.0
messages = ['327354']
nosy_count = 6.0
nosy_names = ['paul.moore', 'giampaolo.rodola', 'tim.golden', 'zach.ware', 'steve.dower', 'ondrej.kutal']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue34934'
versions = ['Python 3.8']

Activity

  1. ondrejkutal commented on Oct 8, 2018

    ondrejkutalmannequin
    MannequinAuthor

    At the moment, socket select.select() function is not interruptable on Windows OS (in main thread). Following code cannot be interrupted (for example by CTRL+C):

    import select, socket
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setblocking(False)
    s.bind(('0.0.0.0', 6666))
    s.listen(100)
    
    select.select([s], [], [], None)
    s.close()

    However this can be achieved by replacing select() calls with use of Windows native APIs WSAEventSelect and WSAWaitForMultipleEvents (see for example https://stackoverflow.com/questions/10353017). I have tried a quick prototype in selectmodule.c, replacing

    Py_BEGIN_ALLOW_THREADS
    errno = 0;
    n = select(max, &ifdset, &ofdset, &efdset, tvp);
    Py_END_ALLOW_THREADS

    with

    #ifndef MS_WINDOWS
            Py_BEGIN_ALLOW_THREADS
            errno = 0;
            n = select(max, &ifdset, &ofdset, &efdset, tvp);
            Py_END_ALLOW_THREADS
    #else
    
            if (!_PyOS_IsMainThread()) {
                Py_BEGIN_ALLOW_THREADS
                errno = 0;
                n = select(max, &ifdset, &ofdset, &efdset, tvp);
                Py_END_ALLOW_THREADS
            }
            else
            {
                // quick prototype, only for read sockets
                WSAEVENT events[50];
                for (u_int i = 0; i < ifdset.fd_count; ++i)
                {
                    events[i+1] = WSACreateEvent();
                    WSAEventSelect(ifdset.fd_array[i], events[i+1], FD_ACCEPT | FD_READ); 
                }
            /* putting interrupt event as a first one in the list
            */
            events[0] = _PyOS_SigintEvent();
            ResetEvent(events[0]);
    
            Py_BEGIN_ALLOW_THREADS
            errno = 0;
            n = WSAWaitForMultipleEvents(ifdset.fd_count, events, FALSE, tvp ? (DWORD)_PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING) : WSA_INFINITE, FALSE);
            Py_END_ALLOW_THREADS
    
            if (n == 0) 
                errno = EINTR;
            else if (n == WSA_WAIT_FAILED)
                n = SOCKET_ERROR;
            else
                n = 1; /* prototype implementation, acting like just 1 socket is ready,
                       for actual number it will be probably necessary to query WSAWaitForMultipleEvents 
                       multiple times since it otherwise returns only index of first ready event...
                       \*/
        }
    

    #endif

    and then I was able to interrupt the script above. I noticed slight performance loss when having timeout 0, repeating select 1000 times it took ~2000 us, wile after this update it took ~3000 us.

    I am just throwing it here to consider as a possibility. Clearly my code above is just proof of concept, modification would be needed (include write fd, some proper fd limit, possibly check multiple times to get actual number of ready fds, etc...).

  2. added
    interpreter-core
    3.8 (EOL)
    type-feature
    on Oct 8, 2018
  3. changed the title [-]Consider making Windows select.select interruptable using WSAEventSelect & WSAWaitForMultipleEvents[/-] [+]Consider making Windows select.select interruptible using WSAEventSelect & WSAWaitForMultipleEvents[/+] on Oct 8, 2018
  4. added
    extension-modules
    and removed
    interpreter-core
    on Oct 9, 2018
  5. transferred this issue fromon Apr 10, 2022
  6. Chronial commented on Dec 13, 2022

    @Chronial

    Related / duplicate: #85609

  7. added
    topic-socket
    on Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.8 (EOL)OS-windowsextension-modulestopic-sockettype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions