Skip to content

test(functional): start a ServerProcess again when another process took its port - #555

Merged
pdevito3 merged 2 commits into
mainfrom
fix/server-process-port-race
Dec 4, 2026
Merged

pdevito3 merged 2 commits into
mainfrom
fix/server-process-port-race

Conversation

@pdevito3

@pdevito3 pdevito3 commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Why

CI on #553 failed a_worker_whose_worker_group_stops_on_a_fault_of_the_job_store_is_unhealthy with Failed to bind to address http://127.0.0.1:32785: address already in use. ServerProcess.FreePort closes its listener before the Server child binds the port, so another process can take it first. Port 32785 is at the bottom of the range that Docker gives to the host ports of test containers.

What changed

  • ServerProcess.StartAsync starts the Server again on a new port when the child stops with AddressInUseException, at most three times. A failed start is disposed before the next one.
  • After the child exits, the helper waits for the exit, so the output holds every line before it looks for AddressInUseException.

Tradeoffs

A child that binds port 0 and reports its address removes the race. It needs a log override for Microsoft.Hosting.Lifetime and a parse of two unnamed http://127.0.0.1:N lines (the http and the grpc endpoints), so the retry is the smaller fix.

Verification

  • A test hack bound (without listen) the first six ports that FreePort gave. On the old helper, 4 of 6 ServerRoleTests failed with The Server stopped with exit code 1. With the retry, 6 of 6 passed. Without the hack, 6 of 6 passed.
  • The last commit (wait for the exit) is not run locally, because the local Docker daemon stopped answering. CI runs the role tests.

…her process took its port

FreePort closes its listener before the Server binds the port, so another process, such as the host port of a test container, can take it first. The Server then stops with AddressInUseException and the role test fails. A start that fails to bind now runs again on a new port, at most three times.
@pdevito3

pdevito3 commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

review-gate: passed 5ccf75e

One independent thermo-nuclear review ran. It found no high or medium finding. It ran the built Server with its port held and confirmed that the JSON console output names AddressInUseException and that no process leaks between starts.

Open findings:

  • Low 1 (fixed after the gate in fbc1b5f): HasExited can be true before the readers get the last lines. The helper now waits for the exit first.
  • Low 2 (stays, from before this change): a foreign listener that accepts and never answers blocks the probe for the 100 s client timeout. The thief in CI was a Postgres container, which closes the connection, so the retry runs.
  • Low 3 (stays, from before this change): a foreign listener that answers 200 on /health makes a dead start look healthy. Unlikely; this change does not make it worse.
  • Low 4 (stays, from before this change): a Worker start that loses its port can claim a job and hold its lease for 60 s. It needs a port theft and a claim within milliseconds.

@pdevito3

pdevito3 commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

verifier: PASS fbc1b5f

Base: c43bb23 (main)

Hack (temporary, uncommitted, the same patch at base and head): StartAsync sets an AsyncLocal<StrongBox<bool>> flag, and FreePort() then binds a raw socket (bind, no listen) to the port it returns for the first start of each StartAsync call, and holds it. Kestrel in the child fails with AddressInUseException, and the health probe gets a fast refusal.

Command: dotnet run -- --treenode-filter "/*/*/ServerRoleTests/*" in tests/FullstackTemplate.FunctionalTests, after dotnet build (0 warnings, 0 errors).

Run Ports blocked Total Passed Failed
base + hack 7 6 1 5
head + hack 8 6 6 0
head, no hack, run 1 0 6 6 0
head, no hack, run 2 0 6 6 0

At base each of the 5 failures is "The Server stopped with exit code 1" with Failed to bind to address http://127.0.0.1:<port>: address already in use in the output, including a_worker_whose_worker_group_stops_on_a_fault_of_the_job_store_is_unhealthy. The 1 base pass is each_job_handler_resolves_without_the_entrypoints_as_in_a_worker, which starts no process. At head every start lost its first port and each test passed on the next port.

@pdevito3
pdevito3 merged commit c7172e1 into main Dec 4, 2026
16 checks passed
@pdevito3
pdevito3 deleted the fix/server-process-port-race branch December 4, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant