Skip to content

[serve] Deflake test_metrics_2 - fix proxy shutdown - #66151

Merged
abrarsheikh merged 2 commits into
ray-project:masterfrom
johntaylor-cell:serve-proxy-shutdown-deflake
Sep 15, 2026
Merged

abrarsheikh merged 2 commits into
ray-project:masterfrom
johntaylor-cell:serve-proxy-shutdown-deflake

Conversation

@johntaylor-cell

@johntaylor-cell johntaylor-cell commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Why

test_metrics_2.py::TestProxyStateMetrics::test_proxy_shutdown_duration_metric is the top
flaker on the serve: HAProxy tests shard (premerge 73158 failed it on both attempts). It
fails as a 90s wait that never sees the series, with no exception, which is why two earlier
budget-widening deflakes did not help.

The test asserted the metric after serve.shutdown(), which races the controller against its
own exit: is_ready_for_shutdown observes the histogram, and Controller.shutdown calls
ray.kill on the controller in that same tick. A metric needs about one export interval to
reach the agent (measured with a standalone actor: 0.3s gap loses the sample, 1.0s lands it),
so once that window is missed no budget helps.

What covers the window is teardown work that outlives the proxy. The shard sets
RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S=0.01, which removes a 30s drain, and
controller shutdown then finishes in ~0.5s: measured 0.41s and 0.55s there, versus 2.04-2.59s
across 100 runs without the overlay. HAProxy alone is not the trigger; on its own it widens the
window to 30.4s.

What

  • Record the duration for every stopped proxy. It is only observed by is_ready_for_shutdown,
    which iterates _proxy_states, so a proxy stopped while the controller keeps running
    (drained node, unhealthy restart) was popped before anything polled it and never recorded.
  • Arm the shutdown start time once. The controller calls ProxyState.shutdown() every tick,
    re-arming it each time, so the histogram measured a tick (~20 samples of 3-6ms), not the
    shutdown.
  • Point the test at that repaired path: kill the proxy so the controller stops and replaces it
    while staying alive, and the export always has time.

Testing

5/5 in the HAProxy env that previously failed deterministically, 3/3 without HAProxy. Whole file:
11 passed / 3 skipped with the overlay, 14 passed without. tests/unit/test_proxy_state.py 38
passed, test_shutdown.py 14 passed. Each product fix has a regression test that fails when
that fix is reverted. The test costs ~25s more, spent detecting the killed proxy (3 health
checks at PROXY_HEALTH_CHECK_PERIOD_S=10), against a 90s budget, and the shard gets faster
since it no longer burns a 90s timeout.

The duration is only observed by is_ready_for_shutdown, which iterates
_proxy_states, so a proxy stopped while the controller keeps running (drained
node, unhealthy restart) was popped from that dict before anything polled it and
was never recorded at all. Stopped proxies are now held until their actor is
confirmed gone, and the manager counts them when deciding whether shutdown is
complete, so it no longer reports ready by having forgotten them.

On the serve.shutdown() path the value was recorded but wrong: the controller
calls ProxyState.shutdown() on every control-loop tick, which re-armed the start
time each time, so the histogram collected one sample per tick measuring that
tick rather than the shutdown. The start time is now armed once.

Signed-off-by: john.taylor <[email protected]>
test_proxy_shutdown_duration_metric asserted the metric after serve.shutdown(),
which races the controller against its own exit: the duration is recorded in the
same control-loop tick that calls ray.kill on the controller, and a metric needs
about one export interval to reach the agent. Widening the budget cannot help,
since once that window is missed nothing further is ever exported. Measured with
a standalone actor that observes a histogram and then kills itself: a 0.3s gap
loses the sample, 1.0s lands it.

The window is whatever teardown work outlives the proxy, which is why this fails
on the HAProxy shard and passes elsewhere. With that step env
(RAY_SERVE_ENABLE_HA_PROXY=1, direct ingress off,
RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S=0.01) controller shutdown finishes
in about 0.5s, measured 0.41s and 0.55s, against 2.04-2.59s across 100 runs
without the overlay.

