Repository navigation
fix(p2p): use OS-assigned port to fix intermittent test failures - #358
Merged
0xB10C merged 1 commit intoFeb 13, 2026
Merged
Conversation
0xB10C
reviewed
Feb 13, 2026
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
force-pushed
the
fix/p2p-extractor-port-race
branch
from
February 13, 2026 08:53
11c706a to
4cff7bb
Compare
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. |
Collaborator
Author
Yeah no worries |
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.
This was referenced Apr 18, 2026
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.
This was referenced May 5, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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 optionaloneshot::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 viatokio::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 (changingfetch_addtoload), forcing every test to compete for a single port:sed -i 's/\.fetch_add(1, Ordering::SeqCst)/.load(Ordering::SeqCst)/' \ extractors/p2p/tests/integration.rsBoth phases ran the integration test suite 5 times with
--test-threads=4: