Project

General

Profile

Feature #14832

Net::ReadTimeout and Net::WriteTimeout should tell the cause socket

Added by usa (Usaku NAKAMURA) about 2 years ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:87440]

Description

When Net::ReadTimeout occurs, we can't get any information from the exception.
It makes the trouble shooting too difficult.

I propose that io member to Net::ReadTimeout instance.
Then, when Net::ReadTimeout is rescued, we can know the cause IO instance.

Index: lib/net/protocol.rb
===================================================================
--- lib/net/protocol.rb (revision 63597)
+++ lib/net/protocol.rb (working copy)
@@ -75,15 +75,41 @@
   # ReadTimeout, a subclass of Timeout::Error, is raised if a chunk of the
   # response cannot be read within the read_timeout.

-  class ReadTimeout            < Timeout::Error; end
+  class ReadTimeout < Timeout::Error
+    def initialize(io = nil)
+      @io = io
+    end
+    attr_reader :io

+    def message
+      msg = super
+      if @io
+        msg = "#{msg} with #{@io.inspect}"
+      end
+      msg
+    end
+  end
+
   ##
   # WriteTimeout, a subclass of Timeout::Error, is raised if a chunk of the
   # response cannot be read within the read_timeout.

-  class WriteTimeout            < Timeout::Error; end
+  class WriteTimeout < Timeout::Error
+    def initialize(io = nil)
+      @io = io
+    end
+    attr_reader :io

+    def message
+      msg = super
+      if @io
+        msg = "#{msg} with #{@io.inspect}"
+      end
+      msg
+    end
+  end

+
   class BufferedIO   #:nodoc: internal use only
     def initialize(io, read_timeout: 60, write_timeout: 60, continue_timeout: nil, debug_output: nil)
       @io = io
@@ -188,12 +214,12 @@
         rv.clear
         return
       when :wait_readable
-        @io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
+        (io = @io.to_io).wait_readable(@read_timeout) or raise Net::ReadTimeout.new(io)
         # continue looping
       when :wait_writable
         # OpenSSL::Buffering#read_nonblock may fail with IO::WaitWritable.
         # http://www.openssl.org/support/faq.html#PROG10
-        @io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
+        (io = @io.to_io).wait_writable(@read_timeout) or raise Net::ReadTimeout.new(io)
         # continue looping
       when nil
         raise EOFError, 'end of file reached'
@@ -267,7 +293,7 @@
           end
           # continue looping
         when :wait_writable
-          @io.to_io.wait_writable(@write_timeout) or raise Net::WriteTimeout
+          (io = @io.to_io).wait_writable(@write_timeout) or raise Net::WriteTimeout.new(io)
           # continue looping
         end while need_retry
       end

Files

timeout.patch (2.3 KB) timeout.patch usa (Usaku NAKAMURA), 06/07/2018 10:49 AM

Also available in: Atom PDF