Timeout::Error
RubyERRORNotableConcurrency

Operation exceeded the time limit

Quick Answer

Use native socket/HTTP timeouts instead of Timeout.timeout when possible; always rescue Timeout::Error specifically.

What this means

Raised by the Timeout module when an operation does not complete within the specified number of seconds. Timeout::Error is a subclass of both RuntimeError and Interrupt, making it tricky to rescue correctly.

Why it happens
  1. 1A network request, database query, or external call exceeding the allotted time
  2. 2Wrapping a blocking C extension call that cannot be interrupted safely

Fix

Prefer native timeouts over Timeout.timeout

Prefer native timeouts over Timeout.timeout
http = Net::HTTP.new(host, 443)
http.use_ssl = true
http.read_timeout = 5    # native OS-level timeout
http.open_timeout = 3

Why this works

Native timeouts use OS-level socket options and are safe to use with C extensions; Timeout.timeout uses Thread#raise which can leave resources in inconsistent states.

Code examples
Using Timeout.timeoutruby
require 'timeout'

begin
  Timeout.timeout(5) { slow_operation }
rescue Timeout::Error
  puts 'Operation timed out'
end
Timeout::Error ancestry (surprising)ruby
Timeout::Error < RuntimeError   # => true
Timeout::Error < Interrupt       # => true (in some Ruby versions)
Combined rescueruby
rescue Timeout::Error, Errno::ETIMEDOUT => e
  raise ServiceTimeout, "Request timed out: #{e.message}"
Sources
Official documentation ↗

Ruby Standard Library Documentation

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

← All Ruby errors