Killing the proxy instead exercises the path where the controller stops and
replaces it while staying alive, so the export always has time. 5/5 in the
HAProxy env that previously failed deterministically, 3/3 without it, and the
whole file passes in both.

Signed-off-by: john.taylor <[email protected]>
@johntaylor-cell
johntaylor-cell requested a review from a team as a code owner September 14, 2026 00:56
@johntaylor-cell johntaylor-cell self-assigned this Sep 14, 2026
@johntaylor-cell johntaylor-cell added serve Ray Serve Related Issue go add ONLY when ready to merge, run all tests labels Sep 14, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request ensures that proxy shutdown duration metrics are correctly recorded and tracked when a proxy is stopped or replaced while the controller continues to run. It introduces a _stopping_proxy_states list to track proxies undergoing shutdown, prevents re-arming the shutdown start time on repeated shutdown calls, and updates the unit and integration tests to verify this behavior. I have no feedback to provide as there are no review comments.

qishengxhe added a commit to qishengxhe/ray that referenced this pull request Sep 14, 2026
Empty commit to retrigger premerge after a flaky
test_proxy_shutdown_duration_metric run (see ray-project#66151).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Signed-off-by: Qisheng He <[email protected]>
Comment on lines +1052 to +1059
# Kill the proxy so the controller stops it and starts a replacement. Going
# through serve.shutdown() races the controller against its own exit: it
# records the duration in the tick that kills itself, too late to export.
node_id = ray.get_runtime_context().get_node_id()
proxy = ray.get_actor(
format_actor_name(SERVE_PROXY_NAME, node_id), namespace=SERVE_NAMESPACE
)
ray.kill(proxy, no_restart=True)

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.

i think we should retain the serve.shutdown() and figure out a way to flush the metrics deterministically before shutting down controller?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to keep serve.shutdown(), there is no clean fix that does not introduce an API change, which is to expose the existing Ray Flush. This would change around 30 lines of production code.

Remembering how long it look my last ray-core change to land, I decided to also investigate a serve-only change that restores serve.shutdown, but there is none other than what is already in the PR. Keeping serve.shutdown requires a graceful controller exit, which never completes because the controller always has in-flight tasks that never finish, so the metric is lost anyway.

So, I recommend we merge the fix as-is to improve the tests flakiness and open a subsequent PR that adds the API, and after than, incorporate the API change into the test.

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.

ok, to merge, but file a GH issue to expose that API from core. @edoakes any objection to exposing that API?

johntaylor-cell added a commit to johntaylor-cell/ray that referenced this pull request Sep 15, 2026
The HAProxy shard is red on test_proxy_shutdown_duration_metric, a known
test_metrics_2 flake that ray-project#66151 fixes. This PR changes no target that runs on
that shard: the guarded block is gated behind a gang placement-group check and
test_metrics_2 has no gang deployments.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: john.taylor <[email protected]>
@abrarsheikh
abrarsheikh merged commit e92b8b1 into ray-project:master Sep 15, 2026
6 checks passed
rueian pushed a commit that referenced this pull request Sep 19, 2026
…#66180)

## Why

`DisconnectServices` already exports before exit via
`opencensus::stats::StatsExporter::ExportNow()`, but has no
OpenTelemetry equivalent, and OpenCensus is off by default. So anything
recorded since the last periodic push dies with the process. That hook
is on the force path too, since `ExecuteForceShutdown` calls it before
`QuickExit`, which is why Ray Serve lost
`ray_serve_proxy_shutdown_duration_ms` in #66151 (roughly a 40% pass
rate on the HAProxy shard).

## What

- `OpenTelemetryMetricRecorder::ForceFlush(timeout)`, the flush
`Shutdown()` already does, without the teardown.
- `ray::stats::FlushMetrics(timeout_ms)`, false when stats are disabled
or OpenTelemetry is off.
- Call it in `DisconnectServices` beside `ExportNow()`, bounded at 500ms
because `ForceFlush` blocks where `ExportNow` does not, and every worker
exit takes this path.

No caller changes needed, including in Serve.

## Testing

