Skip to content

[serve] Run HAProxy drain/undrain test without SO_REUSEPORT - #64216

Merged
eicherseiji merged 6 commits into
ray-project:masterfrom
eicherseiji:serve-reuseport-default
Jun 23, 2026
Merged

eicherseiji merged 6 commits into
ray-project:masterfrom
eicherseiji:serve-reuseport-default

Conversation

@eicherseiji

@eicherseiji eicherseiji commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Why

test_drain_and_undrain_haproxy_manager set SERVE_SOCKET_REUSE_PORT_ENABLED=1 so the per-node HAProxies could share *:8000 on the single test host. The goal is to run the Serve test suite with SO_REUSEPORT disabled, which is the default. Without SO_REUSEPORT, proxies colocated on one machine need distinct ports to coexist.

What

Replace the controller-side TEST_WORKER_NODE_HTTP_PORT and TEST_WORKER_NODE_GRPC_PORT scaffolding with a per-worker-node override read in the proxy actor itself.

  • Add RAY_SERVE_WORKER_PROXY_HTTP_PORT and RAY_SERVE_WORKER_PROXY_GRPC_PORT. A worker proxy binds these instead of the configured http_options.port and grpc_options.port. The head node is exempt, so it keeps its configured port and the head-resident fallback proxy keeps its own port.
  • Apply the override through apply_per_node_port_overrides in both ProxyActor and HAProxyManager, so it covers the native and HAProxy paths.
  • Read the value from each node's own environment so distinct workers get distinct ports through cluster.add_node(env_vars=...). The old scaffolding read one value on the controller and applied it to every worker.

The drain test now runs with SO_REUSEPORT disabled. The head keeps :8000, the two workers bind :8001 and :8002, and each worker gets its own stats and metrics ports. test_head_and_worker_nodes_no_replicas and test_grpc_proxy_on_draining_nodes move to the same per-node env vars.

Limitations

The override changes only the proxy's local bind port. The controller still advertises every proxy at http_options.port through get_targets, so the override is invisible to controller-generated routing. It is safe only when a client reaches the proxy frontend directly, which is what these tests do.

Testing

RAY_SERVE_ENABLE_HA_PROXY=1 pytest python/ray/serve/tests/test_haproxy.py::test_drain_and_undrain_haproxy_manager passes with SO_REUSEPORT disabled.

test_drain_and_undrain_haproxy_manager forced
SERVE_SOCKET_REUSE_PORT_ENABLED=1 so the three per-node HAProxies could
share *:8000 on the single test host. Each HAProxy also binds fixed stats
(8404) and metrics (9101) ports, so SO_REUSEPORT was the only thing letting
the co-located processes coexist.

Give the per-node HAProxies distinct ports instead: the worker frontend via
TEST_WORKER_NODE_HTTP_PORT and the worker stats/metrics ports via per-node
env vars passed to add_node. The test now runs with SO_REUSEPORT disabled
(the default) and no SERVE_SOCKET_REUSE_PORT_ENABLED override, with no
application-code changes.

The cluster is head + one worker: the frontend port has no per-node knob
beyond the uniform TEST_WORKER_NODE_HTTP_PORT, so two workers cannot get
distinct frontends. The test asserts the drain/undrain outcome (the worker
proxy is removed when its node loses all replicas and re-added healthy when
they return). The HEALTHY/DRAINING/DRAINED state machine is covered by
tests/unit/test_proxy_state.py.

Signed-off-by: Seiji Eicher <[email protected]>

@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 refactors the HAProxy draining test in test_haproxy.py to simplify its structure, removing the use of async/threading and assigning distinct ports to the head and worker HAProxies to avoid port collision without relying on SO_REUSEPORT. The review feedback recommends keeping the draining period short by setting RAY_SERVE_PROXY_MIN_DRAINING_PERIOD_S to prevent test timeouts in CI, and safely handling serve_details.proxies in case it is None during transient states to avoid potential AttributeError failures.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread python/ray/serve/tests/test_haproxy.py Resolved
Comment thread python/ray/serve/tests/test_haproxy.py Resolved
@eicherseiji eicherseiji self-assigned this Jun 18, 2026
The default RAY_SERVE_PROXY_MIN_DRAINING_PERIOD_S is 30s, leaving a thin
margin under the 40s drain-completion wait. Set it to 1s so the worker proxy
drains quickly. Addresses review feedback.

Signed-off-by: Seiji Eicher <[email protected]>
Restore the two-worker drain/undrain test so it still asserts the
transient {HEALTHY: 2, DRAINING: 1} proxy state, with SO_REUSEPORT
disabled.

