Skip to content

Commit 2b1a9af

Browse files
authored
Fix: Do not pass negative timeout to Addrinfo#connect_internal (#15578)
This change fixes a bug where, with `Socket.tcp`’s `fast_fallback option` disabled, specifying `open_timeout` could unintentionally pass a negative value to `Addrinfo#connect_internal`, `causing an ArgumentError`. ``` ❯ ruby -rsocket -e 'p Socket.tcp("localhost", 9292, open_timeout: 1, fast_fallback: false)' /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:64:in 'IO#wait_writable': time interval must not be negative (ArgumentError) sock.wait_writable(timeout) or ^^^^^^^ from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:64:in 'Addrinfo#connect_internal' from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:141:in 'Addrinfo#connect' from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:964:in 'block in Socket.tcp_without_fast_fallback' from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:231:in 'Array#each' from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:231:in 'Addrinfo.foreach' from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:945:in 'Socket.tcp_without_fast_fallback' from /Users/misaki-shioi/src/install/lib/ruby/4.0.0+0/socket.rb:671:in 'Socket.tcp' from -e:1:in '<main>' ```
1 parent e42bcd7 commit 2b1a9af

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

ext/socket/lib/socket.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,14 @@ def self.tcp_without_fast_fallback(host, port, local_host, local_port, connect_t
948948
local_addr = nil
949949
end
950950
begin
951-
timeout = open_timeout ? open_timeout - (current_clock_time - starts_at) : connect_timeout
951+
timeout =
952+
if open_timeout
953+
t = open_timeout - (current_clock_time - starts_at)
954+
t.negative? ? 0 : t
955+
else
956+
connect_timeout
957+
end
958+
952959
sock = local_addr ?
953960
ai.connect_from(local_addr, timeout:) :
954961
ai.connect(timeout:)

0 commit comments

Comments
 (0)