Skip to content

fix(websocket): use OS-assigned port to fix intermittent test failures - #365

Merged
0xB10C merged 2 commits into
peer-observer:masterfrom
deadmanoz:fix/websocket-port-race
Mar 11, 2026
Merged

0xB10C merged 2 commits into
peer-observer:masterfrom
deadmanoz:fix/websocket-port-race

Conversation

@deadmanoz

@deadmanoz deadmanoz commented Feb 24, 2026 •

Copy link
Copy Markdown
Collaborator

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 AtomicU16 with a random ephemeral start port, a retry loop on AddrInUse, 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 optional oneshot::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_clients filters 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.

@deadmanoz

Copy link
Copy Markdown
Collaborator Author

Reported flakiness uncovered by coverage: #366

Will investigate further

@deadmanoz
deadmanoz force-pushed the fix/websocket-port-race branch from 933d9cf to bc24b49 Compare February 25, 2026 04:09
@0xB10C

0xB10C commented Mar 3, 2026

Copy link
Copy Markdown
Collaborator

I re-triggered CI on the job where GitHub seemed like it broke.

@0xB10C

0xB10C commented Mar 5, 2026

Copy link
Copy Markdown
Collaborator

Triggered the CI again. It broke on test test_integration_websocket_p2p_message_ping has been running for over 60 seconds.

@deadmanoz

Copy link
Copy Markdown
Collaborator Author

Triggered the CI again. It broke on test test_integration_websocket_p2p_message_ping has been running for over 60 seconds.

Yeah weird... thanks!

@0xB10C

0xB10C commented Mar 6, 2026

Copy link
Copy Markdown
Collaborator

Triggered once more. Maybe rebasing this on top of master could be worthwhile as we have #360 merged now.

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.

@deadmanoz
deadmanoz force-pushed the fix/websocket-port-race branch from bc24b49 to d62a0f0 Compare March 9, 2026 05:58
@deadmanoz

Copy link
Copy Markdown
Collaborator Author

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 PIPELINE_READY:{nanos} marker event to NATS and waits for it to arrive on every websocket client. Once a client receives the marker, we know it's in the server's client map and the full NATS -> websocket server -> websocket client path is proven live. Any extra marker messages are drained before the real test events are published.

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.

@0xB10C

0xB10C commented Mar 9, 2026 •

Copy link
Copy Markdown
Collaborator

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.
@deadmanoz

deadmanoz commented Mar 11, 2026 •

Copy link
Copy Markdown
Collaborator Author

Rebased and conflicts resolved.

@deadmanoz
deadmanoz force-pushed the fix/websocket-port-race branch from d62a0f0 to a76dd87 Compare March 11, 2026 09:17
@0xB10C

0xB10C commented Mar 11, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

@0xB10C
0xB10C merged commit cbcc4c3 into peer-observer:master Mar 11, 2026
12 checks passed
deadmanoz added a commit to deadmanoz/peer-observer that referenced this pull request May 1, 2026
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.
deadmanoz added a commit to deadmanoz/peer-observer that referenced this pull request May 5, 2026
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.
deadmanoz added a commit to deadmanoz/peer-observer that referenced this pull request May 5, 2026
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.
deadmanoz added a commit to deadmanoz/peer-observer that referenced this pull request May 14, 2026
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.
0xB10C pushed a commit that referenced this pull request May 19, 2026
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.
@deadmanoz
deadmanoz deleted the fix/websocket-port-race branch May 28, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

p2p-extractor: in integration test, retry addconnection each time

2 participants