Errno::ECONNRESET
RubyERRORNotableNetwork
Connection reset by peer
Quick Answer
Implement connection pooling with automatic reconnection; treat connection reset as a transient error and retry.
What this means
Raised when the remote end abruptly closes a TCP connection by sending a RST packet. Unlike ECONNREFUSED, the connection was established before the reset occurred. Maps to POSIX errno 104.
Why it happens
- 1Server process crashed or was killed while the connection was active
- 2Network equipment (load balancer, proxy) closed an idle connection
- 3Server-side timeout on a long-running request
Fix
Rescue and reconnect
Rescue and reconnect
def with_retry(times: 3) yield rescue Errno::ECONNRESET, EOFError => e times -= 1 retry if times > 0 raise end
Why this works
Rescuing both ECONNRESET and EOFError covers the two common ways a peer can abruptly terminate a connection.
Code examples
Rescue connection resetruby
begin data = socket.read(4096) rescue Errno::ECONNRESET puts 'Connection was reset by the remote host' reconnect end
HTTP with keep-alive reconnectruby
http = Net::HTTP.new(host, port)
http.start
begin
http.get('/path')
rescue Errno::ECONNRESET
http = Net::HTTP.new(host, port)
http.start
retry
endCombined network rescueruby
rescue Errno::ECONNRESET, Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
raise NetworkError, "Connection failed: #{e.message}"Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev