Skip to content

fix(cli): Treat 502/503/504 from a model endpoint as still starting - #4108

Merged
snivertynv merged 5 commits into
mainfrom
sniverty/local-vllm-external-ready
Oct 8, 2026
Merged

snivertynv merged 5 commits into
mainfrom
sniverty/local-vllm-external-ready

Conversation

@snivertynv

@snivertynv snivertynv commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Gym's model-endpoint startup check (RunHelper.wait_for_model_endpoints) counted any HTTP answer as ready, including the 503 a load balancer returns while none of its backends is healthy. With judges served by vLLM replicas behind a load balancer that are still loading, the check passed immediately and the first rollouts reached a judge with no healthy backend.

_probe_endpoint now classifies 502, 503 and 504 as "still starting", and the check waits on them like a refused connection, within the existing model_endpoint_readiness_timeout_seconds bound. 401, 404 and every other response still count as answering, so endpoints that need auth are unaffected.

Per review, the first commit's per-server wait in local_vllm_model is reverted, so that file is back to main. That wait ran before the server's port opened (cut short by server_spinup_timeout_seconds), required HTTP 200 without sending the API key, and blocked the local launch path on endpoints that answer 401.

Motivation: in NeMo-RL GRPO runs, the judge/reward models (GenRM, NL2Bash, safety judge) run as external vLLM pools behind a load balancer and are reached through base_url. With this change, the Ray cluster can start while those pools load, and the pools can live in a separate long-lived Slurm job that training jobs attach to. In both cases Gym must not send rollouts before the judges serve.

Validation

(Claude-authored testing)

  • pytest tests/unit_tests/test_cli_endpoint_readiness.py: 19 passed. New tests:
    • test_gateway_statuses_are_still_starting
    • test_waits_through_a_load_balancer_with_no_ready_backend
    • test_a_gateway_status_that_never_clears_is_reported
  • pytest responses_api_models/local_vllm_model/tests/test_app.py: 10 passed (unchanged from main).
  • pre-commit run --all-files: all hooks pass.

Rollouts

End to end with NeMo-RL GRPO (Nemotron-3.5-nano RLVR recipe, 10 nodes), with judge pools (GenRM, NL2Bash, safety) behind load balancers that come up before their vLLM replicas finish loading:

  • Wait: Gym logged Waiting for 4 model endpoint(s) to answer (3 policy servers plus the GenRM load balancer). The GenRM load balancer answered Gym's GET /v1/models with 503 every 3 s for about 2.5 minutes. Its first replica registered at the next probe, which got 200, and Gym continued (All model endpoints are answering after 169s).
  • No early requests: the judges received no rollout requests before they were ready, and none of their requests failed. Both GRPO steps completed.
  • Gym version used: this run used Gym 3615823e, the NeMo-RL pipeline's pinned version, whose _probe_endpoint and model-endpoint wait are identical to main's, with only this fix applied. Current main needs ray>=2.58, and that pipeline's server environments pin Ray 2.56.1.

Compatibility and benchmark impact

  • Behaviour change: an endpoint that answers 502/503/504 at startup now holds Gym startup until it answers otherwise, or until model_endpoint_readiness_timeout_seconds (default 600 s) passes. It then fails with the existing error, which now mentions the gateway case. Previously such an endpoint passed the check at once.
  • No new config fields: local_vllm_model is unchanged from main.
  • Benchmark results: no effect.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification.
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass).
  • New source files include the required Apache-2.0 SPDX header.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

…starting

With base_url set, LocalVLLMModel skips its own vLLM launch and reported
ready immediately, even if the endpoint was still loading: a load balancer
in front of vLLM replicas that are still starting answers 503, so the first
rollouts failed against it. Block startup until every base_url answers
GET /models with HTTP 200, for at most external_server_ready_timeout_s
(default 3600 s; None waits indefinitely), then fail with a clear
TimeoutError.

The local-launch wait (await_server_ready) and the new external wait now
share one helper, wait_for_models_endpoint, so both require HTTP 200, use a
per-probe request timeout, and treat any request exception as "not ready".
The local path keeps its fail-fast check on the vLLM actor, now a
RuntimeError instead of an assert. GenRMModel inherits the behaviour.

Signed-off-by: sniverty <[email protected]>
@snivertynv snivertynv self-assigned this Oct 6, 2026
@copy-pr-bot

copy-pr-bot Bot commented Oct 6, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ananthsub

Copy link
Copy Markdown
Contributor

/ok to test 2eed8ee

