summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorMisaki Shioi <31817032+shioimm@users.noreply.github.com>2026-01-08 09:41:42 +0900
committerGitHub <noreply@github.com>2026-01-08 09:41:42 +0900
commit080d66beca71d6cc290a8be4acd49e5a70594f9c (patch)
tree50ddceff7602e3f667a82d9fce9b30eda43009ea /spec
parent950ffa90b7939885cc35d376a2e10aabfdc7170d (diff)
Avoid flaky test failures by retrying on local port conflicts (#15818)
This test obtains an available port number by calling `TCPServer.new`, then closes it and passes the same port number as `local_port` to `TCPSocket.new`. However, `TCPSocket.new` could occasionally fail with `Errno::EADDRINUSE` at the bind(2) step. I believe this happens when tests are run in parallel and another process on the same host happens to bind the same port in the short window between closing the `TCPServer` and calling `TCPSocket.new`. To address this race condition, the test now retries with a newly selected available port when such a conflict occurs.
Diffstat (limited to 'spec')
-rw-r--r--spec/ruby/library/socket/tcpsocket/shared/new.rb21
1 files changed, 15 insertions, 6 deletions
diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb
index 5280eb7900..0e405253c8 100644
--- a/spec/ruby/library/socket/tcpsocket/shared/new.rb
+++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb
@@ -53,14 +53,23 @@ describe :tcpsocket_new, shared: true do
end
it "connects to a server when passed local_host and local_port arguments" do
- server = TCPServer.new(SocketSpecs.hostname, 0)
+ retries = 0
+ max_retries = 3
+
begin
- available_port = server.addr[1]
- ensure
- server.close
+ retries += 1
+ server = TCPServer.new(SocketSpecs.hostname, 0)
+ begin
+ available_port = server.addr[1]
+ ensure
+ server.close
+ end
+ @socket = TCPSocket.send(@method, @hostname, @server.port,
+ @hostname, available_port)
+ rescue Errno::EADDRINUSE
+ raise if retries >= max_retries
+ retry
end
- @socket = TCPSocket.send(@method, @hostname, @server.port,
- @hostname, available_port)
@socket.should be_an_instance_of(TCPSocket)
end