Skip to content

perf(model-metadata): resolve localhost to IPv4 in detect_local_server_type - #37595

Closed
rodboev wants to merge 3 commits into
NousResearch:mainfrom
rodboev:pr/ipv4-localhost-probe
Closed

perf(model-metadata): resolve localhost to IPv4 in detect_local_server_type#37595
rodboev wants to merge 3 commits into
NousResearch:mainfrom
rodboev:pr/ipv4-localhost-probe

Conversation

@rodboev

@rodboev rodboev commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

On Windows, localhost resolves to both ::1 (IPv6) and 127.0.0.1 (IPv4). Python httpx tries IPv6 first; when the local server only binds IPv4, each connection attempt hangs for the full 2 sec connect timeout before falling back. detect_local_server_type() is called 3+ times during agent init (from fetch_endpoint_model_metadata, get_ollama_num_ctx, and _query_local_context_length), each creating a fresh httpx.Client, so the IPv6 timeout compounds to ~6-14s of dead time.

The fix replaces localhost with 127.0.0.1 inside detect_local_server_type() before connecting. The function is only called for local endpoints (all callers guard with is_local_endpoint()), so IPv6 loopback adds no diagnostic value and the replacement is safe.

Changes

  • agent/model_metadata.py: resolve localhost to 127.0.0.1 in detect_local_server_type() after _normalize_base_url and /v1 stripping, before probe requests (+6 lines)
  • tests/agent/test_model_metadata_local_ctx.py: add TestDetectLocalServerTypeLocalhostIPv4 with 3 tests verifying localhost resolution, LAN IP passthrough, and 127.0.0.1 passthrough (+60 lines)

Validation

Scenario Before After
localhost:8317 (IPv4-only server, Windows) 2 sec per probe (~14s total) <0.1s per probe (~4s total)
127.0.0.1:8317 fast fast (unchanged)
192.168.1.100:8080 (LAN IP) fast fast (unchanged)
Linux/macOS (localhost = IPv4 only) fast fast (unchanged)

Test plan

  • pytest tests/agent/test_model_metadata_local_ctx.py -v --timeout=0 — 25 passed
  • Manual: hermes -m claude-sonnet-4-6 -z "test" on Windows with localhost base_url — 4.0s vs 19.9s before

Fixes #37594

@alt-glitch alt-glitch added type/perf Performance improvement or optimization P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Jun 2, 2026
@rodboev
rodboev force-pushed the pr/ipv4-localhost-probe branch 2 times, most recently from bedec7a to 162aeca Compare June 28, 2026 20:00
rodboev added 3 commits July 7, 2026 00:34
…r_type

On Windows, `localhost` resolves to both ::1 (IPv6) and 127.0.0.1 (IPv4).
httpx tries IPv6 first, hanging 2 sec per probe when the server binds IPv4
only. detect_local_server_type() is called 3+ times during init, each with
a new httpx.Client, compounding to ~14s of dead time.

Replace localhost with 127.0.0.1 inside the function before connecting.
The function is only called for local endpoints (callers guard with
is_local_endpoint()), so IPv6 loopback adds no diagnostic value.

Measured: 19.9s → 4.0s on Windows with a local proxy on 127.0.0.1:8317.
@rodboev
rodboev force-pushed the pr/ipv4-localhost-probe branch from 300b8d8 to 7d324b0 Compare July 7, 2026 04:38
kshitijk4poor added a commit that referenced this pull request Jul 9, 2026
…be sites

#37595 fixed the Windows dual-stack IPv6 timeout only inside
detect_local_server_type. The same 2s-per-probe penalty existed at every
other helper that builds a probe URL from base_url. Extract the rewrite
into _localhost_to_ipv4() and apply it at:

- query_ollama_num_ctx
- query_ollama_supports_vision
- _query_ollama_api_show (server_url derivation)
- _query_local_context_length (server root + LM Studio native URL)

