Skip to content

fix(core): bound internal retries by the retry cap - #3914

Merged
athitten merged 8 commits into
mainfrom
athitten/internal-retry-cap
Oct 12, 2026
Merged

athitten merged 8 commits into
mainfrom
athitten/internal-retry-cap

Conversation

@athitten

@athitten athitten commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Follow-up to #3632, split out as requested in #3632 (comment).

In _request_with_retries(), the generic except Exception branch handles every error except ServerDisconnectedError and ClientOSError, which have their own branches. This includes aiohttp timeouts. When the caller does not pass _max_num_tries, this branch counted tries only for external callers. So an internal caller (_internal=True) that set _max_connection_retries to 2 or more never reached the cap and retried forever.

#3415 already counts tries for callers that pass _max_num_tries. This PR fixes the remaining path, where _max_num_tries is not passed.

  • Count tries for internal callers too, so _max_connection_retries stops them.
  • Keep everything else the same:
    • internal callers with no cap still retry with no limit and print no retry message, as Comp coding fixes; lots of misc infra items #90 intended;
    • external callers still stop at MAX_NUM_TRIES;
    • callers that pass _max_num_tries are unchanged.
  • Update the comment that said internal calls are not counted. It keeps Comp coding fixes; lots of misc infra items #90's reason for not limiting internal calls.
  • Add three tests:
    • the cap stops internal callers, with tracing off and on;
    • internal callers with no cap retry silently past MAX_NUM_TRIES;
    • external callers stop at MAX_NUM_TRIES and print one retry message per try.
  • The cap test, test_request_retry_cap_bounds_internal_callers, replaces test_retry_cap_bounds_internal_callers, which was removed from feat(core): expose aiohttp pool wait telemetry #3632.
  • Make the retry tests fail instead of hang when the loop never stops:
    • The test helper _mock_global_client (added in feat: judge endpoint resiliency #2383) now takes the error to raise.
    • Its fake request() calls pytest.fail() once it is called more often than any test needs. pytest.fail() raises a BaseException, which the loop's except Exception does not catch. So the test fails at once, even if the loop never sleeps.
    • Two feat(core): add model retry controls and Gymnasium rollout options #3415 tests now use this helper instead of their own mocks: test_explicit_transport_attempt_cap_disables_request_retry and test_request_caps_generic_errors_at_the_lower_attempt_limit. Before, if the generic branch ignored _max_num_tries, the first test hung, sleeping a real 0.5 s per retry.

No caller in this repo sets both _internal=True and _max_connection_retries today:

  • The internal callers pass no cap: ServerClient, the simple agent's direct tool calls to Resources servers (tool_access), the Terminus 2 sandboxed agent's model client (NeMoGymAsyncOpenAI(internal=True)), the OpenHands capture route, and the LiteLLM version check.
  • The callers with a cap are not internal: the remote agent and sandbox transport use 1, Tavily search uses 0, and vLLM with endpoint_file uses a default of 8.
  • The OpenAI model server and the Gymnasium agent pass _max_num_tries only when upstream_max_num_tries or model_server_transport_max_attempts is set. Neither passes _max_connection_retries, so neither changes.

Related:

Validation

All runs used an activated venv from uv sync --extra dev --extra sandbox --extra openshell --extra telemetry --extra mlflow --extra wandb (the extras in full-test-suite.yml). Every installed package matches uv.lock (192 packages, including aiohttp 3.14.4, nemo-lens 0.2.0+b0f977d, and pytest 9.0.3).

  • pytest tests/unit_tests/test_server_utils.py responses_api_agents/remote_agent/tests/test_app.py tests/unit_tests/telemetry/: 465 passed.
  • With the old code restored, test_request_retry_cap_bounds_internal_callers fails in under a second with Failed: retry loop did not stop instead of hanging. It fails the same way when the retry sleep is also removed.
  • Each change below makes at least one test fail. Each run of test_server_utils.py took about 3 s, and none hung:
    • the cap is ignored;
    • internal callers are limited by MAX_NUM_TRIES;
    • internal callers print;
    • external tries are double-counted;
    • external callers stop printing the retry message;
    • the MAX_NUM_TRIES raise is removed;
    • the retry sleep after a generic error is skipped;
    • the cap check ignores _max_num_tries attempts;
    • the generic branch ignores _max_num_tries;
    • the traced path drops _internal or _max_connection_retries.
  • pytest -n 16 tests/unit_tests responses_api_agents/remote_agent/tests: 8089 passed, 5 skipped. The per-test results match main at a3cf403 (8085 passed, 5 skipped). The 4 extra results are the new tests, with the cap test run twice (tracing off and on).
  • The same focused tests pass on a local merge with the latest main (feat(core): run Gym without Ray installed #4300), which changes no package versions.
  • pre-commit run --all-files: passed.

Rollouts

N/A. This changes a retry counter in the shared HTTP helper. No caller in this repo changes behavior, and no model, agent, environment, or verifier logic is involved.

Compatibility and benchmark impact

None for callers in this repo. A caller that sets _internal=True and _max_connection_retries to 2 or more, without _max_num_tries, now stops after that many tries on errors that reach the generic except Exception branch, including timeouts, instead of retrying forever. This includes code outside this repo that uses NeMoGymAsyncOpenAI(internal=True, max_connection_retries=N). ServerDisconnectedError and ClientOSError keep their own count, 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: internal helper behavior with no user-facing docs.
  • 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. N/A: no new files.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

@athitten athitten added area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working labels Oct 2, 2026
@copy-pr-bot

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

@athitten

Copy link
Copy Markdown
Contributor Author

/review

@athitten

Copy link
Copy Markdown
Contributor Author

/claude review

@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 reliability concerns. The retry branch now counts internal generic-error attempts for the explicit connection cap while preserving silent uncapped internal retries, the external default limit, and _max_num_tries behavior.

The regression tests cover tracing on/off and the neighboring retry behaviors, with a bounded mock that prevents runaway retry tests.

Static review only; tests were not executed.

Review generated by codex

@claude

claude Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

SHIP — no reliability concerns.

Reviewed the retry-branch refactor in _request_with_retries (generic except Exception path) and its tests.

What changed / why it's correct: Moving num_tries += 1 out of the if not _internal: guard (while keeping it inside the _max_num_tries is None branch) fixes a real bug for internal callers. Before, num_tries was pinned at 1 for internal callers on a generic exception, so attempts = explicit_tries if _max_num_tries is not None else num_tries never grew and the _max_connection_retries cap at line 660 could never fire — a passed cap was silently ignored and internal callers retried a generic exception forever. Now num_tries advances, so _max_connection_retries is honored for internal callers. The print and the unconditional MAX_NUM_TRIES ceiling remain gated on not _internal, so internal callers without a cap still retry indefinitely (behavior preserved), and external-caller behavior is entirely unchanged.

Tests: The three new cases trace exactly against the code — ..._retry_cap_bounds_internal_callers (cap=5 → 5 attempts, 4 sleeps), ..._internal_callers_without_a_cap_retry_past_max_num_tries (6 calls, empty stdout), ..._external_callers_stop_at_max_num_tries (3 calls, 3 prints). The _mock_global_client refactor adds a fail()-based runaway guard whose BaseException escapes the loop's except Exception, converting any infinite-retry regression into a fast test failure rather than a hang. Good defensive design.

Nit (NOTE, author's call): the comment at lines 667-669 now says internal calls "still count tries so _max_connection_retries applies" — accurate, but note it's num_tries/attempts doing the counting on this path, not the retries counter used by the ClientOSError/ServerDisconnectedError branches; the wording reads fine as-is.

@athitten

Copy link
Copy Markdown
Contributor Author

/review

@athitten

Copy link
Copy Markdown
Contributor Author

/ok to test c14c70d

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

The retry-branch change advances num_tries for internal generic errors, allowing the explicit connection cap to terminate retries. Uncapped internal calls remain silent and unbounded; external default limits, explicit _max_num_tries behavior, and connection-error branches are preserved. Traced and untraced forwarding retain the retry parameters.

The regression tests check the capped internal path with tracing on/off, uncapped internal recovery, and external attempt limits. The bounded mock prevents runaway retry tests without masking the expected exceptions.

Static review only; tests were not executed.

Review generated by codex

@athitten

Copy link
Copy Markdown
Contributor Author

/ok to test 5387291

@athitten

Copy link
Copy Markdown
Contributor Author

/ok to test 5387291

@athitten
athitten marked this pull request as ready for review October 12, 2026 22:35
@athitten
athitten added this pull request to the merge queue Oct 12, 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/38320500691

Merged via the queue into main with commit 91d8c6c Oct 12, 2026
49 of 51 checks passed
@athitten
athitten deleted the athitten/internal-retry-cap branch October 12, 2026 23:24
khazic pushed a commit to khazic/Gym-lao that referenced this pull request Oct 13, 2026
…DIA-NeMo#4365)

## What does this PR do?

`VLLMModel._post_init` builds one `NeMoGymAsyncOpenAI` client per
`base_url` and passes `endpoint_connection_retries` as the client's
`max_connection_retries` only when `endpoint_file` is configured. On
`main` the condition reads `self.config.endpoint_connection_retries if
self.config.endpoint_file else None`
(`responses_api_models/vllm_model/app.py:466-468` on `main`). A
deployment that enables `route_around_failing_endpoints` over a static
`base_url` list has no endpoint file, so its clients are built with
`max_connection_retries=None`.

`None` means retry forever. `NeMoGymAsyncOpenAI` forwards the value as
`_max_connection_retries` on every request
(`nemo_gym/openai_utils.py:1378-1384` and
`nemo_gym/openai_utils.py:1468`), and in `_request_with_retries` the
`except ClientOSError` branch, which a refused connection reaches as
`aiohttp.ClientConnectorError`, re-raises only when
`_max_connection_retries` is set (`nemo_gym/server_utils.py:638-654`).
Otherwise it sleeps 0.5 s and loops.

The endpoint health accounting that NVIDIA-NeMo#3979 added depends on the call
raising. `_call_endpoint` awaits the engine call and counts a
`ClientConnectionError` against the endpoint only in its `except` branch
(`responses_api_models/vllm_model/app.py:1835-1853`), and
`_note_endpoint_failure` moves sessions off the endpoint once
`endpoint_failure_threshold` consecutive calls have failed
(`responses_api_models/vllm_model/app.py:1855-1873`). With an unbounded
client the call never returns from the retry loop, the endpoint never
records a failure, and every session pinned to a dead endpoint hangs on
it for the rest of the run. The feature cannot react to the failure mode
it was built for.

The change:

- `responses_api_models/vllm_model/app.py:469-473`: the clients receive
`endpoint_connection_retries` when `endpoint_file` or
`route_around_failing_endpoints` is set. A static list without either
setting still gets `None`, and the endpoint-file rebind path already
passed the bound unconditionally
(`responses_api_models/vllm_model/app.py:1756`).
- `responses_api_models/vllm_model/app.py:338-342`: the comment on
`endpoint_connection_retries` states the condition and why an unbounded
client defeats the route-around.
- `responses_api_models/vllm_model/tests/test_app.py:6116-6135`: a new
test builds the model over a two-URL static list with
`route_around_failing_endpoints=True`, no endpoint file and
`endpoint_connection_retries=3`, and checks that both clients carry
`max_connection_retries == 3`. The same list without either setting
yields `None` for both clients. The existing assertion at
`responses_api_models/vllm_model/tests/test_app.py:6114` already pinned
the single-URL default; only its comment changed, because "static
clients retry forever" now holds only without route-around.

## Issue

- NVIDIA-NeMo#3979 added `route_around_failing_endpoints`. This PR fixes its
static-URL configuration.
- NVIDIA-NeMo#2216 reports that the `ServerDisconnectedError` and `ClientOSError`
branches of `_request_with_retries` never compare their counter against
`MAX_NUM_TRIES`, so a permanently unreachable server loops forever. That
unbounded loop is what hides a dead endpoint here. This PR does not
change the loop; it makes `vllm_model` pass the per-client bound that
the loop does honor.
- NVIDIA-NeMo#3914 makes an explicit `_max_connection_retries` cap stop internal
callers in the generic `except Exception` branch of the same loop. The
`vllm_model` clients are external callers (`internal=False`), so this PR
does not depend on it; both concern the same cap.
- Draft NVIDIA-NeMo#4262 changes `_resolve_client` in the same file. This PR does
not touch those lines, so the two compose.

## Validation

- `pytest -q -p no:cacheprovider -o addopts=
responses_api_models/vllm_model/tests/test_app.py` with the repository
root on `PYTHONPATH` (Python 3.13.14, pytest 9.1.1, aiohttp 3.14.3): 254
passed, 0 failed. The same command on `main` at f128d43 gives 253
passed; the difference is the new test.
- The new test fails against the `main` version of
`responses_api_models/vllm_model/app.py` with `[None, None] == [3, 3]`.
- `pytest -q -p no:cacheprovider -o addopts=
responses_api_models/vllm_model/tests/` (the whole directory, three test
files): 406 passed, 0 failed.
- Unit suite (`pytest tests/unit_tests`, with
`test_collect_downstream_junit.py` excluded for a missing local
package): 7752 passed, 56 skipped, 17 failed, 5 errors; the 22 failures
are the same 22 as on `main` in this environment (telemetry,
opensandbox, and local-vLLM tests that need packages this machine
lacks). Each other `responses_api_models/<server>/tests` directory, run
as its own session, matches `main`.
- `ruff check` and `ruff format --check` on both changed files: clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: alexandery <[email protected]>
Co-authored-by: Claude Fable 5.1 <[email protected]>

This branch was previously deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants