Skip to content

fix(server-runtime): retry the head server on a fresh port when another Gym run already answers on its port - #6849

Merged
wprazuch merged 2 commits into
mainfrom
cwing/head-server-port-check
Nov 27, 2026
Merged

wprazuch merged 2 commits into
mainfrom
cwing/head-server-port-check

Conversation

@cwing-nvidia

@cwing-nvidia cwing-nvidia commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Bug. When two Gym runs on one machine use the same head server port (both on the default 11000, or the same ++head_server.port), the second run doesn't recover. It logs

ERROR:    [Errno 48] error while attempting to bind on address ('127.0.0.1', 11600): address already in use

(Errno 98 on Linux), starts its other servers anyway, and later dies with

RuntimeError: Head server finished unexpectedly!

Cause. Gym already handles a taken port: a server whose bind fails with EADDRINUSE is moved to a fresh port and every server is started again once (ServerSupervisor._move_to_a_fresh_port). That retry never runs for the head server here, because _wait_for_head_server only checks whether something answers HTTP on the head server's address, and checks it before checking the head server's thread. The other run's head server answers, so the wait returns "ready" although our head server's thread has already died on the bind. poll() notices later and raises a plain RuntimeError, which isn't retried.

Fix.

  • _wait_for_head_server counts the head server ready only once this process's own uvicorn server has started (uvicorn.Server.started), and reads its thread's liveness before trusting an answer. A taken port now ends the wait with the typed EADDRINUSE start failure that start() already retries on a fresh port.
  • While waiting, and in the spinup timeout, it says when another server answers on the head port ("another Gym run?"), instead of "not up yet (status success)" and "although it is running (last status success)".

How to reproduce

On one machine, from a Gym checkout with uv sync done. No model is needed: the bug shows before any model call (the placeholder endpoint is never reached).

  1. Terminal 1, start a first run and leave it running:

    gym env start --resources-server mcqa --model-type vllm_model \
      ++policy_base_url=http://127.0.0.1:9/v1 ++policy_api_key=x ++policy_model_name=m \
      ++head_server.port=11600

    Wait until it prints Waiting for servers to spin up.

  2. Terminal 2, start a second run with the same command.

On main: terminal 2 prints Waiting for head server to spin up, the address already in use error for port 11600, starts mcqa, mcqa_simple_agent, mcqa_environment_server and policy_model, and then exits with RuntimeError: Head server finished unexpectedly!.

With this PR: terminal 2 prints

`head_server` could not bind port 11600: address already in use. Starting every server again once, with `head_server` on port <another port>.

starts every server again, and goes on to Waiting for servers to spin up like the first run.

Stop both with Ctrl-C.

Validation

  • The reproduction above, on macOS arm64 and in a Linux (aarch64) container (python:3.13.14): fails on main as described, recovers on this branch.
  • Four new tests in tests/unit_tests/server_runtime/supervisor/test_head_server_start.py, each failing on main and passing here. One runs on real sockets: a foreign HTTP server holds and answers on the port, uvicorn fails to bind on its thread as HeadServer.run_webserver runs it, and the wait reports EADDRINUSE.
  • pytest tests/unit_tests/server_runtime/supervisor/: all pass except two existing tests in test_server_start_failed.py that hard-code Linux's errno 98 and fail on macOS (also failing on main, unrelated).
  • pre-commit run --from-ref origin/main --to-ref HEAD passes.

Not in scope

  • Non-head servers have a related, rarer gap: any HTTP answer on their port marks them ready, and they aren't polled again. Follow-up PR.
  • Clients started later (gym eval run --no-serve, gym env status) look for a moved head server on the configured port.

Rollouts

N/A: server startup only; no environment or agent behavior changes.

Compatibility and benchmark impact

None. Runs whose head server binds normally behave as before.

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. N/A: error and progress messages only.
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass). Run on the changed files.
  • New source files include the required Apache-2.0 SPDX header. N/A: no new files.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

When another Gym run held the head server's port, its head server answered the readiness poll while
ours had failed to bind. The wait returned, the existing move-to-a-fresh-port retry never ran, and the
run died later with "Head server finished unexpectedly!", the bind error buried in the log.

