Skip to content

fix(server): build the server once per uvicorn worker - #5681

Merged
ananthsub merged 2 commits into
mainfrom
ananthsub/entrypoint-skip-mp-main
Nov 9, 2026
Merged

ananthsub merged 2 commits into
mainfrom
ananthsub/entrypoint-skip-mp-main

Conversation

@ananthsub

@ananthsub ananthsub commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

With num_workers > 1, uvicorn starts each worker with the spawn method. Spawn first re-runs the parent's main script under the name __mp_main__, and only then does uvicorn import the entrypoint by module name and serve its module-level app. Gym starts a server by running its entrypoint as the main script, so the __mp_main__ copy is the entrypoint file itself.

Servers that support several workers end their entrypoint with elif is_nemo_gym_fastapi_entrypoint(__file__): app = X.run_webserver(). That check is true in the __mp_main__ copy too, so each worker built a full server and app that nothing serves, and then built a second one when uvicorn imported the entrypoint. A server with num_workers=N therefore ran model_post_init and setup_webserver 2N+1 times instead of N+1, and it held its startup data twice in every worker.

The extra build also runs before uvicorn starts the thread that answers its worker health check. A server whose build takes longer than timeout_worker_healthcheck (30 seconds by default) has its workers killed and restarted in a loop, even though the build uvicorn actually serves would have finished in time. The __mp_main__ copy still runs the entrypoint's module-level imports before that thread starts, so very slow imports still count against the timeout. This change removes only the build.

is_nemo_gym_fastapi_entrypoint now returns False when the module calling it is __mp_main__. The adapter line in each server stays unchanged.

This affects every server that already has the adapter on main, such as simple_agent, math_with_judge, example_session_state_mgmt and the environment servers. #4790 adds the adapter to most other servers, so this should land first.

No issue: this came up in the review of #4790.

Validation

  • pytest tests/unit_tests/utils/test_server_process.py: one test runs a fake entrypoint as a script and starts a spawned worker that imports it by module name, the way uvicorn does, and checks that only the import builds the server. If that worker hangs, the fake entrypoint stops it after 60 seconds and fails with a message. The other checks the result for an importing module and for __mp_main__. Both fail on main without this change.
  • NEMO_GYM_MULTIWORKER_E2E=1 RAY_TMPDIR=/tmp pytest tests/e2e/session_routing, which starts real servers with two uvicorn workers and routes sessions across them.
  • pytest -n 8 tests/unit_tests/utils tests/unit_tests/server_runtime
  • pre-commit run --all-files
  • Made the spawned worker in the fake entrypoint hang: the entrypoint exits with an error after the join timeout and no worker process is left running.

Rollouts

N/A: no environment, agent or verifier behavior changes. The change only stops an unused server build at worker startup, which the end-to-end tests above exercise with real multi-worker servers.

Compatibility and benchmark impact

None. Single-worker servers never reach the adapter. Multi-worker servers serve the same app and skip only the build nothing used.

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: the docstring of is_nemo_gym_fastapi_entrypoint explains the behavior, and no user-facing setting changes.
  • 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).

uvicorn starts each worker of a multi-worker server with the spawn method.
Spawn first re-runs the parent's main script as `__mp_main__`, and Gym's
main script is the server entrypoint itself. The module-level adapter
`elif is_nemo_gym_fastapi_entrypoint(__file__)` matched that copy too, so
each worker built a full server it never served before uvicorn imported
the entrypoint and built the one it serves. The extra build repeated every
startup side effect and ran before uvicorn's health-check thread starts,
so a build slower than `timeout_worker_healthcheck` got the worker killed
and restarted.

`is_nemo_gym_fastapi_entrypoint` now returns False when its caller is the
`__mp_main__` copy.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub ananthsub added bug Something isn't working area:core Shared APIs, servers, telemetry, health, and registries labels Nov 5, 2026
@nemo-automation-bot
nemo-automation-bot Bot requested review from a team November 5, 2026 17:44

@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 concerns found.

The entrypoint guard excludes the __mp_main__ caller while preserving the existing worker and path checks for ordinary imports. The regression tests assert both that only the imported module builds the server and that the positive entrypoint check remains valid.

I inspected both changed files and traced the affected adapters, exports, and launch paths. This was a static review; I didn't execute tests.

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.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added the needs-review PR is ready for code review and waiting on a reviewer label Nov 7, 2026
The fake entrypoint joined its spawned worker with a 60-second timeout
and then exited with the worker's exit code. When the worker hangs, that
code is None, so the script exited with status 0 and multiprocessing's
exit handler then waited for the worker anyway. Only the test's
120-second subprocess timeout stopped it, and that left the worker
running after the test.

Kill the worker when the join times out and exit with a message saying
so.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub merged commit 710938e into main Nov 9, 2026
67 checks passed
@ananthsub
ananthsub deleted the ananthsub/entrypoint-skip-mp-main branch November 9, 2026 20:32
@linear-code linear-code Bot mentioned this pull request Nov 12, 2026
8 of 26 tasks

This branch was previously deployed

1 inactive deployment
public — 5bb4c319 Deployed Nov 9, 2026 by copy-pr-bot[bot] via release / finalize / notify #11148
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants