Skip to content

test/connect: fix sporadic "Address already in use" failures - #1617

Merged
axboe merged 1 commit into
axboe:masterfrom
Flowdalic:connect
Jul 22, 2026
Merged

test/connect: fix sporadic "Address already in use" failures#1617
axboe merged 1 commit into
axboe:masterfrom
Flowdalic:connect

Conversation

@Flowdalic

Copy link
Copy Markdown
Contributor

The connect.t test previously generated a pseudo-random port number using rand(). This approach occasionally resulted in port collisions when the randomly selected port was already in use by another process or lingering in a TIME_WAIT state, causing the test suite to sporadically fail with "bind(): Address already in use".

Update the tests in connect.t to bind sockets using sin_port=0, allowing the operating system to assign an available ephemeral port. Afterwards, getsockname() is used to retrieve the assigned port so that the client sockets can be configured to connect to it.

For test_connect_with_no_peer(), we introduce a new helper get_free_port(). This test explicitly requires connecting to a port where no server is listening to verify that an ECONNREFUSED error is returned. get_free_port() binds a temporary socket to port 0 to acquire an unused port from the OS, then immediately closes it. This increases the chances of the test to connect to an empty port.

Remove the port randomization code in test() as it is now unused.

The connect.t test previously generated a pseudo-random port number
using rand(). This approach occasionally resulted in port collisions
when the randomly selected port was already in use by another process
or lingering in a TIME_WAIT state, causing the test suite to
sporadically fail with "bind(): Address already in use".

Update the tests in connect.t to bind sockets using sin_port=0,
allowing the operating system to assign an available ephemeral
port. Afterwards, getsockname() is used to retrieve the assigned port
so that the client sockets can be configured to connect to it.

For test_connect_with_no_peer(), we introduce a new helper
block_port(). This test explicitly requires connecting to a port where
no server is listening to verify that an ECONNREFUSED error is
returned. block_port() binds a temporary socket to port 0 to acquire
an unused port from the OS, and keeps the file descriptor open to
prevent other processes from claiming the port before and during the
test run. Because listen() is never called on this socket, connecting
to it is guaranteed to be refused. After test_connect_with_no_peer()
completed the socket is closed.

Remove the port randomization code in test() as it is now unused.

Signed-off-by: Florian Schmaus <flo@geekplace.eu>
@axboe
axboe merged commit 14f0c69 into axboe:master Jul 22, 2026
20 checks passed
@Flowdalic
Flowdalic deleted the connect branch July 22, 2026 15:08
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.

2 participants