Project

General

Profile

Actions

Feature #14832

closed

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

Added by usa (Usaku NAKAMURA) over 6 years ago. Updated almost 6 years ago.

Status:
Closed
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

Updated by naruse (Yui NARUSE) over 6 years ago

TCPSocket#inspect shows its address and port from Ruby 2.5 like #<TCPSocket:fd 10, AF_INET6, ::1, 56090>.
But if the socket is closed, it becomes #<TCPSocket:(closed)>; it's useless.
At least Net::HTTP.start automatically closes the socket when it escapes given block.
It means its message will be like follows:

%  ./ruby -Ilib test.rb
Sending 5120000 bytes
Traceback (most recent call last):
    13: from test.rb:15:in `<main>'
    12: from /Users/naruse/work/ruby/lib/net/http.rb:1478:in `request'
    11: from /Users/naruse/work/ruby/lib/net/http.rb:1505:in `transport_request'
    10: from /Users/naruse/work/ruby/lib/net/http.rb:1505:in `catch'
     9: from /Users/naruse/work/ruby/lib/net/http.rb:1506:in `block in transport_request'
     8: from /Users/naruse/work/ruby/lib/net/http/generic_request.rb:123:in `exec'
     7: from /Users/naruse/work/ruby/lib/net/http/generic_request.rb:189:in `send_request_with_body'
     6: from /Users/naruse/work/ruby/lib/net/protocol.rb:247:in `write'
     5: from /Users/naruse/work/ruby/lib/net/protocol.rb:265:in `writing'
     4: from /Users/naruse/work/ruby/lib/net/protocol.rb:248:in `block in write'
     3: from /Users/naruse/work/ruby/lib/net/protocol.rb:275:in `write0'
     2: from /Users/naruse/work/ruby/lib/net/protocol.rb:275:in `each_with_index'
     1: from /Users/naruse/work/ruby/lib/net/protocol.rb:275:in `each'
/Users/naruse/work/ruby/lib/net/protocol.rb:296:in `block in write0': Net::WriteTimeout with #<TCPSocket:(closed)> (Net::WriteTimeout)

Maybe WriteTimeout#initialize should get inspect or address.

Updated by usa (Usaku NAKAMURA) over 6 years ago

Maybe WriteTimeout#initialize should get inspect or address.

Sure.
See attached file.

Updated by naruse (Yui NARUSE) over 6 years ago

usa (Usaku NAKAMURA) wrote:

Maybe WriteTimeout#initialize should get inspect or address.

Sure.
See attached file.

Looks good.
Could you commit it?

Actions #4

Updated by usa (Usaku NAKAMURA) almost 6 years ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r66308.


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

  • lib/net/protocol.rb (ReadTimeout, WriteTimeout): Net::ReadTimeout and Net::WriteTimeout should tell the cause socket
    [Feature #14832] [ruby-core:87440]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0