python-socketio
python-socketio copied to clipboard
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.
I don't have access to a Windows machine right now. Can you describe what happens when you press Ctrl-C? Does the client remain connected and continue to run as if nothing had happened? Does anything appear in the logs at that time?
Nothing appears in the log, app continues to run as if nothing happened. Only Ctrl+Break works to terminate.
Okay, I'll try to reproduce on a VM. Thanks.
On Windows, I use Conda envs + Pip and I've tried both cmd.exe and PowerShell with the same result, whereas on Linux I use the system installation of Python + Pip. If this is any helpful.
I believe this might be linked to the issue: https://stackoverflow.com/a/52941752/6912202
Thanks for doing the leg work on this, I was not aware of this difference. I'm not sure there is much I can do to improve this other than adding a SIGBREAK handler to exit cleanly with Ctrl-Brreak.
AFAIK you can either add the Ctrl+Break handler or register the CtrlHandler through Windows API as shown in the SO answer. Either way, way to cleanly end the script is desired since the disconnect handler is not triggered otherwise.
@asmarcz Can I ask you to install the master branch and test if Ctrl-Break interrupts the client gracefully? Believe it or not, I can't find a way to issue the Ctrl-Break key combination from my Mac keyboard, and for some reason the on-screen keyboard from Windows fails to do it too. Thanks!
@asmarcz I've decided to cut a release, because I have a number of other important fixes that I want to get out. If you find that this fix does not work, please reopen and I'll continue my investigation.
After updating python-engineio, the script becomes un-terminateable. Ctrl+C nor Ctrl+Break is able to stop it so I had to close the terminal.
I am afraid I cannot reopen the issue. Haven't found a way to do so.
Okay, I'll revert the fix, since clearly the previous solution is better.
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 ..
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()
Not planning to add custom interrupt handler for Windows at this time, given that it is possible to disable the UNIX-friendly handler that is provided and install a custom one as shown above.