Repository navigation
fix(server): build the server once per uvicorn worker - #5681
Merged
Merged
Conversation
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]>
svcnvidia-nemo-ci
left a comment
Contributor
There was a problem hiding this comment.
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.
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]>
yaoyu-33
approved these changes
Nov 9, 2026
7 tasks
8 of 26 tasks
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-levelapp. 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 withnum_workers=Ntherefore ranmodel_post_initandsetup_webserver2N+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_entrypointnow 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 assimple_agent,math_with_judge,example_session_state_mgmtand 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 onmainwithout 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_runtimepre-commit run --all-filesRollouts
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
is_nemo_gym_fastapi_entrypointexplains the behavior, and no user-facing setting changes.pre-commit run --all-files) (so CI lint/format checks pass).git commit -s) (so the DCO check passes).