Skip to content

How to terminate a disconnected client? #301

@christophevg

Description

@christophevg

Hi,

I'm having trouble cleanly terminating a disconnected client using an interrupt (ctrl+c).
Given this example:

import socketio
import time

sio = socketio.Client()

@sio.on("connect")
def on_connect():
  print("connected")

@sio.on("disconnect")
def on_disconnect():
  print("disconnected")

sio.connect("http://localhost:8000")

try:
  # sio.wait() # doesn't raise it, so have to implement it manually ;-)
  while True:
    time.sleep(1)
except KeyboardInterrupt:
  print("handling interrupt...")
  sio.disconnect()

print("done")

All goes well when issuing the interrupt when connected:

$ python client.py
connected
^Cdisconnected
handling interrupt...
done
$

But when performing the same procedure, after the client was disconnected from the server (by the server), the same interrupt handling code isn't able to terminate the client cleanly, and the process continues, not returning to the console:

$ python client.py
connected

# stopping server now, after initial connected

disconnected
^Chandling interrupt...
done

The only way to terminate the process now is ...

^Z
[1]+  Stopped                 python client.py
$ kill %1

[1]+  Terminated: 15          python client.py

Is there a proper way to terminate a client, when it's in a disconnected state?

regards,
Christophe VG

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions