Repository navigation
fix(llm): bound model-info discovery with a timeout to prevent deadlocks #3912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bconsolvo
wants to merge
10
commits into
OpenHands:main
Choose a base branch
from
bconsolvo:fix/model-info-discovery-deadlock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
461a848
fix(llm): bound model-info discovery with a timeout to prevent deadlocks
bconsolvo dd2dde9
Merge remote-tracking branch 'origin/main' into fix/model-info-discov…
bconsolvo 5173128
fix(llm): address review — fix broken test assertion and shutdown hang
bconsolvo 22b0d0f
Merge branch 'main' into fix/model-info-discovery-deadlock
bconsolvo 85ce417
fix(llm): guard probe thread start during interpreter shutdown
bconsolvo 8e1eb3c
Merge remote-tracking branch 'origin/main' into fix/model-info-discov…
bconsolvo 3074209
style(llm): apply ruff format to model-info-discovery thread call
bconsolvo 98db909
fix(llm): bound the proxy underlying-model probe too
bconsolvo 85fbda1
Merge remote-tracking branch 'upstream/main' into fix/model-info-disc…
bconsolvo b6cb261
test(llm): expect proxy capability source in underlying-probe timeout…
bconsolvo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| """Model-info discovery must never block the caller indefinitely. | ||
|
|
||
| Some providers (self-hosted / OpenAI-compatible such as ``lemonade``, | ||
| ``ollama`` and ``vllm``) resolve model info via a live HTTP request to the | ||
| model server. litellm makes that request synchronously with no timeout, so an | ||
| unreachable endpoint -- or one that loops back to the caller through a reverse | ||
| proxy -- hangs forever. Because ``LLM`` construction runs this probe in | ||
| ``_post_init``, that previously froze any caller, including an async server's | ||
| event loop. | ||
|
|
||
| These tests pin the bounded behavior: discovery returns ``None`` within the | ||
| deadline instead of hanging. | ||
| """ | ||
|
|
||
| import time | ||
| from types import SimpleNamespace | ||
|
|
||
| from openhands.sdk.llm.utils import model_info | ||
| from openhands.sdk.llm.utils.model_info import ( | ||
| _run_with_deadline, | ||
| get_litellm_model_info, | ||
| ) | ||
|
|
||
|
|
||
| def test_run_with_deadline_returns_none_on_timeout(): | ||
| def _hang(*_a, **_kw): | ||
| time.sleep(30) | ||
| return "never" | ||
|
|
||
| started = time.time() | ||
| result = _run_with_deadline(_hang, timeout=0.2) | ||
| elapsed = time.time() - started | ||
|
|
||
| assert result is None | ||
| # Returned promptly rather than waiting for the (abandoned) call. | ||
| assert elapsed < 5 | ||
|
|
||
|
|
||
| def test_run_with_deadline_returns_value_when_fast(): | ||
| assert _run_with_deadline(lambda: 42, timeout=5) == 42 | ||
|
|
||
|
|
||
| def test_get_litellm_model_info_does_not_hang_on_slow_probe(monkeypatch): | ||
| """A self-hosted model whose model-info probe hangs must not block. | ||
|
|
||
| Regression test for the deadlock where ``litellm.get_model_info`` for an | ||
| unreachable self-hosted endpoint blocked ``LLM`` construction (and any | ||
| async caller) forever. | ||
| """ | ||
|
|
||
| def _hang(*_a, **_kw): | ||
| time.sleep(30) | ||
| raise AssertionError("should have been abandoned by the deadline") | ||
|
|
||
| monkeypatch.setattr(model_info, "get_model_info", _hang) | ||
| monkeypatch.setattr(model_info, "MODEL_INFO_DISCOVERY_TIMEOUT", 0.2) | ||
|
|
||
| started = time.time() | ||
| info = get_litellm_model_info( | ||
| secret_api_key=None, | ||
| base_url="http://localhost:9", | ||
| model="lemonade/some-self-hosted-model", | ||
| ) | ||
| elapsed = time.time() - started | ||
|
|
||
| assert info is None | ||
| # Two bounded fallback probes at ~0.2s each must still return promptly. | ||
| assert elapsed < 5 | ||
|
|
||
|
|
||
| def test_litellm_proxy_underlying_probe_does_not_hang(monkeypatch): | ||
| """The proxy alias-pricing probe must also be bounded. | ||
|
|
||
| ``_get_model_info_from_litellm_proxy`` resolves a proxy alias, then probes | ||
| the *underlying* model via ``get_model_info`` to make the alias priceable. | ||
| That nested probe is a synchronous, network-bound litellm call with no | ||
| timeout and runs inside ``LLM._post_init``; an unreachable underlying | ||
| endpoint there would reintroduce the deadlock this PR targets. | ||
| """ | ||
|
|
||
| def _fake_proxy_response(*_a, **_kw): | ||
| # Advertise an alias whose underlying model differs from the request, | ||
| # so the underlying-model probe branch runs. | ||
| return SimpleNamespace( | ||
| json=lambda: { | ||
| "data": [ | ||
| { | ||
| "model_name": "my-alias", | ||
| "litellm_params": {"model": "some/underlying-model"}, | ||
| "model_info": {"max_input_tokens": 100}, | ||
| } | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
| def _hang(*_a, **_kw): | ||
| time.sleep(30) | ||
| raise AssertionError("underlying probe should have been abandoned") | ||
|
|
||
| monkeypatch.setattr(model_info.httpx, "get", _fake_proxy_response) | ||
| monkeypatch.setattr(model_info, "get_model_info", _hang) | ||
| monkeypatch.setattr(model_info, "MODEL_INFO_DISCOVERY_TIMEOUT", 0.2) | ||
| # Bypass the hourly lru_cache so this probe actually runs. | ||
| model_info._get_model_info_from_litellm_proxy.cache_clear() | ||
|
|
||
| started = time.time() | ||
| info = get_litellm_model_info( | ||
| secret_api_key=None, | ||
| base_url="http://localhost:9", | ||
| model="litellm_proxy/my-alias", | ||
| ) | ||
| elapsed = time.time() - started | ||
|
|
||
| # The alias's own model_info is still returned; only the underlying probe | ||
| # was abandoned, and it did so without blocking construction. | ||
| assert info == {"max_input_tokens": 100, "_capability_source": "proxy"} | ||
| assert elapsed < 5 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.