@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 6, 2026 16:34
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

@yaoyu-33 yaoyu-33 added area:model Model servers, inference providers, and model adapters bug Something isn't working complexity:low Localized change in one scope with a small, straightforward review surface needs-review PR is ready for code review and waiting on a reviewer labels Oct 6, 2026
if is_alive is not None and not is_alive():
raise RuntimeError(f"{server_name} vLLM server spinup failed, see the error logs above!")
try:
if requests.get(url=f"{url}/models", timeout=request_timeout_s).status_code == 200:

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.

This probe sends GET /models without an Authorization header, and only HTTP 200 counts as ready.
An external vLLM server started with --api-key, or any OpenAI-compatible endpoint that requires a key, answers 401 to this request every time.
Startup then blocks for the full timeout and fails with did not return HTTP 200, even though the endpoint is up and the configured api_key would work.
Before this change, the same config started immediately and served requests normally.

I reproduced this with a small HTTP server that returns 401 unless it sees Authorization: Bearer <key>.
With timeout_s=4, wait_for_models_endpoint raised TimeoutError after 4 seconds.

The existing command-line probe avoids this on purpose.
The docstring of _probe_endpoint in nemo_gym/cli/env.py says that requiring a 200 would reject endpoints that need auth.

Send the configured key with the probe:

headers = {"Authorization": f"Bearer {api_key}"} if api_key else None
requests.get(url=f"{url}/models", headers=headers, timeout=request_timeout_s)

Please also add a test where the endpoint returns 401 without the header and 200 with it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Understood, thanks for clarifying. I've removed wait_for_models_endpoint for now. I will look at adding a test that checks if a server answers 401 unless it gets Authorization: Bearer .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added test_an_endpoint_that_requires_a_key_does_not_hold_startup.