Tests cover the helper's URL forms, non-localhost passthrough, and that
the ollama probes actually POST to 127.0.0.1.
@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Thanks @rodboev — your fix landed! The probe-cache cluster has now landed on main via PR #61368 (merge commit f556edc), which salvaged this cluster of PRs onto current main with structured review, live smoke tests, and full test gates.

Both of your commits were cherry-picked with -x so your authorship is preserved on main (f9aa59e04 and b7af40bf4). During review we also widened your localhost→IPv4 rewrite to every sibling probe helper (ollama num_ctx / vision / api-show, LM Studio context query) via a shared _localhost_to_ipv4() helper — now scheme-anchored host-only so embedded URLs in query strings can't be corrupted — with tests covering all the URL forms.

Closing since this is now merged with your authorship intact.

santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
…be sites

NousResearch#37595 fixed the Windows dual-stack IPv6 timeout only inside
detect_local_server_type. The same 2s-per-probe penalty existed at every
other helper that builds a probe URL from base_url. Extract the rewrite
into _localhost_to_ipv4() and apply it at:

- query_ollama_num_ctx
- query_ollama_supports_vision
- _query_ollama_api_show (server_url derivation)
- _query_local_context_length (server root + LM Studio native URL)

Tests cover the helper's URL forms, non-localhost passthrough, and that
the ollama probes actually POST to 127.0.0.1.
justemu pushed a commit to justemu/hermes-agent that referenced this pull request Jul 18, 2026
…be sites

NousResearch#37595 fixed the Windows dual-stack IPv6 timeout only inside
detect_local_server_type. The same 2s-per-probe penalty existed at every
other helper that builds a probe URL from base_url. Extract the rewrite
into _localhost_to_ipv4() and apply it at:

- query_ollama_num_ctx
- query_ollama_supports_vision
- _query_ollama_api_show (server_url derivation)
- _query_local_context_length (server root + LM Studio native URL)

Tests cover the helper's URL forms, non-localhost passthrough, and that
the ollama probes actually POST to 127.0.0.1.
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
…be sites

NousResearch#37595 fixed the Windows dual-stack IPv6 timeout only inside
detect_local_server_type. The same 2s-per-probe penalty existed at every
other helper that builds a probe URL from base_url. Extract the rewrite
into _localhost_to_ipv4() and apply it at:

- query_ollama_num_ctx
- query_ollama_supports_vision
- _query_ollama_api_show (server_url derivation)
- _query_local_context_length (server root + LM Studio native URL)

Tests cover the helper's URL forms, non-localhost passthrough, and that
the ollama probes actually POST to 127.0.0.1.
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
…be sites

NousResearch#37595 fixed the Windows dual-stack IPv6 timeout only inside
detect_local_server_type. The same 2s-per-probe penalty existed at every
other helper that builds a probe URL from base_url. Extract the rewrite
into _localhost_to_ipv4() and apply it at:

- query_ollama_num_ctx
- query_ollama_supports_vision
- _query_ollama_api_show (server_url derivation)
- _query_local_context_length (server root + LM Studio native URL)

Tests cover the helper's URL forms, non-localhost passthrough, and that
the ollama probes actually POST to 127.0.0.1.
randlee pushed a commit to randlee/hermes-agent that referenced this pull request Aug 11, 2026
…be sites

NousResearch#37595 fixed the Windows dual-stack IPv6 timeout only inside
detect_local_server_type. The same 2s-per-probe penalty existed at every
other helper that builds a probe URL from base_url. Extract the rewrite
into _localhost_to_ipv4() and apply it at:

- query_ollama_num_ctx
- query_ollama_supports_vision
- _query_ollama_api_show (server_url derivation)
- _query_local_context_length (server root + LM Studio native URL)

Tests cover the helper's URL forms, non-localhost passthrough, and that
the ollama probes actually POST to 127.0.0.1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists type/perf Performance improvement or optimization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[perf] detect_local_server_type: IPv6 dual-stack timeout adds ~2s per probe on Windows

3 participants