websockets: Websocket Server: Closing handshake failed in v10, v9.1 was ok
Hello Augustin, from version 10 occurs an error during closing browser tab connected to my websocket server. It occurs only in approximately 30% cases. But everything was ok in version 9.1.
Tested in Google Chrome 94.
Traceback:
closing handshake failed
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/websockets/legacy/server.py", line 232, in handler
await self.close()
File "/usr/local/lib/python3.9/site-packages/websockets/legacy/protocol.py", line 779, in close
await asyncio.shield(self.close_connection_task)
File "/usr/local/lib/python3.9/site-packages/websockets/legacy/protocol.py", line 1309, in close_connection
self.transport.write_eof()
File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 972, in write_eof
self._sock.shutdown(socket.SHUT_WR)
OSError: [Errno 107] Socket not connected
Websocket server simplified code:
import asyncio
import signal
import websockets
import sys
class Chat(object):
async def process(self, websocket: websockets.WebSocketServerProtocol, path: str):
async for event in websocket:
print(event)
sys.stdout.flush()
__call__ = process
async def server(stop):
async with websockets.serve(Chat(), "0.0.0.0", 8765):
await stop
if __name__ == "__main__":
loop = asyncio.get_event_loop()
stop = loop.create_future()
loop.add_signal_handler(signal.SIGTERM, stop.set_result, None)
loop.run_until_complete(server(stop))
Can you help me, please?
Thanks a lot, Jaromir
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 22 (9 by maintainers)
Commits related to this issue
- Avoid shutdown on closed socket. Fix #1072. — committed to python-websockets/websockets by aaugustin 3 years ago
- Avoid shutdown on closed socket. Fix #1072. — committed to python-websockets/websockets by aaugustin 3 years ago
- Avoid shutdown on closed socket. Fix #1072. — committed to python-websockets/websockets by aaugustin 3 years ago
- Avoid OSError: [Errno 107] noise in logs. Fix #1117. Ref #1072. — committed to python-websockets/websockets by aaugustin 2 years ago
- Catch RuntimeError to cater to uvloop. Fix #1138, #1072 (again). — committed to python-websockets/websockets by aaugustin 2 years ago
I will fix it. Really sorry š¦
Version 10.1, which includes the fix, is available on PyPI. Thanks everyone for your help!
Thanks a lot for your help! Iāll commit the fix shortly.
For some more background, this issue occurred for me when upgrading
uvloop
from0.15.3
to0.16.0
andwebsockets
from9.1
to10.0
. Iāve tested downgrading each of the packages independently and can confirm that the error is caused by the upgrade towebsockets==10.0
.I also ran into this problem with
websockets
10.0 in combination withuvloop
and I can confirm that the change in https://github.com/aaugustin/websockets/issues/1072#issuecomment-955159232 fixes the problem on Debian Buster (and also thatwebsockets
9.1 didnāt have the issue). Hopefully this fix can be merged and released since I now had to downgrade to version 9.1 for the time being. Thank you for your efforts!Iām not very familiar with the bit of code that Iām looking into right now so not sure how much help I can be.
In my example above I triggered an exception:
So it looks like weāre seeing the following because the connection is terminated abnormally:
Not sure if that is helpful. I was able to trigger this behaviour easily with the following where the path isnāt valid:
@aaugustin I can confirm that your patch above fixes the issue.
Before
After
Versions
Iāll try to test it.
The error indeed happens at line 1308 in protocol.py
Half-close the TCP connection if possible (when thereās no TLS).
On Wed, Nov 3, 2021 at 9:59 PM Aymeric Augustin @.***> wrote:
I tried to reproduce this error but I didnāt manage. I didnāt find any changes in this area between 9.1 and 10.0 either. So I have no idea why itās happening in 10.0 but didnāt happen before š¦
This change could help. Would someone who hits the issue be able to test if it helps, please?