# backend yet answers 503). Do not report ready until it serves, so no
# request can reach it early.
base_urls = self.config.base_url if isinstance(self.config.base_url, list) else [self.config.base_url]
wait_for_models_endpoint(

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.

Gym already waits for model endpoints after the Gym servers start, and that check is where this gap should be fixed.

RunHelper.wait_for_server_readiness calls wait_for_model_endpoints in nemo_gym/cli/env.py.
That function probes every base_url in the config, including a local_vllm_model with base_url set.
It misses the load balancer case only because _probe_endpoint counts any HTTP response as answering, and 503 is a response.

Placing the wait here instead has three side effects:

  • Plain vllm_model with an external base_url has the same 503 problem and is not covered by this change.
  • The same URL is checked twice with different rules: here it needs a 200 within 3600 seconds, then the head process accepts any response within 600 seconds.
  • While this wait runs, the Gym model server has not opened its port yet. A slow judge pool therefore shows up as a Gym server that failed to start.
flowchart TD
    A["gym env start"] --> B["Gym model server process runs setup_webserver"]
    B --> C["This PR: wait_for_models_endpoint blocks until GET /models returns 200"]
    C -->|"up to 3600 s"| D["Gym model server opens its port"]
    A --> E["Head process: wait_for_spinup polls Gym server ports"]
    E -->|"port still closed after 600 s"| F["RuntimeError: Timed out waiting for Gym servers"]
    D --> E
    E -->|"all ports open"| G["wait_for_model_endpoints probes every base_url"]
    G -->|"any HTTP response, including 503"| H["Head marks the run ready"]
Loading

Treat 502, 503 and 504 as "still starting" in _probe_endpoint, and drop the per-server wait and config field.
That covers every model type and keeps 401 and 404 counting as reachable.
It also reuses the existing model_endpoint_readiness_timeout_seconds key.

Does the NeMo-RL integration start Gym servers through RunHelper.start()?
If it uses another path that skips wait_for_server_readiness, a server-side wait may still be needed.
In that case the API key and timeout comments on this PR still apply.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ack, fixing.

# With base_url set (an external server, e.g. a load balancer in front of vLLM
# replicas that may still be loading), startup blocks until every URL answers
# GET /models with HTTP 200, for at most this many seconds. None waits indefinitely.
external_server_ready_timeout_s: Optional[float] = 3600.0

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.

The 3600-second default and None cannot take effect under gym env start.
This wait runs inside setup_webserver(), before the Gym model server opens its port.
Meanwhile, the head process waits for every Gym server port with server_spinup_timeout_seconds, which defaults to 600 seconds.

Consider a judge pool that takes 15 minutes to load.
After 10 minutes the head process raises Timed out after 600s waiting for Gym servers to become ready and shuts the run down.
That message points at dependency installation and local model download, so the user goes looking in the wrong place.
Setting this field to None does not change the outcome.

If this wait stays in the model server, document that server_spinup_timeout_seconds must be at least this value.
Also name both settings in the TimeoutError message.

RuntimeError: ``is_alive`` returned False.
TimeoutError: A URL was not ready within ``timeout_s``.
"""
deadline = None if timeout_s is None else monotonic() + timeout_s

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.

timeout_s=0 makes the deadline the current time, so the first probe that is not a 200 raises TimeoutError immediately.
Gym's other startup limits use 0 differently.
server_spinup_timeout_seconds: 0 waits without a limit, and model_endpoint_readiness_timeout_seconds: 0 skips the check.
A user who copies that convention and sets external_server_ready_timeout_s: 0 will see startup fail instead of wait.

Make 0 (or any value at or below 0) mean "no limit" here, matching server_spinup_timeout_seconds.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ack, should be fixed now.

poll_count += 1
sleep(3)
wait_for_models_endpoint(
[self.config.base_url[0]],

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.

The local launch path now requires HTTP 200 and passes no timeout_s.
Before this change, any HTTP response ended the wait.

If a config sets api_key in vllm_serve_kwargs, the local vLLM server answers this probe with 401.
The Ray actor stays alive, so the is_alive check never fires, and startup waits forever.
No checked-in config does this today, but the old code started fine in that setup.

Sending the configured key with the probe, as requested on the external path, fixes this path too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ack, fixing

A load balancer in front of vLLM replicas that are still loading answers
503 (502 if its backends refuse connections), which the model-endpoint
startup check counted as "answering", so the first rollouts could reach a
judge with no healthy backend. Treat 502, 503 and 504 as "still starting"
and wait on them like a refused connection, within the existing
model_endpoint_readiness_timeout_seconds bound. 401, 404 and other
responses still count as answering, so endpoints that need auth are
unaffected.

Revert the per-server wait in local_vllm_model from the previous commit:
the startup check already covers external base_urls, and the wait ran
before the server port opened, so it was cut short by
server_spinup_timeout_seconds, required HTTP 200 without the API key, and
blocked the local launch path on endpoints that answer 401.

Signed-off-by: sniverty <[email protected]>
snivertynv added a commit to NVIDIA-NeMo/RL that referenced this pull request Oct 7, 2026
… check

NVIDIA-NeMo/Gym#4108 now holds rollouts in Gym's model-endpoint startup
check, which treats a load balancer's 503 (no healthy backend yet) as still
starting, instead of in a per-server wait in local_vllm_model. Point the
EXTERNAL_VLLM_EARLY_RAY README row and launcher comment at that check, and
document its model_endpoint_readiness_timeout_seconds bound (set under
env.nemo_gym; 0 skips the check).

Signed-off-by: sniverty <[email protected]>
@snivertynv snivertynv changed the title fix(local_vllm_model): Wait for an external base_url to serve before starting fix(cli): Treat 502/503/504 from a model endpoint as still starting Oct 7, 2026
A server started with --api-key answers the probe, which sends no key,
with 401. Cover that the startup wait treats this as answering and moves
on after one probe instead of waiting until the timeout.

Signed-off-by: sniverty <[email protected]>
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Oct 7, 2026
Comment thread nemo_gym/cli/env.py Outdated
"""
try:
requests.get(_endpoint_probe_url(base_url), timeout=timeout_seconds)
response = requests.get(_endpoint_probe_url(base_url), timeout=timeout_seconds)

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.

This probe goes through requests, which honours HTTP_PROXY, HTTPS_PROXY and NO_PROXY from the environment.
The rollout traffic it stands in for does not: Gym's shared aiohttp ClientSession (server_utils.py) leaves trust_env at its default of False, so model requests always connect directly.

Before this change, a probe that went through a proxy always passed, so the mismatch was harmless.
Now consider a cluster that exports HTTP_PROXY for internet access, with an internal http:// judge host missing from NO_PROXY.
The proxy cannot reach the judge and answers 502 or 503, so startup waits the full model_endpoint_readiness_timeout_seconds and then fails, even though the direct path that rollouts use would work.
The proxy also hides DNS failures, so a placeholder hostname is waited on for 600 seconds instead of being reported once.

Probing without the environment proxy keeps the check on the same path as the traffic:

Suggested change
response = requests.get(_endpoint_probe_url(base_url), timeout=timeout_seconds)
with requests.Session() as session:
# Model requests go through Gym's aiohttp client, which ignores proxy environment
# variables, so the probe must not route through a proxy either.
session.trust_env = False
response = session.get(_endpoint_probe_url(base_url), timeout=timeout_seconds)

A test that sets HTTP_PROXY to an address that refuses connections and expects a local endpoint to still count as answering would pin it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Fixed in 1e59dbb: _probe_endpoint now uses a requests.Session() with trust_env = False, so it ignores HTTP_PROXY/HTTPS_PROXY/ALL_PROXY and connects directly, matching Gym's aiohttp client.

Added test_proxy_environment_variables_are_ignored test. It runs a real local HTTP server, points all proxy variables at a dead port and clears NO_PROXY, and checks that the probe still reports answering. I confirmed the test fails without the change (it gets refused).

Comment thread nemo_gym/cli/env.py

Nothing accepted a connection there. The server was never started, is still starting up, or the URL
in your config does not match where it is listening.
Nothing accepted a connection there, or only a proxy or load balancer answered with 502, 503 or 504.

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.

The troubleshooting page still describes the old rule, and the PR checklist marks documentation as updated.
fern/versions/latest/pages/troubleshooting/configuration.mdx needs three edits:

  • The sample error at L261-L262 still shows the old first sentence of this message.
  • L274 says "Any response, including 401 or 404, means something is listening." It should say any response other than 502, 503 or 504.
  • L281 says "Any HTTP answer counts as listening", which is no longer true.

A sentence in that Note explaining that a proxy or load balancer answering 502, 503 or 504 is waited on like a refused connection would also help.
The versioned copies under v0.5.x and v0.6.0 should stay as they are.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated fern/versions/latest/pages/troubleshooting/configuration.mdx in 1e59dbb:

  • the sample error message now matches the new text
  • "Any response other than 502, 503 or 504, including 401 or 404, means something is listening", plus a note that a gateway status usually comes from a proxy or load balancer whose backend is still starting
  • the Note now says 502/503/504 are waited on and that the check ignores proxy variables

Comment on lines +198 to +199
def keyed_server(url, headers=None, **kwargs):
authorized = (headers or {}).get("Authorization") == "Bearer secret-key"

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.

Small nit: the probe never sends an Authorization header, so the 200 branch of keyed_server can never run, and the docstring's "and 200 with it" describes a case the test does not exercise.
The test is still a useful guard that a 401 does not hold startup.
Returning 401 directly would say exactly that:

Suggested change
def keyed_server(url, headers=None, **kwargs):
authorized = (headers or {}).get("Authorization") == "Bearer secret-key"
def keyed_server(url, **kwargs):
return MagicMock(status_code=401)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 1e59dbb. The fake server now returns 401 unconditionally.

…/504

Model requests go through Gym's aiohttp client, which ignores proxy
environment variables, but the readiness probe used requests, which
honours them. With a proxy set, a local endpoint came back as a proxy
error or a 502/504 and, now that those mean "still starting", was waited
on until the timeout. Probe with a session that has trust_env disabled.

Update the troubleshooting page for the new rule (502/503/504 are waited
on, the probe bypasses proxies), and simplify the 401 test: the probe
never sends a key, so the fake server's 200 branch could not run.

Signed-off-by: sniverty <[email protected]>
@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 1e59dbb

@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 197296a

@snivertynv

Copy link
Copy Markdown
Contributor Author

/ok to test 197296a

@snivertynv
snivertynv added this pull request to the merge queue Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/37795415612

Merged via the queue into main with commit 0b27f0e Oct 8, 2026
44 checks passed
@snivertynv
snivertynv deleted the sniverty/local-vllm-external-ready branch October 8, 2026 15:06
JeffPengCoder added a commit to JeffPengCoder/Gym that referenced this pull request Oct 10, 2026
No maintained WebVoyager profile enables global_aiohttp_client_trust_env;
only the retired BrowserGym route used it. It also contradicted the model
endpoint readiness probe (NVIDIA-NeMo#4108), which connects directly because Gym's
aiohttp client ignores proxy variables, so an endpoint reachable only through
a proxy would have been reported unreachable. The visual browser keeps its own
per-context Playwright proxy.

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

This branch was previously deployed

1 inactive deployment
public — 197296a8 Deployed Oct 8, 2026 by copy-pr-bot[bot] via release / finalize / notify #4701
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters bug Something isn't working complexity:low Localized change in one scope with a small, straightforward review surface needs-review PR is ready for code review and waiting on a reviewer sla:review-overdue Review response is over the one-business-day SLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants