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.

SIGINT handler does not disconnect client

See original GitHub issue

When attempting to disconnect client, it only disconnects once, but then reconnects.

Connection log:

DEBUG:asyncio:Using selector: KqueueSelector
INFO:engineio.client:Attempting WebSocket connection to wss://myproject.com/api/ws/?transport=websocket&EIO=3
INFO:engineio.client:WebSocket connection accepted with {'sid': '7a2f537ad5cc49e79c5f05fa3644c923', 'upgrades': [], 'pingTimeout': 60000, 'pingInterval': 25000}
INFO:socketio.client:Engine.IO connection established
INFO:engineio.client:Sending packet PING data None
INFO:engineio.client:Received packet MESSAGE data 0
INFO:socketio.client:Namespace / is connected
INFO:engineio.client:Received packet PONG data None

After sending SIGINT:

INFO:engineio.client:Sending packet PING data None
ERROR:engineio.client:packet queue is empty, aborting
INFO:engineio.client:Exiting write loop task
INFO:engineio.client:Unexpected error receiving packet: "", aborting
INFO:engineio.client:Waiting for write loop task to end
INFO:engineio.client:Waiting for ping loop task to end
INFO:engineio.client:PONG response has not been received, aborting
INFO:engineio.client:Exiting ping task
INFO:socketio.client:Engine.IO connection dropped
WARNING:project:Disconnected
INFO:engineio.client:Exiting read loop task
INFO:socketio.client:Connection failed, new attempt in 0.91 seconds
INFO:engineio.client:Attempting WebSocket connection to wss://myproject.com/api/ws/?transport=websocket&EIO=3
INFO:engineio.client:WebSocket connection accepted with {'sid': '3c5ebff507024dadbb293e9d1adfcad7', 'upgrades': [], 'pingTimeout': 60000, 'pingInterval': 25000}
INFO:socketio.client:Engine.IO connection established
INFO:socketio.client:Reconnection successful
INFO:engineio.client:Sending packet PING data None
INFO:engineio.client:Received packet MESSAGE data 0
INFO:socketio.client:Namespace / is connected
INFO:engineio.client:Received packet PONG data None

After another SIGINT:

^CERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<AsyncClient.disconnect() running at /Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/engineio/asyncio_client.py:106>>
/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py:604: RuntimeWarning: coroutine 'AsyncClient.disconnect' was never awaited
  self._ready.clear()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 566, in run_until_complete
    self.run_forever()
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 1735, in _run_once
    event_list = self._selector.select(timeout)
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/selectors.py", line 558, in select
    kev_list = self._selector.control(None, max_ev, timeout)
  File "/Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/engineio/client.py", line 43, in signal_handler
    return original_signal_handler(sig, frame)
  File "/Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/socketio/client.py", line 26, in signal_handler
    return original_signal_handler(sig, frame)
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/asnelzin/dev/project/project/__main__.py", line 93, in <module>
    asyncio.run(main(sys.argv[1:]))
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/runners.py", line 46, in run
    _cancel_all_tasks(loop)
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/runners.py", line 62, in _cancel_all_tasks
    tasks.gather(*to_cancel, loop=loop, return_exceptions=True))
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 566, in run_until_complete
    self.run_forever()
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 534, in run_forever
    self._run_once()
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 1735, in _run_once
    event_list = self._selector.select(timeout)
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/selectors.py", line 558, in select
    kev_list = self._selector.control(None, max_ev, timeout)
  File "/Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/engineio/client.py", line 43, in signal_handler
    return original_signal_handler(sig, frame)
  File "/Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/socketio/client.py", line 26, in signal_handler
    return original_signal_handler(sig, frame)
KeyboardInterrupt
ERROR:asyncio:Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10c7e0310>
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<AsyncClient._ping_loop() running at /Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/engineio/asyncio_client.py:446> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10c7ed790>()]>>
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<AsyncClient._write_loop() running at /Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/engineio/asyncio_client.py:551> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10c7eda50>()]>>
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<AsyncClient._read_loop_websocket() running at /Users/asnelzin/dev/project/venv/lib/python3.7/site-packages/engineio/asyncio_client.py:500> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10c0af5d0>()]> cb=[<TaskWakeupMethWrapper object at 0x10c7ed9d0>()]>
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<Queue.get() running at /Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/queues.py:159> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10c7ed2d0>()]> cb=[_release_waiter(<Future pendi...10c7eda50>()]>)() at /Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/tasks.py:392]>
Exception ignored in: <coroutine object Queue.get at 0x10c806710>
Traceback (most recent call last):
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/queues.py", line 161, in get
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 683, in call_soon
  File "/Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/base_events.py", line 475, in _check_closed
RuntimeError: Event loop is closed
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<Event.wait() running at /Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/locks.py:293> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10c7ed7d0>()]> cb=[_release_waiter(<Future pendi...10c7ed790>()]>)() at /Users/asnelzin/.pyenv/versions/3.7.4/lib/python3.7/asyncio/tasks.py:392]>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
miguelgrinbergcommented, Feb 21, 2021

@LeonardoRick the order in which things are destroyed/stopped during shutdown of the Python process is really unpredictable, so it is not possible to have a reliable shutdown procedure so late in the life of the process. Here is an alternative that works well:

import socketio
import atexit
import sys

class SocketIOTest:
    def __init__(self):
        self.sio = socketio.Client()

        self.sio.connect('http://localhost:5000')
        self.sio.emit('init', 'message')

        @self.sio.event
        def connect():
            print("I'm connected!")

        @self.sio.event
        def connect_error():
            print("The connection failed!")

        @self.sio.event
        def disconnect():
            print("I'm disconnected!")

    def finish(self):
        self.sio.sleep(2)
        self.sio.disconnect()
        self.sio.wait()

s = SocketIOTest()
print('exiting...')
s.finish()
sys.exit(0)
1reaction
miguelgrinbergcommented, Feb 22, 2021

@LeonardoRick with the standard client it is very difficult to do an orderly exit inside atexit. Asyncio termination is cleaner, for that reason I believe it works better for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

signal handler ending the process after its execution
I use socket_rcv() to receive data from the server socket and also to send a SIGINT to the process if the read() return...
Read more >
Handling SIGINT signal for a server program
If the program was ignoring the signal, it continues to do so. Otherwise, it sets its own handler to handle the signal.
Read more >
Termination Signals (The GNU C Library)
The SIGHUP (“hang-up”) signal is used to report that the user's terminal is disconnected, perhaps because a network or telephone connection was broken....
Read more >
LINUX Signals
SIGINT is a signal generated when a user presses Control-C. This will terminate the program from the terminal. SIGALRM is generated when the...
Read more >
Unix / Linux - Signals and Traps - Tutorialspoint
Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the...
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