Without SO_REUSEPORT the three co-located HAProxies need distinct
frontend ports. The worker HTTP port was only settable via a single
global TEST_WORKER_NODE_HTTP_PORT read in the controller, so every
worker got the same port. Read it in the proxy actor from the proxy's
own node env (gated to non-head), mirroring how the stats/metrics ports
already resolve per node, so each worker binds a distinct frontend port
via add_node(env_vars=...).

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji eicherseiji added the go add ONLY when ready to merge, run all tests label Jun 22, 2026
…RPC}_PORT

Replace the controller-side TEST_WORKER_NODE_{HTTP,GRPC}_PORT reads in
ProxyStateManager._start_proxy with a single per-node knob,
RAY_SERVE_PROXY_HTTP_PORT / RAY_SERVE_PROXY_GRPC_PORT, read in the proxy
actor itself. The controller read the head node's env and applied one
value to every worker, so co-located worker proxies could not bind
distinct ports. The per-node read lets each proxy bind its own port,
mirroring the existing RAY_SERVE_HAPROXY_STATS_PORT / _METRICS_PORT knobs.

Migrate test_cluster and test_proxy to set the port per node via
add_node(env_vars=...).

Signed-off-by: Seiji Eicher <[email protected]>
RAY_SERVE_PROXY_HTTP_PORT / RAY_SERVE_PROXY_GRPC_PORT were read only in
ProxyActor.__init__, but the HAProxy feature uses HAProxyManager (a sibling of
ProxyActor selected at controller.py), which never read them. The override was
therefore a no-op for test_drain_and_undrain_haproxy_manager, whose module is
skipped unless RAY_SERVE_ENABLE_HA_PROXY: all three HAProxy frontends bound
port 8000 and the co-located workers could not bind without SO_REUSEPORT.

Extract apply_per_node_port_overrides() and call it from both ProxyActor and
HAProxyManager. Restore the head-node exemption so the head proxy and the
head-resident fallback proxy keep their configured ports. Drop the stale
override narration from _start_proxy's docstring.

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji
eicherseiji marked this pull request as ready for review June 22, 2026 22:09
@eicherseiji
eicherseiji requested a review from a team as a code owner June 22, 2026 22:09
@ray-gardener ray-gardener Bot added the serve Ray Serve Related Issue label Jun 23, 2026
Comment thread python/ray/serve/_private/constants.py Resolved
The per-node proxy port override only applies to worker-node proxies. The
head node is exempt so it keeps the configured http_options.port and the
head-resident fallback proxy keeps its own port.

Rename RAY_SERVE_PROXY_{HTTP,GRPC}_PORT to RAY_SERVE_WORKER_PROXY_{HTTP,GRPC}_PORT
to make the worker-only scope clear, and reword the constant comment to point at
http_options.port as the default knob.

Signed-off-by: Seiji Eicher <[email protected]>
@eicherseiji
eicherseiji enabled auto-merge (squash) June 23, 2026 20:09
@eicherseiji
eicherseiji merged commit 99b849a into ray-project:master Jun 23, 2026
4 checks passed
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…ect#64216)

## Why

`test_drain_and_undrain_haproxy_manager` set
`SERVE_SOCKET_REUSE_PORT_ENABLED=1` so the per-node HAProxies could
share `*:8000` on the single test host. The goal is to run the Serve
test suite with SO_REUSEPORT disabled, which is the default. Without
SO_REUSEPORT, proxies colocated on one machine need distinct ports to
coexist.

## What

Replace the controller-side `TEST_WORKER_NODE_HTTP_PORT` and
`TEST_WORKER_NODE_GRPC_PORT` scaffolding with a per-worker-node override
read in the proxy actor itself.

- Add `RAY_SERVE_WORKER_PROXY_HTTP_PORT` and
`RAY_SERVE_WORKER_PROXY_GRPC_PORT`. A worker proxy binds these instead
of the configured `http_options.port` and `grpc_options.port`. The head
node is exempt, so it keeps its configured port and the head-resident
fallback proxy keeps its own port.
- Apply the override through `apply_per_node_port_overrides` in both
`ProxyActor` and `HAProxyManager`, so it covers the native and HAProxy
paths.
- Read the value from each node's own environment so distinct workers
get distinct ports through `cluster.add_node(env_vars=...)`. The old
scaffolding read one value on the controller and applied it to every
worker.

The drain test now runs with SO_REUSEPORT disabled. The head keeps
`:8000`, the two workers bind `:8001` and `:8002`, and each worker gets
its own stats and metrics ports.
`test_head_and_worker_nodes_no_replicas` and
`test_grpc_proxy_on_draining_nodes` move to the same per-node env vars.

## Limitations

