python-socketio: KeyboardInterrupt ignored on Windows

Hello, with the sample below after hitting Ctrl+C, the script exits on Linux but nothing happens on Windows.

Linux
python 3.8.6
python-engineio 3.13.2
python-socketio 4.6.0

Windows
python 3.8.5/3.9.0 neither is working
python-engineio 3.13.2
python-socketio 4.6.0
import socketio

sio = socketio.Client(logger=True, engineio_logger=True)
nspc = "/example"

@sio.event(namespace=nspc)
def connect():
   print("Connected.")

@sio.event(namespace=nspc)
def disconnect():
   print("Disconnected.")

sio.connect("https://example.com", namespaces=[nspc], transports="websocket")
sio.wait()

The connect event handler gets invoked, the PING/PONG starts and I attempt Ctrl+C with no success on Windows.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Example code to handle Ctrl+c using win32api.SetConsoleCtrlHandler (install pywin32 first or use ctypes).

from engineio.client import signal_handler
from win32api import SetConsoleCtrlHandler

import socketio

sio = socketio.Client()


@sio.event
def connect():
    print('connection established')


@sio.event
def disconnect():
    print('disconnected from server')


def handler(event):
    import inspect
    import signal
    if event == 0:
        try:
            signal_handler(signal.SIGINT, inspect.currentframe())
        except:
            # SetConsoleCtrlHandler handle cannot raise exceptions
            pass


if __name__ == '__main__':
    SetConsoleCtrlHandler(handler, 1)
    sio.connect('http://localhost:5000')
    sio.wait()

Hello, i have the same problem

I currently have a NodeJS server, with python as client when i connect to the server i can’t close the connection which make me close the whole terminal and re-open with venv again …

Okay, I’ll revert the fix, since clearly the previous solution is better.