Skip to content

[Bugfix][Router] Resolve backend hostnames with getaddrinfo - #1162

Open
Mazyod wants to merge 2 commits into
vllm-project:mainfrom
Mazyod:fix/backend-dns-negative-cache
Open

Mazyod wants to merge 2 commits into
vllm-project:mainfrom
Mazyod:fix/backend-dns-negative-cache

Conversation

@Mazyod

@Mazyod Mazyod commented Oct 20, 2026

Copy link
Copy Markdown

FIX #1161

The shared backend session used aiohttp's default resolver, which is c-ares when aiodns is installed through aiohttp[speedups]. c-ares keeps a cached NXDOMAIN for the SOA negative TTL (up to an hour), so a backend whose name resolved only after the router's first lookup could stay unreachable for that long. Details and a standalone repro are in the issue.

This PR passes aiohttp.ThreadedResolver() to the shared session's connector: a one-line change plus a comment.

  • The shared backend client now resolves with the system getaddrinfo and skips the extra c-ares query cache. Caching in the system or upstream DNS can still delay recovery.
  • aiohttp's successful-lookup cache, the connection-error type (ClientConnectorDNSError), and the speedups extra are unchanged. Lookups run in the event loop's executor.
  • Scope is the shared client only. The disaggregated-prefill, sleep/wake, and external-provider sessions are unchanged here.

Testing:

  • src/tests/test_aiohttp_client.py patches socket.getaddrinfo to fail and then succeed, and checks that the shared client reaches a local server on the second request. It fails on main, where c-ares never consults getaddrinfo, and passes with this change.
  • Locally, all 273 router tests pass on Python 3.12, and pre-commit and ruff check pass.

  • Make sure the code changes pass the pre-commit checks.
  • Sign-off your commit by using -s when doing git commit
  • Try to classify PRs for easy understanding of the type of changes, such as [Bugfix], [Feat], and [CI].

aiohttp[speedups] installs aiodns, so aiohttp's default resolver is
c-ares. c-ares caches NXDOMAIN in-process for the SOA negative TTL, up to
an hour. A backend whose hostname did not resolve on the router's first
attempt can therefore keep failing with a connection error after DNS
answers, until the cached entry expires or the router restarts. One
example is a Docker Compose service that starts after the router.

Use aiohttp.ThreadedResolver for the shared backend session. It resolves
with the system getaddrinfo instead of the additional c-ares query cache.
System or upstream DNS caching still applies, and aiohttp's
successful-lookup cache (ttl_dns_cache, 10 s) is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Mazyad Alabduljaleel <[email protected]>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request configures the AiohttpClientWrapper to use aiohttp.ThreadedResolver instead of the default resolver, preventing failed DNS lookups from being cached and ensuring newly created backends are reachable. A test case is also introduced to verify this behavior. The feedback suggests refactoring the test's setup and teardown logic using nested try...finally blocks to prevent resource leaks and avoid masking exceptions if initialization fails.

Comment on lines +20 to +47
await runner.setup()
await web.TCPSite(runner, "127.0.0.1", 0).start()
url = f"http://vllm-worker.invalid:{runner.addresses[0][1]}/health"

real_getaddrinfo = socket.getaddrinfo
service_exists = False

def getaddrinfo(host, *args, **kwargs):
if host == "vllm-worker.invalid":
if not service_exists:
raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")
host = "127.0.0.1"
return real_getaddrinfo(host, *args, **kwargs)

monkeypatch.setattr(socket, "getaddrinfo", getaddrinfo)
wrapper = AiohttpClientWrapper()
wrapper.start()
try:
with pytest.raises(aiohttp.ClientConnectorDNSError):
async with wrapper().get(url):
pass

service_exists = True
async with wrapper().get(url) as response:
assert response.status == 200
finally:
await wrapper.stop()
await runner.cleanup()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If web.TCPSite.start() or any other setup step fails before the try block is entered, runner.cleanup() will not be called, potentially leaking the runner resources. Additionally, if wrapper.start() fails or is not reached, calling wrapper.stop() in the finally block will raise an AttributeError because wrapper.async_client is None, masking the original exception.

Using nested try...finally blocks ensures that both the runner and the client wrapper are safely and independently cleaned up only if their respective initialization succeeded.

    await runner.setup()
    try:
        await web.TCPSite(runner, "127.0.0.1", 0).start()
        url = f"http://vllm-worker.invalid:{runner.addresses[0][1]}/health"

        real_getaddrinfo = socket.getaddrinfo
        service_exists = False

        def getaddrinfo(host, *args, **kwargs):
            if host == "vllm-worker.invalid":
                if not service_exists:
                    raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")
                host = "127.0.0.1"
            return real_getaddrinfo(host, *args, **kwargs)

        monkeypatch.setattr(socket, "getaddrinfo", getaddrinfo)
        wrapper = AiohttpClientWrapper()
        wrapper.start()
        try:
            with pytest.raises(aiohttp.ClientConnectorDNSError):
                async with wrapper().get(url):
                    pass

            service_exists = True
            async with wrapper().get(url) as response:
                assert response.status == 200
        finally:
            await wrapper.stop()
    finally:
        await runner.cleanup()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 1c3a483: the test server is now released even if a later setup step fails. wrapper.start() already ran before the inner try, so stop() couldn't hide an earlier error, but the nesting makes the cleanup order explicit.

Address review feedback: release the test HTTP server even if a later setup step fails.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Mazyad Alabduljaleel <[email protected]>
Mazyod pushed a commit to Mazyod/production-stack that referenced this pull request Oct 20, 2026
…laybook

Point the single-label hostname patch at upstream PR vllm-project#1154, which
superseded the now-closed vllm-project#737. Record that the DNS resolver change is
proposed upstream for the shared client only (vllm-project#1161/vllm-project#1162), and that
upstream vllm-project#1164's 503 must not replace the fork's documented 502 envelope
on a refresh. Add the evidence-based playbook for getting fixes merged
upstream. Docs only; does not trigger an image release. Standard
pre-commit hooks pass on the changed files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01A5cnah4yC2sS7LEZ8mdrTd
Signed-off-by: ci-sim <ci@sim>
@Mazyod

Mazyod commented Nov 18, 2026

Copy link
Copy Markdown
Author

Hi @ruizhang0101, could you take a look when you have a chance? I fixed the test cleanup from review. We’ve been using the resolver change in our fork since October. Thanks!

@Mazyod

Mazyod commented Nov 24, 2026

Copy link
Copy Markdown
Author

Status update: this still merges cleanly onto current main (405a2fa), and all 284 router tests pass with it on Python 3.12. We've run the change in production through our 0.1.13 and 0.1.14 router releases. @ruizhang0101, could you approve the CI workflows when you get a chance? Happy to rebase or adjust anything.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: router keeps failing a backend for up to an hour after its hostname starts resolving (c-ares NXDOMAIN cache)

1 participant