@@ -347,7 +347,7 @@ def get_endpoint_info(self) -> List[EndpointInfo]:
347347
348348 async def initialize_client_sessions (self ) -> None :
349349 """
350- Initialize aiohttp ClientSession objects for prefill and decode endpoints.
350+ Initialize aiohttp client sessions for prefill and decode endpoints.
351351 This must be called from an async context during app startup.
352352 """
353353 if (
@@ -756,18 +756,22 @@ def _add_engine(
756756 # Store model information in the endpoint info
757757 self .available_engines [engine_name ].model_info = model_info
758758
759- if self .event_loop_ready .is_set () and self .event_loop is not None :
760- try :
759+ # Initialize client sessions only if event_loop is available
760+ try :
761+ if hasattr (self .app .state , "event_loop" ) and self .app .state .event_loop :
761762 fut = asyncio .run_coroutine_threadsafe (
762- self .initialize_client_sessions (),
763- self .event_loop ,
763+ self .initialize_client_sessions (), self .app .state .event_loop
764764 )
765765 fut .result ()
766- except Exception as e :
767- logger .error (f"Error initializing client sessions: { e } " )
768- else :
769- logger .debug (
770- "Event loop not ready; deferring client session initialization"
766+ logger .info ("Client sessions initialized successfully in _add_engine" )
767+ else :
768+ # Event loop not ready yet, client sessions will be initialized in lifespan
769+ logger .debug (
770+ "Event loop not ready in _add_engine, client sessions will be initialized later"
771+ )
772+ except Exception as e :
773+ logger .error (
774+ f"Error initializing client sessions in _add_engine: { e } " , exc_info = True
771775 )
772776
773777 # Track all models we've ever seen
@@ -850,35 +854,63 @@ def close(self):
850854
851855 async def initialize_client_sessions (self ) -> None :
852856 """
853- Initialize aiohttp ClientSession objects for prefill and decode endpoints.
857+ Initialize aiohttp client sessions for prefill and decode endpoints.
854858 This must be called from an async context during app startup.
855859 """
860+ logger .debug (
861+ f"initialize_client_sessions called. prefill_model_labels={ self .prefill_model_labels } , decode_model_labels={ self .decode_model_labels } "
862+ )
856863 if (
857864 self .prefill_model_labels is not None
858865 and self .decode_model_labels is not None
859866 ):
860867 endpoint_infos = self .get_endpoint_info ()
868+ logger .debug (f"Got { len (endpoint_infos )} endpoints" )
861869 for endpoint_info in endpoint_infos :
870+ logger .debug (
871+ f"Checking endpoint: url={ endpoint_info .url } , model_label={ endpoint_info .model_label } "
872+ )
862873 if endpoint_info .model_label in self .prefill_model_labels :
863874 if (
864875 hasattr (self .app .state , "prefill_client" )
865876 and self .app .state .prefill_client is not None
866877 ):
867- await self .app .state .prefill_client .close ()
868- self .app .state .prefill_client = aiohttp .ClientSession (
869- base_url = endpoint_info .url ,
870- timeout = aiohttp .ClientTimeout (total = None ),
871- )
878+ # Session already initialised; skip to avoid disrupting
879+ # in-flight requests. xPyD (multiple prefill nodes) is
880+ # not supported in this PR — only the first discovered
881+ # prefill endpoint is used.
882+ logger .debug (
883+ f"prefill_client already set, skipping { endpoint_info .url } "
884+ )
885+ else :
886+ self .app .state .prefill_client = aiohttp .ClientSession (
887+ base_url = endpoint_info .url ,
888+ timeout = aiohttp .ClientTimeout (total = None ),
889+ )
890+ logger .info (
891+ f"Created prefill_client for { endpoint_info .url } with timeout=None"
892+ )
893+
872894 elif endpoint_info .model_label in self .decode_model_labels :
873895 if (
874896 hasattr (self .app .state , "decode_client" )
875897 and self .app .state .decode_client is not None
876898 ):
877- await self .app .state .decode_client .close ()
878- self .app .state .decode_client = aiohttp .ClientSession (
879- base_url = endpoint_info .url ,
880- timeout = aiohttp .ClientTimeout (total = None ),
881- )
899+ logger .debug (
900+ f"decode_client already set, skipping { endpoint_info .url } "
901+ )
902+ else :
903+ self .app .state .decode_client = aiohttp .ClientSession (
904+ base_url = endpoint_info .url ,
905+ timeout = aiohttp .ClientTimeout (total = None ),
906+ )
907+ logger .info (
908+ f"Created decode_client for { endpoint_info .url } with timeout=None"
909+ )
910+ else :
911+ logger .warning (
912+ "prefill_model_labels or decode_model_labels is None, skipping client session initialization"
913+ )
882914
883915 def has_ever_seen_model (self , model_name : str ) -> bool :
884916 """Check if we've ever seen this model, even if currently scaled to zero."""
@@ -1212,6 +1244,21 @@ def _add_engine(self, engine_name: str, model_names: List[str], model_label: str
12121244 # Store model information in the endpoint info
12131245 self .available_engines [engine_name ].model_info = model_info
12141246
1247+ try :
1248+ # Only initialize client sessions if event_loop is available
1249+ if hasattr (self .app .state , "event_loop" ) and self .app .state .event_loop :
1250+ fut = asyncio .run_coroutine_threadsafe (
1251+ self .initialize_client_sessions (), self .app .state .event_loop
1252+ )
1253+ fut .result ()
1254+ else :
1255+ # Event loop not ready yet, client sessions will be initialized in lifespan
1256+ logger .debug (
1257+ "Event loop not ready, client sessions will be initialized later"
1258+ )
1259+ except Exception as e :
1260+ logger .error (f"Error initializing client sessions: { e } " )
1261+
12151262 def _delete_engine (self , engine_name : str ):
12161263 logger .info (f"Serving engine { engine_name } is deleted" )
12171264 with self .available_engines_lock :
@@ -1287,25 +1334,58 @@ def close(self):
12871334
12881335 async def initialize_client_sessions (self ) -> None :
12891336 """
1290- Initialize aiohttp ClientSession objects for prefill and decode endpoints.
1337+ Initialize aiohttp client sessions for prefill and decode endpoints.
12911338 This must be called from an async context during app startup.
12921339 """
1340+ logger .debug (
1341+ f"K8sServiceNameServiceDiscovery.initialize_client_sessions called. prefill_model_labels={ self .prefill_model_labels } , decode_model_labels={ self .decode_model_labels } "
1342+ )
12931343 if (
12941344 self .prefill_model_labels is not None
12951345 and self .decode_model_labels is not None
12961346 ):
12971347 endpoint_infos = self .get_endpoint_info ()
1348+ logger .debug (f"Got { len (endpoint_infos )} endpoints" )
12981349 for endpoint_info in endpoint_infos :
1350+ logger .debug (
1351+ f"Checking endpoint: url={ endpoint_info .url } , model_label={ endpoint_info .model_label } "
1352+ )
12991353 if endpoint_info .model_label in self .prefill_model_labels :
1300- self .app .state .prefill_client = aiohttp .ClientSession (
1301- base_url = endpoint_info .url ,
1302- timeout = aiohttp .ClientTimeout (total = None ),
1303- )
1354+ if (
1355+ hasattr (self .app .state , "prefill_client" )
1356+ and self .app .state .prefill_client is not None
1357+ ):
1358+ logger .debug (
1359+ f"prefill_client already set, skipping { endpoint_info .url } "
1360+ )
1361+ else :
1362+ self .app .state .prefill_client = aiohttp .ClientSession (
1363+ base_url = endpoint_info .url ,
1364+ timeout = aiohttp .ClientTimeout (total = None ),
1365+ )
1366+ logger .info (
1367+ f"Created prefill_client for { endpoint_info .url } with timeout=None"
1368+ )
13041369 elif endpoint_info .model_label in self .decode_model_labels :
1305- self .app .state .decode_client = aiohttp .ClientSession (
1306- base_url = endpoint_info .url ,
1307- timeout = aiohttp .ClientTimeout (total = None ),
1308- )
1370+ if (
1371+ hasattr (self .app .state , "decode_client" )
1372+ and self .app .state .decode_client is not None
1373+ ):
1374+ logger .debug (
1375+ f"decode_client already set, skipping { endpoint_info .url } "
1376+ )
1377+ else :
1378+ self .app .state .decode_client = aiohttp .ClientSession (
1379+ base_url = endpoint_info .url ,
1380+ timeout = aiohttp .ClientTimeout (total = None ),
1381+ )
1382+ logger .info (
1383+ f"Created decode_client for { endpoint_info .url } with timeout=None"
1384+ )
1385+ else :
1386+ logger .warning (
1387+ "K8sServiceNameServiceDiscovery: prefill_model_labels or decode_model_labels is None, skipping client session initialization"
1388+ )
13091389
13101390
13111391def _create_service_discovery (
0 commit comments