Skip to content

Support connect_timeout keyword arg in TCPSocket #3424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Compatibility:
* Implement `Time#deconstruct_keys` from Ruby 3.2 (#3039, @rwstauner).
* Do not autosplat a proc that accepts a single positional argument and keywords (#3039, @andrykonchin).
* Support passing anonymous * and ** parameters as method call arguments (#3039, @andrykonchin).
* Support `connect_timeout` keyword argument to `TCPSocket.{new,open}` (#3421, @manefz, @patricklinpl, @nirvdrum, @rwstauner).

Performance:

Expand Down
4 changes: 2 additions & 2 deletions lib/truffle/socket/tcp_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.gethostbyname(hostname)
[hostname, alternatives, family, *addresses]
end

def initialize(host, service, local_host = nil, local_service = nil)
def initialize(host, service, local_host = nil, local_service = nil, connect_timeout: nil)
@no_reverse_lookup = Primitive.class(self).do_not_reverse_lookup

if host
Expand Down Expand Up @@ -108,7 +108,7 @@ def initialize(host, service, local_host = nil, local_service = nil)
end

connect_status = Truffle::Socket::Foreign
.connect(descriptor, Socket.sockaddr_in(port, address))
.connect(descriptor, Socket.sockaddr_in(port, address), connect_timeout)

break if connect_status >= 0
end
Expand Down
15 changes: 13 additions & 2 deletions lib/truffle/socket/truffle/foreign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require 'timeout'

module Truffle
module Socket
module Foreign
Expand Down Expand Up @@ -107,14 +109,23 @@ def self.bind(descriptor, sockaddr)
end
end

def self.connect(descriptor, sockaddr)
def self.connect(descriptor, sockaddr, connect_timeout = nil)
sockaddr = Socket.coerce_to_string(sockaddr)

sockaddr_p = Primitive.io_thread_buffer_allocate(sockaddr.bytesize)
begin
sockaddr_p.write_bytes(sockaddr)

_connect(descriptor, sockaddr_p, sockaddr.bytesize)
if Primitive.nil?(connect_timeout)
_connect(descriptor, sockaddr_p, sockaddr.bytesize)
else
# In CRuby a connect_timeout of 0 fires immediately whereas a 0 for
# Timeout.timeout means "no timeout", so we change 0 to 1ns
# (the smallest time interval for clock_gettime).
Timeout.timeout(connect_timeout == 0 ? 0.000000001 : connect_timeout, IO::TimeoutError) do
_connect(descriptor, sockaddr_p, sockaddr.bytesize)
end
end
ensure
Primitive.io_thread_buffer_free(sockaddr_p)
end
Expand Down
5 changes: 2 additions & 3 deletions spec/tags/library/socket/tcpsocket/initialize_tags.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
fails:TCPSocket#initialize raises Errno::ETIMEDOUT with :connect_timeout when no server is listening on the given address
fails:TCPSocket#initialize with a running server connects to a server when passed connect_timeout argument
fails:TCPSocket#initialize raises IO::TimeoutError with :connect_timeout when no server is listening on the given address
fails:TCPSocket#initialize with a running server does not use the given block and warns to use TCPSocket::open
fails:TCPSocket#initialize using IPv4 when a server is listening on the given address creates a socket which is set to nonblocking
fails:TCPSocket#initialize using IPv4 when a server is listening on the given address creates a socket which is set to close on exec
fails:TCPSocket#initialize using IPv6 when a server is listening on the given address creates a socket which is set to nonblocking
fails:TCPSocket#initialize using IPv6 when a server is listening on the given address creates a socket which is set to close on exec
slow:TCPSocket#initialize raises IO::TimeoutError with :connect_timeout when no server is listening on the given address
slow:TCPSocket#initialize with a running server connects to a server when passed connect_timeout argument
5 changes: 2 additions & 3 deletions spec/tags/library/socket/tcpsocket/open_tags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
fails:TCPSocket.open raises Errno::ETIMEDOUT with :connect_timeout when no server is listening on the given address
fails:TCPSocket.open with a running server connects to a server when passed connect_timeout argument
fails:TCPSocket.open raises IO::TimeoutError with :connect_timeout when no server is listening on the given address
slow:TCPSocket.open raises IO::TimeoutError with :connect_timeout when no server is listening on the given address
slow:TCPSocket.open with a running server connects to a server when passed connect_timeout argument
2 changes: 2 additions & 0 deletions src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class IO

include Enumerable

class TimeoutError < IOError; end

module WaitReadable; end
module WaitWritable; end

Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestSocket_TCPSocket.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
exclude :test_initialize_failure, "needs investigation"
exclude :test_inspect, "needs investigation"
exclude :test_recvfrom, "needs investigation"
exclude :test_initialize_connect_timeout, "needs investigation"
exclude :test_initialize_resolv_timeout, "needs investigation"