Skip to content

Commit 5c18e54

Browse files
⚡ Bolt: Fix Router network config and lifecycle safety
- Configures `aiohttp.ClientSession(trust_env=True)` to respect proxy settings, which is critical for CI environments. - Adds defensive lazy initialization of `self.session` in `_generate` and `_generate_stream` to prevent crashes if `startup()` is bypassed in tests. - Re-verifies code style with `black`. Co-authored-by: ZeyuChen <1371212+ZeyuChen@users.noreply.github.com>
1 parent e017bd3 commit 5c18e54

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

fastdeploy/router/router.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def __init__(self, args):
101101
async def startup(self):
102102
"""Initialize resources"""
103103
if self.session is None:
104-
self.session = aiohttp.ClientSession()
104+
self.session = aiohttp.ClientSession(trust_env=True)
105105
logger.info("Router session initialized")
106106

107107
async def shutdown(self):
@@ -240,7 +240,8 @@ async def _generate(
240240
self, modified_request, urls, return_result_url_index=-1, endpoint="v1/chat/completions"
241241
) -> ORJSONResponse:
242242
if self.session is None:
243-
raise RuntimeError("Router session not initialized")
243+
logger.warning("Router session not initialized, creating a new one")
244+
self.session = aiohttp.ClientSession(trust_env=True)
244245

245246
timeout = aiohttp.ClientTimeout(total=self.timeout)
246247
tasks = [self.session.post(f"{url}/{endpoint}", json=modified_request, timeout=timeout) for url in urls]
@@ -273,7 +274,8 @@ async def _generate_stream(
273274
):
274275
async def stream_results():
275276
if self.session is None:
276-
raise RuntimeError("Router session not initialized")
277+
logger.warning("Router session not initialized, creating a new one")
278+
self.session = aiohttp.ClientSession(trust_env=True)
277279

278280
timeout = aiohttp.ClientTimeout(total=self.timeout)
279281
tasks = [self.session.post(f"{url}/{endpoint}", json=modified_request, timeout=timeout) for url in urls]

0 commit comments

Comments
 (0)