Files
ngit-grasp/tests
DanConwayDev 1c362df0cf test(harness): port reservation + retry to harden against :0 bind race
The naive find_free_port pattern — bind 127.0.0.1:0, read the port, drop
the listener, return — has a TOCTOU window where another caller in the
process can be handed the same port back before the consumer's real bind.
The hazard is especially sharp where a port is pre-allocated to embed in
an announcement event, then used minutes later to spawn the relay that
will host it (purgatory_sync, sync/historic_sync, sync/metrics,
archive_grasp_services, sync_helpers::run_sync_test_*).

Introduce tests/common/port.rs with PortReservation + reserve_port():
the listener stays bound for the lifetime of the reservation, so no
other reserve_port call in this process can be handed the same number.
TestRelay grows reservation-based constructors and routes every
existing public start* method through the same reservation path
internally; the bare find_free_port / start_on_port_* /
start_with_archive_and_sync / start_with_all_options API is removed
and all callers migrated.

Defense-in-depth retry on subprocess early-exit (detected via try_wait
during the readiness probe so we fail fast rather than waiting the
full 5s timeout) covers the residual microsecond-scale TOCTOU window
between PortReservation::release and ngit-grasp's own bind. Has not
been observed to fire in local stress runs; kept as belt-and-braces
for CI / loaded hardware.

Mirrors ngit commit 5883494 ("test(harness): port reservation + retry
to harden against :0 bind race").
2026-05-20 16:08:51 +00:00
..
2026-01-09 19:58:41 +00:00