@@ -314,29 +314,32 @@ def __exit__(self, *args: Any) -> None:
314314 self .close ()
315315
316316 def _run_stream (self ) -> None :
317- """Background thread that maintains the SSE connection."""
317+ """Background thread that maintains the SSE connection.
318+
319+ During initialization: retries until init succeeds or wait_for_init times out.
320+ After initialization: retries indefinitely until close() is called.
321+ """
318322 retry_count = 0
319323 max_retries = 10
320324
321325 while not self ._stop_event .is_set ():
322326 try :
323327 self ._connect_stream ()
324328 retry_count = 0 # Reset on successful connection
325- except ReplaneError as e :
329+ except AuthenticationError as e :
330+ # Auth errors are permanent - don't retry
326331 if not self ._initialized .is_set ():
327332 self ._init_error = e
328333 self ._initialized .set ()
329- return
334+ return
330335
336+ except ReplaneError as e :
337+ # During init: log and retry (wait_for_init will timeout if needed)
338+ # After init: log and retry indefinitely
331339 logger .warning ("SSE connection error: %s" , e )
332340
333341 except Exception as e :
334342 error = NetworkError (str (e ), cause = e )
335- if not self ._initialized .is_set ():
336- self ._init_error = error
337- self ._initialized .set ()
338- return
339-
340343 logger .warning ("SSE connection error: %s" , error )
341344
342345 if self ._stop_event .is_set ():
@@ -366,24 +369,23 @@ def _connect_stream(self) -> None:
366369 is_https ,
367370 )
368371
369- # Create connection
370- # Note: SSE connections are long-lived, so we use the inactivity timeout
371- # rather than the short request timeout. The inactivity timeout is used
372- # for detecting dead connections during streaming.
372+ # Use request_timeout for the initial handshake (server should respond
373+ # with headers within this time). After headers are received, we set
374+ # the socket timeout to inactivity_timeout for streaming.
373375 conn : http .client .HTTPConnection
374376 if is_https :
375377 context = ssl .create_default_context ()
376378 conn = http .client .HTTPSConnection (
377379 host ,
378380 port ,
379- timeout = self ._inactivity_timeout ,
381+ timeout = self ._request_timeout ,
380382 context = context ,
381383 )
382384 else :
383385 conn = http .client .HTTPConnection (
384386 host ,
385387 port ,
386- timeout = self ._inactivity_timeout ,
388+ timeout = self ._request_timeout ,
387389 )
388390
389391 try :
0 commit comments