darabos commited on
Commit
d2fc137
·
1 Parent(s): a55eb17

Possibly fix backend websocket exceptions.

Browse files
Files changed (1) hide show
  1. server/crdt.py +18 -11
server/crdt.py CHANGED
@@ -7,15 +7,20 @@ import fastapi
7
  import os.path
8
  import pycrdt
9
  import pycrdt_websocket
10
-
11
  import pycrdt_websocket.ystore
 
 
12
 
13
  router = fastapi.APIRouter()
14
 
15
 
16
  def ws_exception_handler(exception, log):
17
- print("exception", exception)
18
- log.exception(exception)
 
 
 
 
19
  return True
20
 
21
 
@@ -39,7 +44,9 @@ class WebsocketServer(pycrdt_websocket.WebsocketServer):
39
  if "env" not in ws:
40
  ws["env"] = "unset"
41
  try_to_load_workspace(ws, name)
42
- room = pycrdt_websocket.YRoom(ystore=ystore, ydoc=ydoc)
 
 
43
  room.ws = ws
44
 
45
  def on_change(changes):
@@ -56,12 +63,6 @@ class WebsocketServer(pycrdt_websocket.WebsocketServer):
56
  return room
57
 
58
 
59
- websocket_server = WebsocketServer(
60
- # exception_handler=ws_exception_handler,
61
- auto_clean_rooms=False,
62
- )
63
- asgi_server = pycrdt_websocket.ASGIServer(websocket_server)
64
-
65
  last_ws_input = None
66
 
67
 
@@ -140,8 +141,13 @@ async def workspace_changed(e, ws_crdt):
140
 
141
  @contextlib.asynccontextmanager
142
  async def lifespan(app):
 
 
 
 
143
  async with websocket_server:
144
  yield
 
145
 
146
 
147
  def sanitize_path(path):
@@ -151,4 +157,5 @@ def sanitize_path(path):
151
  @router.websocket("/ws/crdt/{room_name}")
152
  async def crdt_websocket(websocket: fastapi.WebSocket, room_name: str):
153
  room_name = sanitize_path(room_name)
154
- await asgi_server({"path": room_name}, websocket._receive, websocket._send)
 
 
7
  import os.path
8
  import pycrdt
9
  import pycrdt_websocket
 
10
  import pycrdt_websocket.ystore
11
+ import uvicorn
12
+ import builtins
13
 
14
  router = fastapi.APIRouter()
15
 
16
 
17
  def ws_exception_handler(exception, log):
18
+ if isinstance(exception, builtins.ExceptionGroup):
19
+ for ex in exception.exceptions:
20
+ if not isinstance(ex, uvicorn.protocols.utils.ClientDisconnected):
21
+ log.exception(ex)
22
+ else:
23
+ log.exception(exception)
24
  return True
25
 
26
 
 
44
  if "env" not in ws:
45
  ws["env"] = "unset"
46
  try_to_load_workspace(ws, name)
47
+ room = pycrdt_websocket.YRoom(
48
+ ystore=ystore, ydoc=ydoc, exception_handler=ws_exception_handler
49
+ )
50
  room.ws = ws
51
 
52
  def on_change(changes):
 
63
  return room
64
 
65
 
 
 
 
 
 
 
66
  last_ws_input = None
67
 
68
 
 
141
 
142
  @contextlib.asynccontextmanager
143
  async def lifespan(app):
144
+ global websocket_server
145
+ websocket_server = WebsocketServer(
146
+ auto_clean_rooms=False,
147
+ )
148
  async with websocket_server:
149
  yield
150
+ print("closing websocket server for some reason")
151
 
152
 
153
  def sanitize_path(path):
 
157
  @router.websocket("/ws/crdt/{room_name}")
158
  async def crdt_websocket(websocket: fastapi.WebSocket, room_name: str):
159
  room_name = sanitize_path(room_name)
160
+ server = pycrdt_websocket.ASGIServer(websocket_server)
161
+ await server({"path": room_name}, websocket._receive, websocket._send)