From 9a04bfc3642badb1402e33e65d648a2bd67446b5 Mon Sep 17 00:00:00 2001 From: huanghuoguoguo <60681390+huanghuoguoguo@users.noreply.github.com> Date: Sun, 14 Jun 2026 21:16:40 +0800 Subject: [PATCH] fix(modelmgr): bound Space model sync startup wait --- src/langbot/pkg/provider/modelmgr/modelmgr.py | 14 ++++++++++- .../unit_tests/provider/test_model_service.py | 23 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/langbot/pkg/provider/modelmgr/modelmgr.py b/src/langbot/pkg/provider/modelmgr/modelmgr.py index 0c577f1a..0b4fd2ed 100644 --- a/src/langbot/pkg/provider/modelmgr/modelmgr.py +++ b/src/langbot/pkg/provider/modelmgr/modelmgr.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio import sqlalchemy import traceback @@ -84,8 +85,19 @@ class ModelManager: self.ap.logger.info('LangBot Space Models service is disabled, skipping sync.') return + sync_timeout = space_config.get('models_sync_timeout') try: - await self.sync_new_models_from_space() + if sync_timeout: + await asyncio.wait_for( + self.sync_new_models_from_space(), + timeout=float(sync_timeout), + ) + else: + await self.sync_new_models_from_space() + except asyncio.TimeoutError: + self.ap.logger.warning( + f'LangBot Space model sync timed out after {sync_timeout}s, skipping startup sync.' + ) except Exception as e: self.ap.logger.warning('Failed to sync new models from LangBot Space, model list may not be updated.') self.ap.logger.warning(f' - Error: {e}') diff --git a/tests/unit_tests/provider/test_model_service.py b/tests/unit_tests/provider/test_model_service.py index ba1657cd..b4e1b3ca 100644 --- a/tests/unit_tests/provider/test_model_service.py +++ b/tests/unit_tests/provider/test_model_service.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio from types import SimpleNamespace from unittest.mock import AsyncMock, Mock @@ -88,6 +89,28 @@ def test_token_manager_next_token_ignores_empty_token_list(): assert token_mgr.using_token_index == 0 +@pytest.mark.asyncio +async def test_model_manager_initialize_skips_space_sync_after_timeout(): + ap = SimpleNamespace() + ap.discover = SimpleNamespace(get_components_by_kind=Mock(return_value=[])) + ap.instance_config = SimpleNamespace(data={'space': {'models_sync_timeout': 0.01}}) + ap.logger = Mock() + + mgr = ModelManager(ap) + mgr.load_models_from_db = AsyncMock() + + async def slow_sync(): + await asyncio.sleep(1) + + mgr.sync_new_models_from_space = AsyncMock(side_effect=slow_sync) + + await mgr.initialize() + + mgr.load_models_from_db.assert_awaited_once() + mgr.sync_new_models_from_space.assert_awaited_once() + ap.logger.warning.assert_any_call('LangBot Space model sync timed out after 0.01s, skipping startup sync.') + + @pytest.mark.asyncio async def test_updated_llm_model_is_immediately_usable_by_local_agent_pipeline(): from langbot.pkg.api.http.service.model import LLMModelsService