Skip to content
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

Websocket connect attempts to send message to closing connection #1933

Open
FabioRosado opened this issue Jun 19, 2022 · 2 comments
Open

Websocket connect attempts to send message to closing connection #1933

FabioRosado opened this issue Jun 19, 2022 · 2 comments
Labels

Comments

@FabioRosado
Copy link
Member

Description

While testing opsdroid-web it seems that sometimes the connection with opsdroid drops. If I try to reconnect from the opsdroid-web, the connection happens, but then when opsdroid tries to talk back to the socket, an error occurs:

DEBUG opsdroid.connector.websocket Responding with: 'Hey FabioRosado' in target fb091c98-efd1-11ec-9d61-fe44219afb74
ERROR opsdroid.core Exception when running skill 'hello'.
Traceback (most recent call last):
  File "/Users/fabiorosado/github/opsdroid/opsdroid/core.py", line 474, in run_skill
    return await skill(event)
  File "/Users/fabiorosado/Library/Application Support/opsdroid/opsdroid_modules/skill/hello/__init__.py", line 13, in hello
    await message.respond(text)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/events.py", line 285, in respond
    return await super().respond(response)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/events.py", line 147, in respond
    result = await opsdroid.send(event)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/core.py", line 671, in send
    return await event.connector.send(event)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/connector/__init__.py", line 220, in send
    return await self.events[type(event)](self, event)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/connector/websocket/__init__.py", line 187, in send_message
    await self.active_connections[message.target].send_str(message.text)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/web_ws.py", line 308, in send_str
    await self._writer.send(data, binary=False, compress=compress)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/http_websocket.py", line 690, in send
    await self._send_frame(message, WSMsgType.TEXT, compress)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/http_websocket.py", line 653, in _send_frame
    self._write(header + message)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/http_websocket.py", line 663, in _write
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport

I believe that the issue is happening on the following line:

message.target = next(iter(self.active_connections))

Opsdroid-web doesn't yet send the socket in which it's connected to opsdroid, so when we do next(iter(...)) we get the first socket which is closing.

Steps to Reproduce

Leave opsdroid running for a while, let your computer hibernate and try to connect to the websocket again send a basic message such as 'hello' and see the error

Expected Functionality

Opsdroid should remove closing/closed connections from self.active_connections currently we only remove sockets from self.active_connections when we are disconnecting the connector.

Experienced Functionality

Opsdroid tries to reply to a closing connection

DEBUG opsdroid.connector.websocket Responding with: 'Hey FabioRosado' in target fb091c98-efd1-11ec-9d61-fe44219afb74
ERROR opsdroid.core Exception when running skill 'hello'.
Traceback (most recent call last):
  File "/Users/fabiorosado/github/opsdroid/opsdroid/core.py", line 474, in run_skill
    return await skill(event)
  File "/Users/fabiorosado/Library/Application Support/opsdroid/opsdroid_modules/skill/hello/__init__.py", line 13, in hello
    await message.respond(text)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/events.py", line 285, in respond
    return await super().respond(response)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/events.py", line 147, in respond
    result = await opsdroid.send(event)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/core.py", line 671, in send
    return await event.connector.send(event)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/connector/__init__.py", line 220, in send
    return await self.events[type(event)](self, event)
  File "/Users/fabiorosado/github/opsdroid/opsdroid/connector/websocket/__init__.py", line 187, in send_message
    await self.active_connections[message.target].send_str(message.text)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/web_ws.py", line 308, in send_str
    await self._writer.send(data, binary=False, compress=compress)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/http_websocket.py", line 690, in send
    await self._send_frame(message, WSMsgType.TEXT, compress)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/http_websocket.py", line 653, in _send_frame
    self._write(header + message)
  File "/opt/homebrew/lib/python3.9/site-packages/aiohttp/http_websocket.py", line 663, in _write
    raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport

Versions

  • Opsdroid version: main
  • Python version: 3.10
  • OS/Docker version: macOS Monterey

Configuration File

Please include your version of the configuration file below.

  • hello skill
  • websocket connection

Additional Details

Any other details you wish to include such as screenshots, console messages, etc.

@FabioRosado FabioRosado changed the title Websocket connect might not close properly Websocket connect attempts to send message to closing connection Jun 19, 2022
@cshel16
Copy link

cshel16 commented Nov 10, 2022

Hi, I'd like to work on this!

@FabioRosado
Copy link
Member Author

Hi, please go ahead 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants