@@ -288,6 +288,11 @@ def _handle_event(self, event_type: str, data: Any) -> None:
288288 data = EvaluationRow (** data )
289289 self .websocket_manager .broadcast_row_upserted (data )
290290
291+ def start_loops (self ):
292+ """Start the broadcast loop and evaluation watcher."""
293+ self .websocket_manager .start_broadcast_loop ()
294+ self .evaluation_watcher .start ()
295+
291296 async def run_async (self ):
292297 """
293298 Run the logs server asynchronously with file watching.
@@ -300,11 +305,7 @@ async def run_async(self):
300305 logger .info (f"Serving files from: { self .build_dir } " )
301306 logger .info ("WebSocket endpoint available at /ws" )
302307
303- # Start the broadcast loop
304- self .websocket_manager .start_broadcast_loop ()
305-
306- # Start the evaluation watcher
307- self .evaluation_watcher .start ()
308+ self .start_loops ()
308309
309310 config = uvicorn .Config (
310311 self .app ,
@@ -336,24 +337,26 @@ def run(self):
336337
337338def create_app (host : str = "localhost" , port : int = 8000 , build_dir : Optional [str ] = None ) -> FastAPI :
338339 """
339- Factory function to create a FastAPI app instance.
340+ Factory function to create a FastAPI app instance and start the server with async loops .
340341
341- This allows uvicorn to call it with parameters and avoids top-level variable instantiation.
342+ This creates a LogsServer instance and starts it in a background thread to ensure
343+ all async loops (WebSocket broadcast, evaluation watching) are running.
342344
343345 Args:
344346 host: Host to bind to
345347 port: Port to bind to
346348 build_dir: Optional custom build directory path
347349
348350 Returns:
349- FastAPI app instance
351+ FastAPI app instance with server running in background
350352 """
351353 if build_dir is None :
352354 build_dir = os .path .abspath (
353355 os .path .join (os .path .dirname (os .path .dirname (os .path .dirname (__file__ ))), "vite-app" , "dist" )
354356 )
355357
356358 server = LogsServer (host = host , port = port , build_dir = build_dir )
359+ server .start_loops ()
357360 return server .app
358361
359362
0 commit comments