question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyboardInterrupt ignored on Windows

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
char101commented, Jun 8, 2022

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()

1reaction
Madscientistecommented, Jul 28, 2021

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 …

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is KeyboardInterrupt not working in python?
You're running this code in an IDE like PyCharm. The IDE is taking ctrl+c (keyboardinterrupt) as copy. Try running your code in the...
Read more >
Issue 21822: [Windows] KeyboardInterrupt during Thread.join ...
Parting thoughts based on my limited expertise in the area, take them or ignore them: * Semaphore-based AllocNonRecursiveMutex (!_ ...
Read more >
Complete Guide to Python KeyboardInterrupt - eduCBA
Guide to Python KeyboardInterrupt. Here we discuss how KeyboardInterrupt exception work, how to avoid KeyboardInterrupt exceptions in Python.
Read more >
Control-C handling in Python and Trio - Nathaniel J. Smith
Option 1: KeyboardInterrupt ... The first option is to ignore the issue entirely. By default, the Python interpreter sets things up so that ......
Read more >
How to Kill a Python Thread - miguelgrinberg.com
At this point the main thread of the application raised the KeyboardInterrupt exception and wanted to exit, but the background thread did ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found