Count the head server ready only once this process's own uvicorn server has started, and check its
thread before trusting an answer, so a taken port becomes the typed EADDRINUSE start failure that
start() already retries on a fresh port. Say so while waiting, instead of "not up yet (status success)".

Signed-off-by: Chris Wing <[email protected]>
…up timeout; test the real path

Review follow-ups: the timeout said the head server "is running (last status success)" when it never
started and another server answered on its port; say that instead. Drop a redundant liveness re-check.
Add an end-to-end test on real sockets: a foreign HTTP server holds and answers on the port, uvicorn
fails to bind on its thread, and the wait reports EADDRINUSE.

Signed-off-by: Chris Wing <[email protected]>
@nemo-pr-automation
nemo-pr-automation Bot requested a review from a team November 26, 2026 22:33
@svcnvidia-nemo-ci svcnvidia-nemo-ci added risk:mid Autobot PR risk: mid bug Something isn't working area:core Shared APIs, servers, telemetry, health, and registries needs-review PR is ready for code review and waiting on a reviewer labels Nov 26, 2026
@cwing-nvidia cwing-nvidia changed the title fix(server-runtime): don't take another run's head server for ours fix(server-runtime): retry the head server on a fresh port when another Gym run already answers on its port Nov 26, 2026

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

SHIP

LGTM. No BLOCKER, RISK, or NOTE findings.

I reviewed both changed files and traced head-server creation, status polling, port reassignment, shutdown and the CLI/in-process callers. The readiness check now distinguishes this run's uvicorn instance from another server answering on the same port, without changing the existing one-retry policy.

Validation:

  • CI on d3cf9702b8d3eb22e9fbd45f726a1761d7825c59: lint, all four core-test shards, combined core coverage, all eight server-suite shards and both test aggregators passed. Changed-server tests and session-routing end-to-end were skipped, not passed.
  • The overall CI workflow is red. The optional openclaw and miniswe_sandboxed harness-conformance checks failed on both this head and the admitted base; they are separate from the passing test aggregators.
  • I ran leader/startup_integration.py against base and head using real head servers, HTTP clients and sockets. Base accepted the foreign server; head moved to a fresh port and served its own endpoint. Normal free-port startup passed on both revisions. Managed child-server spawning was omitted.
  • Focused delegated probes passed for startup/death interleavings and real successful startup with thread cleanup. All four new regression-test bodies failed against base for the expected reasons.

Local probes used 766267172432.dkr.ecr.us-east-1.amazonaws.com/gym:847e81f03fd364da697c53eca5bc9d35f9df3576, a successful default-branch CI image because this head has no container build. Its pyproject.toml and uv.lock match the reviewed head. I relied on CI for the existing suites rather than rerunning them. No model or GPU workloads ran.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@wprazuch
wprazuch added this pull request to the merge queue Nov 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 27, 2026
@wprazuch
wprazuch added this pull request to the merge queue Nov 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

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

Merged via the queue into main with commit 133c4cf Nov 27, 2026
83 of 87 checks passed
@wprazuch
wprazuch deleted the cwing/head-server-port-check branch November 27, 2026 10:16
@terrykong terrykong mentioned this pull request Dec 4, 2026
5 tasks done
terrykong added a commit to NVIDIA-NeMo/RL that referenced this pull request Dec 4, 2026
RL picks the Gym head-server port and writes it into Gym's config. If the
head server cannot bind it, Gym's RunHelper.start() moves it to a fresh
port in its global config and restarts every server, but RL kept
building its rollout client from the port it picked. With this Gym bump
(NVIDIA-NeMo/Gym#6849) that also happens when another Gym run already
answers on the port, so RL could send rollouts to the other run.

Read the port back from Gym's global config right after start(), before
the rollout client is built.

Test: a fake RunHelper.start() moves the head port; the head server
config and the rollout client must use the new port. It fails without
the fix (11000 != 11500).

Signed-off-by: Terry Kong <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer risk:mid Autobot PR risk: mid

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants