@@ -82,6 +82,21 @@ async def prepare(self):
8282 if self ._websocket_server .room_exists (self ._room_id ):
8383 self .room : YRoom = await self ._websocket_server .get_room (self ._room_id )
8484 else :
85+ # Logging exceptions, instead of raising them here to ensure
86+ # that the y-rooms stay alive even after an exception is seen.
87+ def exception_logger (exception : Exception , log : Logger ) -> bool :
88+ """A function that catches any exceptions raised in the websocket
89+ server and logs them.
90+
91+ The protects the y-room's task group from cancelling
92+ anytime an exception is raised.
93+ """
94+ log .error (
95+ f"Document Room Exception, (room_id={ self ._room_id or 'unknown' } : " ,
96+ exc_info = exception ,
97+ )
98+ return True
99+
85100 if self ._room_id .count (":" ) >= 2 :
86101 # DocumentRoom
87102 file_format , file_type , file_id = decode_file_path (self ._room_id )
@@ -103,33 +118,18 @@ async def prepare(self):
103118 self .event_logger ,
104119 ystore ,
105120 self .log ,
106- self ._document_save_delay ,
121+ exception_handler = exception_logger ,
122+ save_delay = self ._document_save_delay ,
107123 )
108124
109- def exception_logger (exception : Exception , log : Logger ) -> bool :
110- """A function that catches any exceptions raised in the websocket
111- server and logs them.
112-
113- The protects the y-room's task group from cancelling
114- anytime an exception is raised.
115- """
116- room_id = "unknown"
117- if self .room .room_id :
118- room_id = self .room .room_id
119- log .error (
120- f"Document Room Exception, (room_id={ room_id } : " ,
121- exc_info = exception ,
122- )
123- return True
124-
125- # Logging exceptions, instead of raising them here to ensure
126- # that the y-rooms stay alive even after an exception is seen.
127- self .room .exception_handler = exception_logger
128-
129125 else :
130126 # TransientRoom
131127 # it is a transient document (e.g. awareness)
132- self .room = TransientRoom (self ._room_id , self .log )
128+ self .room = TransientRoom (
129+ self ._room_id ,
130+ exception_handler = exception_logger ,
131+ log = self .log ,
132+ )
133133
134134 await self ._websocket_server .start_room (self .room )
135135 self ._websocket_server .add_room (self ._room_id , self .room )
0 commit comments