Errno::ETIMEDOUT
RubyERRORNotableNetwork

Connection or operation timed out

Quick Answer

Set explicit timeouts on HTTP and socket operations; rescue ETIMEDOUT alongside Timeout::Error for full coverage.

What this means

Raised when a network connection or IO operation exceeds the OS-level timeout. Unlike Timeout::Error which is Ruby-level, ETIMEDOUT comes from the kernel TCP stack. Maps to POSIX errno 110.

Why it happens
  1. 1Target host is unreachable and TCP connection times out after retries
  2. 2Slow network causing read/write to exceed OS socket timeout
  3. 3No route to host due to firewall drop rules (as opposed to reject)

Fix

Set read and open timeouts on HTTP

Set read and open timeouts on HTTP
http = Net::HTTP.new(host, port)
http.open_timeout = 5   # seconds to establish connection
http.read_timeout = 10  # seconds to read response
http.start { |h| h.get('/path') }

Why this works

Explicit timeouts prevent threads from blocking indefinitely on slow or unreachable hosts.

Code examples
Rescue ETIMEDOUTruby
begin
  socket.connect(remote_addr)
rescue Errno::ETIMEDOUT
  puts 'Connection timed out — host may be unreachable'
end
Combined timeout rescueruby
rescue Errno::ETIMEDOUT, Timeout::Error => e
  raise ConnectionTimeout, e.message
Socket-level timeout optionruby
sock = Socket.new(:INET, :STREAM)
sock.setsockopt(:SOCKET, :SO_RCVTIMEO, [10, 0].pack('l_2'))
Sources
Official documentation ↗

Ruby Core Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Ruby errors