Handle abrupt WebSocket disconnect (e.g. Safari tab close)

Catch ConnectionClosed in User.send() and handler_recv() so that an
unclean client disconnect no longer produces "Task exception was never
retrieved" / "connection handler failed" log noise.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SH2D4LRuCnhLSzoYerAfJX
This commit is contained in:
2026-06-28 15:34:53 +02:00
co-authored by Claude Sonnet 4.6
parent d44601fee1
commit 0f31cc41cc
2 changed files with 17 additions and 9 deletions
+4
View File
@@ -1,4 +1,5 @@
import asyncio
import websockets.exceptions
from ws.server.ws_server import WsServer
from ws.user import User, UserSet, update
from ws.connection import IConnection
@@ -31,6 +32,7 @@ class WsServerMultiUser(WsServer):
await self.notify_users()
async def handler_recv(self, websocket, path):
try:
while True:
data = await websocket.recv()
usr = self.USERS.get(websocket)
@@ -39,6 +41,8 @@ class WsServerMultiUser(WsServer):
await usr.send(self.global_state)
else:
await self.listener.on_recv(data)
except websockets.exceptions.ConnectionClosed:
pass
async def handler_send(self, websocket, path):
while True:
+4
View File
@@ -1,5 +1,6 @@
import dpath.util as dp
import json
import websockets.exceptions
def update(d, u, only_existing=True):
@@ -26,7 +27,10 @@ class User:
msg = dp.search(data, path)
result.update(msg)
if len(result) != 0:
try:
await self.socket.send(json.dumps(result))
except websockets.exceptions.ConnectionClosed:
pass
async def process(self, data):
handled = True