Skip to content

fix(p2p): use OS-assigned port to fix intermittent test failures - #358

Merged
0xB10C merged 1 commit into
peer-observer:masterfrom
deadmanoz:fix/p2p-extractor-port-race
Feb 13, 2026
Merged

0xB10C merged 1 commit into
peer-observer:masterfrom
deadmanoz:fix/p2p-extractor-port-race

Conversation

@deadmanoz

Copy link
Copy Markdown
Collaborator

Summary

Use port 0 (OS-assigned) for the p2p-extractor TCP listener in integration tests instead of manually picking random ephemeral ports.

https://docs.rs/tokio/latest/tokio/net/struct.TcpListener.html#method.bind

"Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via the local_addr method."

This is race-free by design, there's no window between choosing a port and binding it where another process could grab it, so this eliminates the race condition that caused intermittent "Address already in use" failures (#316).

The run() function now accepts an optional oneshot::Sender<SocketAddr> that reports the actual bound address back to the caller. This also replaces the previous 2-second sleep with a proper synchronisation barrier via tokio::select!, making tests both faster and more reliable. If the extractor fails before binding (e.g. NATS connection error), the real error is surfaced instead of a generic channel-closed panic.

Fixes #316.

Testing

The fix was validated in my local testing setup with a before/after test script. To reliably reproduce the race condition on the old code, setup() was patched to return the same port for all parallel tests (changing fetch_add to load), forcing every test to compete for a single port:

sed -i 's/\.fetch_add(1, Ordering::SeqCst)/.load(Ordering::SeqCst)/' \
    extractors/p2p/tests/integration.rs

Both phases ran the integration test suite 5 times with --test-threads=4:

AddrInUse failures Other failures
Old code (patched, same port) 5/5 —
Fixed code (OS-assigned) 0/5 1/5 (split INV messages as per #357, another fix incoming!)

Comment thread extractors/p2p/tests/integration.rs Resolved
@0xB10C

0xB10C commented Feb 13, 2026 •

Copy link
Copy Markdown
Collaborator

This change seems very reasonable. Left a small nit. Otherwise ready to merge.

Use port 0 (OS-assigned) for the p2p-extractor TCP listener in
integration tests instead of manually picking random ephemeral ports.
This eliminates the TOCTOU race condition that caused intermittent
"Address already in use" failures (issue peer-observer#316).

The `run()` function now accepts an optional `oneshot::Sender` that
reports the actual bound address back to the caller. This also replaces
the previous 2-second sleep with a proper synchronization barrier,
making tests both faster and more reliable.
@0xB10C
0xB10C force-pushed the fix/p2p-extractor-port-race branch from 11c706a to 4cff7bb Compare February 13, 2026 08:53
@0xB10C

0xB10C commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator

Went ahead and squashed my nit fix into your commit and pushed to save on a review cycle. Once CI passes, I can merge.

@deadmanoz

Copy link
Copy Markdown
Collaborator Author

Went ahead and squashed my nit fix into your commit and pushed to save on a review cycle. Once CI passes, I can merge.

Yeah no worries

@0xB10C
0xB10C merged commit ecbc5ab into peer-observer:master Feb 13, 2026
10 checks passed
@0xB10C

0xB10C commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator

Another one down! Thanks @deadmanoz

deadmanoz added a commit to deadmanoz/peer-observer that referenced this pull request Feb 23, 2026
Use port 0 (OS-assigned) for the metrics server in integration tests
instead of manually picking random ephemeral ports. This eliminates
the race condition that caused intermittent "Address already in
use" failures (same pattern as the P2P extractor fix in peer-observer#358).

The metricserver::start() function now returns the actual bound
SocketAddr. The metrics::run() function accepts an optional
oneshot::Sender that reports the bound address back to the caller.
This also replaces the previous retry loop and 1-second sleep with a
proper synchronization barrier via tokio::select!, making tests both
faster and more reliable.
deadmanoz added a commit to deadmanoz/peer-observer that referenced this pull request Feb 23, 2026
Use port 0 (OS-assigned) for the metrics server in integration tests
instead of manually picking random ephemeral ports. This eliminates
the race condition that caused intermittent "Address already in
use" failures (same pattern as the P2P extractor fix in peer-observer#358).

The metricserver::start() function now returns the actual bound
SocketAddr. The metrics::run() function accepts an optional
oneshot::Sender that reports the bound address back to the caller.
This also replaces the previous retry loop and 1-second sleep with a
proper synchronization barrier via tokio::select!, making tests both
faster and more reliable.
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/p2p-extractor-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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

p2p-extractor: intermittent integration test failure: "Address already in use"

2 participants