Fix(tests): replace hardcoded stratum ports with OS-assigned port 0 by nkatha23 · Pull Request #477 · braidpool/braidpool · GitHub
Skip to content

Fix(tests): replace hardcoded stratum ports with OS-assigned port 0#477

Merged
zaidmstrr merged 2 commits into
braidpool:devfrom
nkatha23:fix/test-hardcoded-ports
Jun 11, 2026
Merged

Fix(tests): replace hardcoded stratum ports with OS-assigned port 0#477
zaidmstrr merged 2 commits into
braidpool:devfrom
nkatha23:fix/test-hardcoded-ports

Conversation

@nkatha23

@nkatha23 nkatha23 commented Jun 8, 2026

Copy link
Copy Markdown

While reviewing PRs #309 and #474, I kept seeing test_invalid_json fail with AddrInUse on port 5050. Looking into it, all five stratum tests bind fixed ports (3353, 3356, 3357, 3358, 5050), if a previous test leaves a
server running or two tests race to bind the same port, the test fails with a misleading error that looks like a code bug rather than a port conflict.

The fix adds an optional oneshot channel parameter to run_stratum_service. After binding, the server sends its actual address back through the channel. Tests use port 0 so the OS assigns a free port, then await the receiver before connecting with no sleep, race windows, or hardcoded ports.

rpc_server.rs already uses the same pattern via server.local_addr(), so this is consistent with how the rest of the codebase handles this.

Production call in main.rs passes None, zero impact outside tests.

@zaidmstrr zaidmstrr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 17a64a7

Thanks for creating the PR. The problem seems to be exits. I haven't reviewed the code yet nor looked at the approach but will review it soon.

@Sansh2356

Copy link
Copy Markdown
Contributor

@zaidmstrr zaidmstrr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the PR, most of the things look good. But in the future we can perform a good refactor as a follow-up PR, which can enhance the code readability and provide a nicer architecture. By moving the TcpListener::bind call out of the run_stratum_service and into the caller. Here, the caller binds port 0 itself; the server function just accepts the ready listener, not a port.

Comment thread node/src/stratum.rs
nkatha23 added 2 commits June 10, 2026 21:19
Tests binding fixed ports (3353, 3356, 3357, 3358, 5050) caused AddrInUse failures when tests ran concurrently or a previous test left a server listening. This was a known flake documented across multiple PRs (braidpool#309, braidpool#474).

Add bound_addr_tx: Option<oneshot::Sender<SocketAddr>> to run_stratum_service. After binding, the actual address is sent
through the channel so tests can connect to the real port.

All 5 tests now use port 0 (OS-assigned) and await the oneshot receiver before connecting, no sleep needed, no race window.
Production call in main.rs passes None.
server_endpoints and the warning log were using self.stratum_config.port which shows 0 when OS port assignment is used. Switch to bound_addr.port()
so logs always show the real listening port.
@nkatha23 nkatha23 force-pushed the fix/test-hardcoded-ports branch from 17a64a7 to 4207ad8 Compare June 10, 2026 18:32

@zaidmstrr zaidmstrr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code ACK 4207ad8

Thanks for doing the changes. Now looks good to me.

@zaidmstrr zaidmstrr merged commit 86672f9 into braidpool:dev Jun 11, 2026
13 checks passed
@nkatha23 nkatha23 deleted the fix/test-hardcoded-ports branch June 11, 2026 06:39
nkatha23 added a commit to nkatha23/braidpool that referenced this pull request Jun 15, 2026
run_stratum_service now accepts a ready TcpListener instead of binding internally. The caller (main.rs or tests) binds the socket
and passes it in, removing the need for the oneshot channel workaround introduced in PR braidpool#477.

This separates concerns cleanly — the function runs the server,the caller controls how the socket is acquired. Consistent with how rpc_server.rs handles its server socket via local_addr().

Changes:
- run_stratum_service: listener: TcpListener replaces bound_addr_tx
- main.rs: binds stratum_listener before spawn, passes it in
- All 5 test sites: bind port 0, read local_addr() directly
- Remove oneshot from stratum.rs imports
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.

3 participants