1717from textual ._log import LogGroup , LogVerbosity
1818from textual .constants import DEVTOOLS_PORT
1919
20+ READY_TIMEOUT = 0.5
2021WEBSOCKET_CONNECT_TIMEOUT = 3
2122LOG_QUEUE_MAXSIZE = 512
2223
@@ -102,6 +103,7 @@ def __init__(self, host: str = "127.0.0.1", port: int | None = None) -> None:
102103 self .log_queue : Queue [str | bytes | Type [ClientShutdown ]] | None = None
103104 self .spillover : int = 0
104105 self .verbose : bool = False
106+ self ._ready_event : asyncio .Event = asyncio .Event ()
105107
106108 async def connect (self ) -> None :
107109 """Connect to the devtools server.
@@ -139,6 +141,7 @@ async def update_console() -> None:
139141 self .console .width = payload ["width" ]
140142 self .console .height = payload ["height" ]
141143 self .verbose = payload .get ("verbose" , False )
144+ self ._ready_event .set ()
142145
143146 async def send_queued_logs ():
144147 """Coroutine function which is scheduled as a Task, which consumes
@@ -156,9 +159,18 @@ async def send_queued_logs():
156159 await websocket .send_bytes (log )
157160 log_queue .task_done ()
158161
162+ async def server_info_received () -> None :
163+ """Wait for the first server info message to be received and handled."""
164+ try :
165+ await asyncio .wait_for (self ._ready_event .wait (), timeout = READY_TIMEOUT )
166+ except asyncio .TimeoutError :
167+ return
168+
159169 self .log_queue_task = asyncio .create_task (send_queued_logs ())
160170 self .update_console_task = asyncio .create_task (update_console ())
161171
172+ await server_info_received ()
173+
162174 async def _stop_log_queue_processing (self ) -> None :
163175 """Schedule end of processing of the log queue, meaning that any messages a
164176 user logs will be added to the queue, but not consumed and sent to
0 commit comments