about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/yahns/openssl_client.rb39
1 files changed, 32 insertions, 7 deletions
diff --git a/lib/yahns/openssl_client.rb b/lib/yahns/openssl_client.rb
index 0d376bd..c090083 100644
--- a/lib/yahns/openssl_client.rb
+++ b/lib/yahns/openssl_client.rb
@@ -40,15 +40,31 @@ module Yahns::OpenSSLClient # :nodoc:
   def kgio_trywrite(buf)
     len = buf.bytesize
     return if len == 0
-    buf = @ssl_blocked = buf.dup
+
+    case @ssl_blocked
+    when nil # likely
+      buf = @ssl_blocked = buf.dup
+    when Exception
+      raise @ssl_blocked
+    when String
+      if @ssl_blocked != buf
+        pfx = object_id
+        warn("#{pfx} BUG: ssl_blocked != buf\n" \
+             "#{pfx} ssl_blocked=#{@ssl_blocked.inspect}\n" \
+             "#{pfx} buf=#{buf.inspect}\n")
+        raise 'BUG: ssl_blocked} != buf'
+      end
+    end
+
     case rv = @ssl.write_nonblock(buf, exception: false)
     when :wait_readable, :wait_writable
-      return rv # do not clear ssl_blocked
+      rv # do not clear ssl_blocked
     when Integer
-      rv = len == rv ? nil : buf.byteslice(rv, len - rv)
+      @ssl_blocked = len == rv ? nil : buf.byteslice(rv, len - rv)
     end
-    @ssl_blocked = nil
-    rv
+  rescue SystemCallError => e # ECONNRESET/EPIPE
+    e.set_backtrace([])
+    raise(@ssl_blocked = e)
   end
 
   def kgio_trywritev(buf)
@@ -75,22 +91,31 @@ module Yahns::OpenSSLClient # :nodoc:
   def trysendio(io, offset, count)
     return 0 if count == 0
 
-    unless buf = @ssl_blocked
+    case buf = @ssl_blocked
+    when nil
       count = 0x4000 if count > 0x4000
       buf = Thread.current[:yahns_sfbuf] ||= ''.dup
       io.pos = offset
       buf = io.read(count, buf) or return # nil for EOF
       buf = @ssl_blocked = buf.dup
+    when Exception
+      raise buf
+    # when String # just use it as-is
     end
 
     # call write_nonblock directly since kgio_trywrite allocates
     # an unnecessary string
+    len = buf.size
     case rv = @ssl.write_nonblock(buf, exception: false)
     when :wait_readable, :wait_writable
       return rv # do not clear ssl_blocked
+    when Integer
+      @ssl_blocked = len == rv ? nil : buf.byteslice(rv, len - rv)
     end
-    @ssl_blocked = nil
     rv
+  rescue SystemCallError => e # ECONNRESET/EPIPE
+    e.set_backtrace([])
+    raise(@ssl_blocked = e)
   end
 
   def shutdown # we never call this with a how=SHUT_* arg