The override changes only the proxy's local bind port. The controller
still advertises every proxy at `http_options.port` through
`get_targets`, so the override is invisible to controller-generated
routing. It is safe only when a client reaches the proxy frontend
directly, which is what these tests do.

## Testing

`RAY_SERVE_ENABLE_HA_PROXY=1 pytest
python/ray/serve/tests/test_haproxy.py::test_drain_and_undrain_haproxy_manager`
passes with SO_REUSEPORT disabled.

---------

Signed-off-by: Seiji Eicher <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ect#64216)

## Why

`test_drain_and_undrain_haproxy_manager` set
`SERVE_SOCKET_REUSE_PORT_ENABLED=1` so the per-node HAProxies could
share `*:8000` on the single test host. The goal is to run the Serve
test suite with SO_REUSEPORT disabled, which is the default. Without
SO_REUSEPORT, proxies colocated on one machine need distinct ports to
coexist.

## What

Replace the controller-side `TEST_WORKER_NODE_HTTP_PORT` and
`TEST_WORKER_NODE_GRPC_PORT` scaffolding with a per-worker-node override
read in the proxy actor itself.

- Add `RAY_SERVE_WORKER_PROXY_HTTP_PORT` and
`RAY_SERVE_WORKER_PROXY_GRPC_PORT`. A worker proxy binds these instead
of the configured `http_options.port` and `grpc_options.port`. The head
node is exempt, so it keeps its configured port and the head-resident
fallback proxy keeps its own port.
- Apply the override through `apply_per_node_port_overrides` in both
`ProxyActor` and `HAProxyManager`, so it covers the native and HAProxy
paths.
- Read the value from each node's own environment so distinct workers
get distinct ports through `cluster.add_node(env_vars=...)`. The old
scaffolding read one value on the controller and applied it to every
worker.

The drain test now runs with SO_REUSEPORT disabled. The head keeps
`:8000`, the two workers bind `:8001` and `:8002`, and each worker gets
its own stats and metrics ports.
`test_head_and_worker_nodes_no_replicas` and
`test_grpc_proxy_on_draining_nodes` move to the same per-node env vars.

## Limitations

The override changes only the proxy's local bind port. The controller
still advertises every proxy at `http_options.port` through
`get_targets`, so the override is invisible to controller-generated
routing. It is safe only when a client reaches the proxy frontend
directly, which is what these tests do.

## Testing

`RAY_SERVE_ENABLE_HA_PROXY=1 pytest
python/ray/serve/tests/test_haproxy.py::test_drain_and_undrain_haproxy_manager`
passes with SO_REUSEPORT disabled.

---------

Signed-off-by: Seiji Eicher <[email protected]>
Signed-off-by: 400Ping <[email protected]>
abrarsheikh pushed a commit that referenced this pull request Nov 16, 2026
## Description

`start_grpc_server(..., enable_so_reuseport=False)` did not turn
SO_REUSEPORT off. It passed the option as the string `"0"`, and gRPC
reads `grpc.so_reuseport` as an integer and ignores a string, which
leaves its default (on). Direct ingress replicas ask for `False` so that
a port another process listens on fails to bind and Serve allocates
another. Instead they could share a port with another process, and the
kernel split the port's connections between them. In #66815 about 8% of
a deployment's requests failed with `UNIMPLEMENTED` this way.

This PR passes the option as an integer and adds a test: a second server
binds a port in use only with `enable_so_reuseport=True`.

Behavior changes only where `enable_so_reuseport=False` is passed:
direct ingress replica servers, and Serve's proxies when
`SERVE_SOCKET_REUSE_PORT_ENABLED=0`. Proxies keep SO_REUSEPORT on by
default, as before.

## Related issues

Fixes #66815

## Additional information

- Not a duplicate: I searched open and closed issues and PRs for
`so_reuseport` and direct ingress ports. #64216 (HAProxy drain test
without SO_REUSEPORT), #64623 and #64544 deal with other port behavior.
- Tests, on Linux with Ray 3.0.0.dev0 (6703877, whose `grpc_util.py`
and test file match master) and grpcio 1.75.0, loading this PR's
`grpc_util.py` in place of the installed one:
- `python -m pytest -v python/ray/serve/tests/unit/test_grpc_util.py -k
so_reuseport`: 2 passed. Without the fix,
`test_start_grpc_server_so_reuseport[False]` fails with `DID NOT RAISE
RuntimeError`.
- `python -m pytest -q python/ray/serve/tests/unit/test_grpc_util.py`:
18 passed.
- black 22.10.0 and ruff 0.8.4 (the pre-commit versions) pass on both
files.
- The test is skipped off Linux, where SO_REUSEPORT semantics differ.
- AI assistance (Claude Code) was used to find the cause and prepare
this change.

Signed-off-by: xyuzh <[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.

3 participants