Repository navigation
fix(websocket): use OS-assigned port to fix intermittent test failures - #365
Conversation
|
Reported flakiness uncovered by coverage: #366 Will investigate further |
933d9cf to
bc24b49
Compare
|
I re-triggered CI on the job where GitHub seemed like it broke. |
|
Triggered the CI again. It broke on |
Yeah weird... thanks! |
|
Triggered once more. Maybe rebasing this on top of If it times out again, it's probably something related to the not OS-port assigning getting stuck in a loop and we don't timeout. |
bc24b49 to
d62a0f0
Compare
|
Ok, I don't think it's not getting stuck in a loop, it's that the sequence of current events is fundamentally liable to race conditions under duress. I have applied a similar solution now to what I opted for on #372. That is, a pipeline readiness barrier. After connecting all websocket clients, the test publishes a unique This replaces the sleep-based sync, which, IMO, is non-deterministic and just masks any raciness issues. If the pipeline isn't ready, the test retries; if it can't become ready within 5 seconds, it panics with a message rather than hanging... Stress-tested 50 consecutive runs on a Linux x86_64 machine (NixOS, comparable to CI) with zero failures. For reference, the same machine reproduced the original hang on run 2/50 (without the barrier) and a timeout-based approach I was also looking at failed on 5/50. |
|
Green CI, on both my fork and here: |
|
With #364 merged, can you rebase this and resolve conflicts? Thanks! |
Replace AtomicU16/retry/sleep port selection with OS-assigned port (127.0.0.1:0) and oneshot channel to report the bound address back to tests. Eliminates port collision race and removes 1-second sleep. The run() function now accepts an optional oneshot::Sender<SocketAddr> that fires after the TCP listener binds, and nc.flush() ensures the NATS subscription is registered server-side before the channel fires.
After a client connects and sends its subscription JSON, there is a window where NATS events can arrive before the server registers the client in its broadcast map. Each client is inserted and subscribed independently, so one client receiving events does not prove the others are ready. Add a per-client readiness barrier that publishes a marker event through NATS and waits for it to arrive on each subscribing client's websocket stream, proving the full pipeline is live. The marker event type matches the client's subscription (messages or mempool) because broadcast_to_clients() filters by protobuf variant. A final drain pass flushes any cross-contamination markers between clients.
|
Rebased and conflicts resolved. |
d62a0f0 to
a76dd87
Compare
|
Thank you! |
Enable integration tests to bind the Prometheus port to :0 and discover the OS-assigned port, eliminating the TOCTOU window of the old "pick ephemeral port, drop listener, rebind" pattern. Matches the shape used by p2p_extractor::run (PR peer-observer#358) and websocket::run (PR peer-observer#365). shared::metricserver::start already resolves the listener's local_addr internally for logging. Return it through the signature (Result<(), io::Error> -> Result<SocketAddr, io::Error>) so callers can discover the bound port. Existing callers in tools/metrics and tools/connectivity-check use the result as a statement (? and .unwrap()) and compile unchanged since they simply discard the new Ok value. rpc_extractor::run gains an Option<oneshot::Sender<SocketAddr>> parameter and, after metricserver::start succeeds, forwards the resolved address through the channel if provided. main.rs passes None in production.
Enable integration tests to bind the Prometheus port to :0 and discover the OS-assigned port, eliminating the TOCTOU window of the old "pick ephemeral port, drop listener, rebind" pattern. Matches the shape used by p2p_extractor::run (PR peer-observer#358) and websocket::run (PR peer-observer#365). shared::metricserver::start already resolves the listener's local_addr internally for logging. Return it through the signature (Result<(), io::Error> -> Result<SocketAddr, io::Error>) so callers can discover the bound port. Existing callers in tools/metrics and tools/connectivity-check use the result as a statement (? and .unwrap()) and compile unchanged since they simply discard the new Ok value. rpc_extractor::run gains an Option<oneshot::Sender<SocketAddr>> parameter and, after metricserver::start succeeds, forwards the resolved address through the channel if provided. main.rs passes None in production.
Enable integration tests to bind the Prometheus port to :0 and discover the OS-assigned port, eliminating the TOCTOU window of the old "pick ephemeral port, drop listener, rebind" pattern. Matches the shape used by p2p_extractor::run (PR peer-observer#358) and websocket::run (PR peer-observer#365). shared::metricserver::start already resolves the listener's local_addr internally for logging. Return it through the signature (Result<(), io::Error> -> Result<SocketAddr, io::Error>) so callers can discover the bound port. Existing callers in tools/metrics and tools/connectivity-check use the result as a statement (? and .unwrap()) and compile unchanged since they simply discard the new Ok value. rpc_extractor::run gains an Option<oneshot::Sender<SocketAddr>> parameter and, after metricserver::start succeeds, forwards the resolved address through the channel if provided. main.rs passes None in production.
Enable integration tests to bind the Prometheus port to :0 and discover the OS-assigned port, eliminating the TOCTOU window of the old "pick ephemeral port, drop listener, rebind" pattern. Matches the shape used by p2p_extractor::run (PR peer-observer#358) and websocket::run (PR peer-observer#365). shared::metricserver::start already resolves the listener's local_addr internally for logging. Return it through the signature (Result<(), io::Error> -> Result<SocketAddr, io::Error>) so callers can discover the bound port. Existing callers in tools/metrics and tools/connectivity-check use the result as a statement (? and .unwrap()) and compile unchanged since they simply discard the new Ok value. rpc_extractor::run gains an Option<oneshot::Sender<SocketAddr>> parameter and, after metricserver::start succeeds, forwards the resolved address through the channel if provided. main.rs passes None in production.
Enable integration tests to bind the Prometheus port to :0 and discover the OS-assigned port, eliminating the TOCTOU window of the old "pick ephemeral port, drop listener, rebind" pattern. Matches the shape used by p2p_extractor::run (PR #358) and websocket::run (PR #365). shared::metricserver::start already resolves the listener's local_addr internally for logging. Return it through the signature (Result<(), io::Error> -> Result<SocketAddr, io::Error>) so callers can discover the bound port. Existing callers in tools/metrics and tools/connectivity-check use the result as a statement (? and .unwrap()) and compile unchanged since they simply discard the new Ok value. rpc_extractor::run gains an Option<oneshot::Sender<SocketAddr>> parameter and, after metricserver::start succeeds, forwards the resolved address through the channel if provided. main.rs passes None in production.
Summary
Use port 0 (OS-assigned) for the websocket TCP listener in integration tests instead of manually picking random ephemeral ports.
Same approach as the P2P extractor fix in #358. The old code used
AtomicU16with a random ephemeral start port, a retry loop onAddrInUse, and a 1-second sleep to wait for the server to start. This had the same race condition, a window between choosing a port and binding it where another process could grab it.The
run()function now accepts an optionaloneshot::Sender<SocketAddr>that reports the actual bound address back to the caller. Tests bind to port 0, receive the OS-assigned address via the oneshot, and connect clients immediately (no sleep, no race).This PR also adds a pipeline readiness barrier (as was introduced with #372) to eliminate the following race: after a client connects and sends its subscription JSON, there's a window where NATS events can arrive at the server before the client is registered in the broadcast map with its subscriptions applied.
As each client is inserted and subscribed independently, one client receiving events does not prove the others are ready. So the barrier publishes a marker event through NATS for every subscribing client and waits for it to arrive on that client's websocket stream, thus proving the full pipeline is live.
The marker event type matches the client's subscription (just messages or mempool for now, because only those were present in the tests) because
broadcast_to_clientsfilters by protobuf variant. A final drain pass flushes any cross-contamination markers between clients before real test events are published.Fixes #327.
Testing
All 6 integration tests pass a 50-run stress testing on Linux (NixOS x86_64), zero failures.