Skip to content

adding exception handling for room start tasks #30

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions pycrdt_websocket/yroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from anyio.abc import TaskGroup, TaskStatus
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
from pycrdt import Doc, Subscription
from tornado.websocket import WebSocketClosedError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pycrdt-websocket is server agnostic, Tornado cannot be used here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


from .awareness import Awareness
from .websocket import Websocket
Expand Down Expand Up @@ -138,12 +139,24 @@ async def _broadcast_updates(self):
# broadcast internal ydoc's update to all clients, that includes changes from the
# clients and changes from the backend (out-of-band changes)
for client in self.clients:
self.log.debug("Sending Y update to client with endpoint: %s", client.path)
message = create_update_message(update)
self._task_group.start_soon(client.send, message)
try:
self.log.debug("Sending Y update to client with endpoint: %s", client.path)
message = create_update_message(update)
self._task_group.start_soon(client.send, message)
except Exception as e:
self.log.error(
"Error sending Y update to client with endpoint: %s",
client.path,
exc_info=e,
)
if isinstance(e, WebSocketClosedError):
self.client.remove(client)
if self.ystore:
self.log.debug("Writing Y update to YStore")
self._task_group.start_soon(self.ystore.write, update)
try:
self._task_group.start_soon(self.ystore.write, update)
self.log.debug("Writing Y update to YStore")
except Exception as e:
self.log.error("Error writing Y update to YStore", exc_info=e)

async def __aenter__(self) -> YRoom:
async with self._start_lock:
Expand Down Expand Up @@ -210,9 +223,9 @@ async def serve(self, websocket: Websocket):
websocket: The WebSocket through which to serve the client.
"""
async with create_task_group() as tg:
self.clients.append(websocket)
await sync(self.ydoc, websocket, self.log)
try:
self.clients.append(websocket)
await sync(self.ydoc, websocket, self.log)
async for message in websocket:
# filter messages (e.g. awareness)
skip = False
Expand Down Expand Up @@ -246,7 +259,9 @@ async def serve(self, websocket: Websocket):
)
tg.start_soon(client.send, message)
except Exception as e:
self.log.debug("Error serving endpoint: %s", websocket.path, exc_info=e)
self.log.error("Error serving endpoint: %s", websocket.path, exc_info=e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove a client if we cannot serve them?

Suggested change
self.log.error("Error serving endpoint: %s", websocket.path, exc_info=e)
self.log.error("Error serving endpoint: %s", websocket.path, exc_info=e)
self.clients.remove(websocket)

if isinstance(e, WebSocketClosedError):
raise e

# remove this client
self.clients = [c for c in self.clients if c != websocket]
Loading