A metric recorded before a force kill now reaches the endpoint, with the
report interval set to 60s so only the flush can deliver it. C++ case
covers `ForceFlush` leaving the recorder usable. `core_worker_lib`,
`//:_raylet` and the stats and observability targets build clean;
recorder test 5/5.

Closes #66178.

---------

Signed-off-by: john.taylor <[email protected]>
Co-authored-by: Claude <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
## Why


`test_metrics_2.py::TestProxyStateMetrics::test_proxy_shutdown_duration_metric`
is the top
flaker on the `serve: HAProxy tests` shard (premerge 73158 failed it on
both attempts). It
fails as a 90s wait that never sees the series, with no exception, which
is why two earlier
budget-widening deflakes did not help.

The test asserted the metric after `serve.shutdown()`, which races the
controller against its
own exit: `is_ready_for_shutdown` observes the histogram, and
`Controller.shutdown` calls
`ray.kill` on the controller in that same tick. A metric needs about one
export interval to
reach the agent (measured with a standalone actor: 0.3s gap loses the
sample, 1.0s lands it),
so once that window is missed no budget helps.

What covers the window is teardown work that outlives the proxy. The
shard sets
`RAY_SERVE_DIRECT_INGRESS_MIN_DRAINING_PERIOD_S=0.01`, which removes a
30s drain, and
controller shutdown then finishes in ~0.5s: measured 0.41s and 0.55s
there, versus 2.04-2.59s
across 100 runs without the overlay. HAProxy alone is not the trigger;
on its own it widens the
window to 30.4s.

## What

- Record the duration for every stopped proxy. It is only observed by
`is_ready_for_shutdown`,
which iterates `_proxy_states`, so a proxy stopped while the controller
keeps running
(drained node, unhealthy restart) was popped before anything polled it
and never recorded.
- Arm the shutdown start time once. The controller calls
`ProxyState.shutdown()` every tick,
re-arming it each time, so the histogram measured a tick (~20 samples of
3-6ms), not the
  shutdown.
- Point the test at that repaired path: kill the proxy so the controller
stops and replaces it
  while staying alive, and the export always has time.

## Testing

5/5 in the HAProxy env that previously failed deterministically, 3/3
without HAProxy. Whole file:
11 passed / 3 skipped with the overlay, 14 passed without.
`tests/unit/test_proxy_state.py` 38
passed, `test_shutdown.py` 14 passed. Each product fix has a regression
test that fails when
that fix is reverted. The test costs ~25s more, spent detecting the
killed proxy (3 health
checks at `PROXY_HEALTH_CHECK_PERIOD_S=10`), against a 90s budget, and
the shard gets faster
since it no longer burns a 90s timeout.

---------

Signed-off-by: john.taylor <[email protected]>
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…ray-project#66180)

## Why

`DisconnectServices` already exports before exit via
`opencensus::stats::StatsExporter::ExportNow()`, but has no
OpenTelemetry equivalent, and OpenCensus is off by default. So anything
recorded since the last periodic push dies with the process. That hook
is on the force path too, since `ExecuteForceShutdown` calls it before
`QuickExit`, which is why Ray Serve lost
`ray_serve_proxy_shutdown_duration_ms` in ray-project#66151 (roughly a 40% pass
rate on the HAProxy shard).

## What

- `OpenTelemetryMetricRecorder::ForceFlush(timeout)`, the flush
`Shutdown()` already does, without the teardown.
- `ray::stats::FlushMetrics(timeout_ms)`, false when stats are disabled
or OpenTelemetry is off.
- Call it in `DisconnectServices` beside `ExportNow()`, bounded at 500ms
because `ForceFlush` blocks where `ExportNow` does not, and every worker
exit takes this path.

No caller changes needed, including in Serve.

## Testing

A metric recorded before a force kill now reaches the endpoint, with the
report interval set to 60s so only the flush can deliver it. C++ case
covers `ForceFlush` leaving the recorder usable. `core_worker_lib`,
`//:_raylet` and the stats and observability targets build clean;
recorder test 5/5.

Closes ray-project#66178.

---------

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

Labels

go add ONLY when ready to merge, run all tests serve Ray Serve Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants