Skip to content

fix(core): close shared aiohttp clients on their owner loop - #4823

Merged
JeffPengCoder merged 3 commits into
NVIDIA-NeMo:mainfrom
JeffPengCoder:feature/aiohttp-owner-loop-lifecycle
Oct 28, 2026
Merged

JeffPengCoder merged 3 commits into
NVIDIA-NeMo:mainfrom
JeffPengCoder:feature/aiohttp-owner-loop-lifecycle

Conversation

@JeffPengCoder

Copy link
Copy Markdown
Collaborator

What does this PR do?

This splits the HTTP-lifecycle changes out of #3591, as suggested in its review.

  • Owner-loop close. At exit, the process-wide data client and the reserved control client (feat(checkpoint): add the participant control plane, episode steps, and coordination #3882) were closed with asyncio.run(), on a new event loop rather than the loop that created them. The owner loop is now recorded when the data client is created, and both clients are closed there:

    • the close is scheduled when the exit hook runs on the owner loop;
    • it is handed to the owner loop when that loop is running in another thread;
    • it is run to completion otherwise.

    close_global_aiohttp_client() gives async callers the same close.

  • Wildcard bind addresses. Gym clients now dial a server bound to 0.0.0.0 or :: at 127.0.0.1 or [::1]. Otherwise a proxy-aware HTTP client can send a request for 0.0.0.0 to an external proxy instead of the local server.

There is no separate issue: this is a split of #3591, and #3591 drops these hunks once this lands.

Validation

On macOS ARM64 with Python 3.13.14 and the dev and sandbox dependencies (aiohttp 3.14.4, yarl 1.25.1, FastAPI 0.135.2):

  • tests/unit_tests/test_server_utils.py: 197 passed. Four tests are new: closing on an idle owner loop, closing from the owner loop without blocking it, closing the control client with the data client, and dialing wildcard bind hosts through loopback.
  • The core unit suite plus the vLLM, simple-agent, compaction-agent and OSWorld tests, run on this branch and on its base, main 968e069c5: 10,889 passed here against 10,884 on main; the 4 new tests all pass. The remaining 27 failures are identical on main and are specific to this macOS machine.
  • pre-commit run on the changed files: passed.

Rollouts

N/A. No environment or agent behavior changes. This code was also part of #3591's head in the October 20 OSWorld reproduction (361 tasks, no degradation).

Compatibility and benchmark impact

  • Gym clients reach servers configured with a wildcard host through loopback; other hosts are unchanged.
  • New public helper close_global_aiohttp_client(). Benchmark impact: none.

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: internal lifecycle behavior.)
  • 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. (No new files.)
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

The process-wide data client and the reserved control client were closed at exit with asyncio.run(), i.e. on a new event loop rather than the loop that created them. Record the owner loop and close both clients there: schedule the close when called from the owner loop, hand it to the owner loop when that loop runs in another thread, and run it to completion otherwise. close_global_aiohttp_client() offers the same close to async callers.

Clients also stop dialing wildcard bind addresses: a server bound to 0.0.0.0 or :: is reached at 127.0.0.1 or [::1]. A proxy-aware HTTP client can otherwise send a request for 0.0.0.0 to an external proxy instead of the local server.

Split from NVIDIA-NeMo#3591.

Signed-off-by: Jeff Peng <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Oct 21, 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.

@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working labels Oct 21, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the needs-review PR is ready for code review and waiting on a reviewer label Oct 21, 2026
JeffPengCoder added a commit to JeffPengCoder/Gym that referenced this pull request Oct 21, 2026
…#4822 and NVIDIA-NeMo#4823

The sandbox-provider changes (Docker create reconciliation, E2B service endpoints and transport fallback, OpenSandbox endpoint compatibility, the e2b>=2.46.0 range) now live in NVIDIA-NeMo#4822, and the owner-loop close of the shared aiohttp clients with wildcard-host handling lives in NVIDIA-NeMo#4823. Restore those 16 files to this branch's main base so each change is reviewed in exactly one PR. Nothing else in this PR uses them; it keeps the component runtime policy, rollout identity and the vLLM transport-log context.

Signed-off-by: Jeff Peng <[email protected]>
zyzhou5 pushed a commit to zyzhou5/Gym that referenced this pull request Oct 21, 2026
…VIDIA-NeMo#3591)

* fix(core): isolate runtime, sandbox and model transport contracts

Extract shared support from JeffPengCoder/Gym 360b0df. Include E2B endpoint/transport and vLLM model changes; retain upstream GymAiohttpTransport while preserving the no-config Ray worker fallback. Keep OSWorld tasks, agents and resources out of this PR.

Signed-off-by: Jeff Peng <[email protected]>

* fix(e2b): align SDK installation guidance with gateway requirements

Signed-off-by: Jeff Peng <[email protected]>

* fix(cli): keep site-packages off server import paths in wheel installs

In a wheel install the built-in servers live in the parent interpreter's site-packages, so dir_path.parent.parent is that site-packages. Passing it as project_root put it first on each server's PYTHONPATH and NEMO_GYM_EXTRA_ROOTS, ahead of the server venv's own pinned dependencies. Apply the source-checkout guard the verifier runner already uses, and test both layouts; the wheel-install case fails without the guard.

Signed-off-by: Jeff Peng <[email protected]>

* fix(rollout): keep index-derived capture ids and gate dispatch logging

Rows without a caller-supplied _ng_rollout_id no longer receive a random rollout-<uuid4>. The capture key stays main's {task}-{rollout}, so a resumed attempt decodes to the same rollout as attempt 0 and existing capture files keep their names. A caller-supplied id is still forwarded unchanged. Rows are no longer mutated, so the per-dispatch deep copy is removed as well.

The per-dispatch summary is printed only when request debugging is enabled, like the reverification summary. The summary drops the NeMo-RL purpose and trajectory-identity fields and adds only the core _ng_rollout_id.

Signed-off-by: Jeff Peng <[email protected]>

* fix(vllm): drop the per-request purpose marker

The marker printed on every chat-completion request and ran before sampling overrides and capture/prefix preparation, so it could report temperature and top_p values the engine never received. The opt-in transport log (NEMO_GYM_VLLM_TRANSPORT_LOG) already records the final request body together with the rollout context headers. A test now checks that the log holds the post-override values and that nothing is printed per request.

Signed-off-by: Jeff Peng <[email protected]>

* chore(agent): drop the legacy compaction session change

Publishing the final policy decision only after guard admission changes nothing observable today: a guard rejection raises before build_response, the only place the decision is published. NVIDIA-NeMo#3368 deletes this session, and its replacement client checks guards before generation and acknowledges history only after accepting a response, so the invariant belongs there. Restore session.py and its test to main's versions; this also removes the only conflict with NVIDIA-NeMo#3368.

Signed-off-by: Jeff Peng <[email protected]>

* chore(core): leave provider and HTTP lifecycle changes to NVIDIA-NeMo#4822 and NVIDIA-NeMo#4823

The sandbox-provider changes (Docker create reconciliation, E2B service endpoints and transport fallback, OpenSandbox endpoint compatibility, the e2b>=2.46.0 range) now live in NVIDIA-NeMo#4822, and the owner-loop close of the shared aiohttp clients with wildcard-host handling lives in NVIDIA-NeMo#4823. Restore those 16 files to this branch's main base so each change is reviewed in exactly one PR. Nothing else in this PR uses them; it keeps the component runtime policy, rollout identity and the vLLM transport-log context.

Signed-off-by: Jeff Peng <[email protected]>

* fix(core): keep server venvs on the parent Python and fail loudly on pin conflicts

- A server's .python-version now selects its interpreter only when it excludes the
  parent's Python. A request that admits it (such as >=3.13.14,<3.14, 3.13 or
  [email protected]) builds the venv with the parent's exact version, because Ray rejects
  processes on different patch releases. Variant and pre-release requests
  (3.13+freethreaded, 3.13.14+debug, 3.13.14rc1) and requests for other implementations
  are passed to uv unchanged. Like uv, the first non-comment line is the request.
- Only the parent's constraints (the Ray pin) are passed as uv overrides, through process
  substitution like the existing -c flag. head_server_deps such as openai stay ordinary
  requirements, so a server pin that conflicts with them fails setup instead of being
  replaced silently.
- Drop the environment-identity file: nothing read it, so it could not detect a stale venv,
  and its tests passed whatever it contained.
- Replace the vacuous vllm_model marker-file test with one that checks the real policy, and
  document per-server .python-version/uv.toml and NEMO_GYM_ALLOWED_COMPONENT_ROOTS.

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

---------

Signed-off-by: Jeff Peng <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
Co-authored-by: Onur Yilmaz <[email protected]>
Co-authored-by: yaoyu-33 <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Oct 22, 2026
@yfw

yfw commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

/ok to test 18d7d51

@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 28, 2026 00:26
@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Oct 28, 2026
@JeffPengCoder

Copy link
Copy Markdown
Collaborator Author

/ok to test 9b3af48

@JeffPengCoder
JeffPengCoder added this pull request to the merge queue Oct 28, 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/40640889128

Merged via the queue into NVIDIA-NeMo:main with commit 9d6e0c2 Oct 28, 2026
46 of 49 checks passed
@JeffPengCoder
JeffPengCoder deleted the feature/aiohttp-owner-loop-lifecycle branch October 28, 2026 06:32

This branch was previously deployed

1 inactive deployment
public — 9b3af48d Deployed Oct 28, 2026 by copy-pr-bot[bot] via release / finalize / notify #7750
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