Commit 6085c1f
Fix v1 endpoint port pre-probe race (bind 0.0.0.0:0 + readback) (#1513)
* Fix v1 endpoint port pre-probe race (bind 0.0.0.0:0 + readback)
Endpoint pre-allocated its interception-server port at construction via
get_free_port(), which binds a throwaway socket to 127.0.0.1:0, reads the
number, and closes it. InterceptionServer then bound that cached port on
0.0.0.0 at the first rollout. That left two defects on the shared v1 path:
- the port was unreserved between Endpoint construction and the first
rollout (a TOCTOU race — another port consumer can take it), and
- it was validated on 127.0.0.1 but bound on 0.0.0.0, so a port free on
loopback could already be taken on another interface and the real bind
could collide.
Fix: stop pre-probing. Hand InterceptionServer port 0 so it binds
0.0.0.0:0 and adopts the OS-assigned port via the getsockname() readback
that already existed in InterceptionServer.start() (previously dead code
on the v1 path). Endpoint now reads server.port directly; it is 0 only in
the construct->start() window, which the rollout path never observes
(register_rollout calls start() before building any URL). Probe and bind
become the same held operation on the same interface.
Explicit Endpoint(port=...) construction is unchanged. This matches the
bind-and-readback pattern already used by cli_agent_env and rlm_env.
Verified: ruff clean; test_v1_endpoint_protocols.py and
test_interception_utils.py (33 tests) pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* remove unnecessary comment
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent fdbfe49 commit 6085c1f
1 file changed
Lines changed: 4 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
34 | 33 | | |
35 | 34 | | |
36 | 35 | | |
| |||
136 | 135 | | |
137 | 136 | | |
138 | 137 | | |
139 | | - | |
140 | 138 | | |
141 | 139 | | |
142 | 140 | | |
143 | | - | |
| 141 | + | |
| 142 | + | |
144 | 143 | | |
145 | 144 | | |
146 | 145 | | |
| |||
261 | 260 | | |
262 | 261 | | |
263 | 262 | | |
264 | | - | |
| 263 | + | |
265 | 264 | | |
266 | 265 | | |
267 | 266 | | |
| |||
282 | 281 | | |
283 | 282 | | |
284 | 283 | | |
285 | | - | |
| 284 | + | |
286 | 285 | | |
287 | 286 | | |
288 | 287 | | |
| |||
0 commit comments