Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error trying to disconnect #568

Closed
numbereself opened this issue Nov 20, 2020 · 4 comments
Closed

Error trying to disconnect #568

numbereself opened this issue Nov 20, 2020 · 4 comments
Labels

Comments

@numbereself
Copy link

When i try calling the async disconnect function as a result of a packet i get an error stating "Cannot write to closing transport"

Here is the code I used:

import socketio
import asyncio

async def main():
  sio = socketio.AsyncClient(logger=True)

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

  @sio.event
  async def disconnect():
    print('connection closed')

  @sio.event
  async def welcome(data):
    await sio.emit("loaded", {
      "height": 1080,
      "width": 1920,
      "scale": 2,
      "success": 1
    })
    await sio.sleep(1.2)
    await sio.disconnect()

  await sio.connect("wss://eu2.adventure.land:2083")
  await sio.wait()

asyncio.run(main())

And here is the ouput:

PS C:\Users\Gary\Documents\alPaca> py .\test.py
Engine.IO connection established
Namespace / is connected
connection established
Received event "welcome" [/]
Emitting event "loaded" [/]
Received event "entities" [/]
Received event "entities" [/]
Received event "entities" [/]
Received event "entities" [/]
Received event "action" [/]
Received event "hit" [/]
Received event "death" [/]
Engine.IO connection dropped
Received event "entities" [/]
Traceback (most recent call last):
  File ".\test.py", line 29, in <module>
    asyncio.run(main())
  File "C:\Python38\lib\asyncio\runners.py", line 43, in run
  File "C:\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File ".\test.py", line 27, in main
    await sio.wait()
  File "C:\Python38\lib\site-packages\socketio\asyncio_client.py", line 128, in wait
    await self.eio.wait()
  File "C:\Python38\lib\site-packages\engineio\asyncio_client.py", line 88, in wait
    await self.read_loop_task
  File "C:\Python38\lib\site-packages\engineio\asyncio_client.py", line 526, in _read_loop_websocket
    await self.write_loop_task
  File "C:\Python38\lib\site-packages\engineio\asyncio_client.py", line 597, in _write_loop
    await self.ws.send_str(pkt.encode(
  File "C:\Python38\lib\site-packages\aiohttp\client_ws.py", line 150, in send_str
    await self._writer.send(data, binary=False, compress=compress)
  File "C:\Python38\lib\site-packages\aiohttp\http_websocket.py", line 687, in send
    await self._send_frame(message, WSMsgType.TEXT, compress)
  File "C:\Python38\lib\site-packages\aiohttp\http_websocket.py", line 598, in _send_frame
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
Traceback (most recent call last):
  File "C:\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Python38\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Python38\lib\asyncio\base_events.py", line 719, in call_soon
  File "C:\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed
PS C:\Users\Gary\Documents\alPaca> pip show python-engineio
Name: python-engineio
Version: 3.13.1
Summary: Engine.IO server
Home-page: http://github.com/miguelgrinberg/python-engineio/
Author: Miguel Grinberg
Author-email: miguelgrinberg50@gmail.com
License: MIT
Location: c:\python38\lib\site-packages
Requires: six
Required-by: python-socketio
PS C:\Users\Gary\Documents\alPaca> pip show python-socketio
Name: python-socketio
Version: 4.6.0
Summary: Socket.IO server
Home-page: http://github.com/miguelgrinberg/python-socketio/
Author: Miguel Grinberg
Author-email: miguelgrinberg50@gmail.com
License: MIT
Location: c:\python38\lib\site-packages
Requires: python-engineio, six
Required-by:

I am not exactly sure what this server in particular is running, but I am asuming it is NodeJS.
Also I feel like a disconnect event should fire regardless of the reason. No event fires however.
I was expecting to see the disconnect event fire at least. Also not seeing an exception is preferable.
Or is there an issue about using sleep here?

@miguelgrinberg
Copy link
Owner

I think there is a race condition here, where the client is in the process of closing down and yet the server has sent an event. I will try to reproduce this.

@miguelgrinberg miguelgrinberg self-assigned this Nov 20, 2020
@graham33
Copy link

I'm seeing a similar issue ("Cannot write to closing transport" exception when explicitly calling disconnect on an async client) using python-socket 4.6.0/python-engineio 3.13.2 with the latest aiohttp (3.7.3). I do not observe this with an earlier aiohttp (3.6.2).

Looking at the aiohttp release notes (https://docs.aiohttp.org/en/stable/changes.html#id32), it looks to be related to this change: aio-libs/aiohttp#4080. Should the disconnect function be changed to either handle this exception, or somehow cancel the underlying aiohttp web handler task to restore the previous behaviour?

Any pointers on the best way to work around this would be appreciated.Thanks!

@graham33
Copy link

I solved this by upgrading to the latest 4.x version of python-socketio and the latest 3.x version of python-engineio (4.6.1 and 3.14.2 respectively), so please disregard.

@miguelgrinberg
Copy link
Owner

@numbereself with the 4.6.1 release of python-socketio I do not get the error, but I do see the missing disconnect handler. Unfortunately the server that you are using has not been upgraded to the latest version of Socket.IO protocol, so I cannot test with the most current version of this package.

@miguelgrinberg miguelgrinberg removed their assignment Feb 22, 2021
Repository owner locked and limited conversation to collaborators Jun 27, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

3 participants