[前][次][番号順一覧][スレッド一覧]

ruby-changes:34142

From: akr <ko1@a...>
Date: Thu, 29 May 2014 19:32:24 +0900 (JST)
Subject: [ruby-changes:34142] akr:r46223 (trunk): * ext/openssl/lib/openssl/ssl.rb (SSLServer#accept): Close a socket

akr	2014-05-29 19:32:19 +0900 (Thu, 29 May 2014)

  New Revision: 46223

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?revision=46223&view=revision

  Log:
    * ext/openssl/lib/openssl/ssl.rb (SSLServer#accept): Close a socket
      if any exception occur.

  Modified files:
    trunk/ChangeLog
    trunk/ext/openssl/lib/openssl/ssl.rb
    trunk/test/openssl/test_partial_record_read.rb
    trunk/test/openssl/test_ssl.rb
    trunk/test/openssl/utils.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46222)
+++ ChangeLog	(revision 46223)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu May 29 19:31:10 2014  Tanaka Akira  <akr@f...>
+
+	* ext/openssl/lib/openssl/ssl.rb (SSLServer#accept): Close a socket
+	  if any exception occur. 
+
 Thu May 29 05:05:29 2014  Eric Wong  <e@8...>
 
 	* include/ruby/ruby.h: Hide Symbol internals.
Index: ext/openssl/lib/openssl/ssl.rb
===================================================================
--- ext/openssl/lib/openssl/ssl.rb	(revision 46222)
+++ ext/openssl/lib/openssl/ssl.rb	(revision 46223)
@@ -234,8 +234,12 @@ module OpenSSL https://github.com/ruby/ruby/blob/trunk/ext/openssl/lib/openssl/ssl.rb#L234
           ssl.sync_close = true
           ssl.accept if @start_immediately
           ssl
-        rescue SSLError => ex
-          sock.close
+        rescue Exception => ex
+          if ssl
+            ssl.close
+          else
+            sock.close
+          end
           raise ex
         end
       end
Index: test/openssl/test_partial_record_read.rb
===================================================================
--- test/openssl/test_partial_record_read.rb	(revision 46222)
+++ test/openssl/test_partial_record_read.rb	(revision 46223)
@@ -8,17 +8,26 @@ if defined?(OpenSSL) https://github.com/ruby/ruby/blob/trunk/test/openssl/test_partial_record_read.rb#L8
 
       start_server(port, OpenSSL::SSL::VERIFY_NONE, true, :server_proc =>
           Proc.new do |server_ctx, server_ssl|
-            server_ssl.io.write("\x01") # the beginning of a TLS record
-            sleep 6                     # do not finish prematurely before the read by the client is attempted
+            begin
+              server_ssl.io.write("\x01") # the beginning of a TLS record
+              sleep 6                     # do not finish prematurely before the read by the client is attempted
+            ensure
+              server_ssl.close
+            end
           end
       ) do |server, port|
         sock = TCPSocket.new("127.0.0.1", port)
         ssl = OpenSSL::SSL::SSLSocket.new(sock)
-        ssl.connect
-        sleep 3  # wait is required for the (incomplete) TLS record to arrive at the client socket
+        ssl.sync_close = true
+        begin
+          ssl.connect
+          sleep 3  # wait is required for the (incomplete) TLS record to arrive at the client socket
 
-        # Should raise a IO::WaitReadable since a full TLS record is not available for reading.
-        assert_raise(IO::WaitReadable) { ssl.read_nonblock(1) }
+          # Should raise a IO::WaitReadable since a full TLS record is not available for reading.
+          assert_raise(IO::WaitReadable) { ssl.read_nonblock(1) }
+        ensure
+          ssl.close
+        end
       end
     end
 
Index: test/openssl/utils.rb
===================================================================
--- test/openssl/utils.rb	(revision 46222)
+++ test/openssl/utils.rb	(revision 46223)
@@ -311,7 +311,6 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOP https://github.com/ruby/ruby/blob/trunk/test/openssl/utils.rb#L311
           if (server)
             server.join(5)
             if server.alive?
-              server.kill
               server.join
               flunk("TCPServer was closed and SSLServer is still alive") unless $!
             end
@@ -322,7 +321,6 @@ AQjjxMXhwULlmuR/K+WwlaZPiLIBYalLAZQ7ZbOP https://github.com/ruby/ruby/blob/trunk/test/openssl/utils.rb#L321
       end
     ensure
       threads.each {|th|
-        th.kill
         th.join
       }
     end
Index: test/openssl/test_ssl.rb
===================================================================
--- test/openssl/test_ssl.rb	(revision 46222)
+++ test/openssl/test_ssl.rb	(revision 46223)
@@ -125,7 +125,12 @@ class OpenSSL::TestSSL < OpenSSL::SSLTes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L125
       assert_raise(OpenSSL::SSL::SSLError, Errno::ECONNRESET){
         sock = TCPSocket.new("127.0.0.1", port)
         ssl = OpenSSL::SSL::SSLSocket.new(sock)
-        ssl.connect
+        ssl.sync_close = true
+        begin
+          ssl.connect
+        ensure
+          ssl.close
+        end
       }
 
       ctx = OpenSSL::SSL::SSLContext.new
@@ -239,8 +244,13 @@ class OpenSSL::TestSSL < OpenSSL::SSLTes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L244
       ctx = OpenSSL::SSL::SSLContext.new
       ctx.set_params
       ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
-      assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
-      assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
+      ssl.sync_close = true
+      begin
+        assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
+        assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
+      ensure
+        ssl.close
+      end
 
       sock = TCPSocket.new("127.0.0.1", port)
       ctx = OpenSSL::SSL::SSLContext.new
@@ -251,8 +261,13 @@ class OpenSSL::TestSSL < OpenSSL::SSLTes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L261
         end
       )
       ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
-      ssl.connect
-      assert_equal(OpenSSL::X509::V_OK, ssl.verify_result)
+      ssl.sync_close = true
+      begin
+        ssl.connect
+        assert_equal(OpenSSL::X509::V_OK, ssl.verify_result)
+      ensure
+        ssl.close
+      end
 
       sock = TCPSocket.new("127.0.0.1", port)
       ctx = OpenSSL::SSL::SSLContext.new
@@ -263,8 +278,13 @@ class OpenSSL::TestSSL < OpenSSL::SSLTes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L278
         end
       )
       ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
-      assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
-      assert_equal(OpenSSL::X509::V_ERR_APPLICATION_VERIFICATION, ssl.verify_result)
+      ssl.sync_close = true
+      begin
+        assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
+        assert_equal(OpenSSL::X509::V_ERR_APPLICATION_VERIFICATION, ssl.verify_result)
+      ensure
+        ssl.close
+      end
     }
   end
 
@@ -279,12 +299,16 @@ class OpenSSL::TestSSL < OpenSSL::SSLTes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L299
         end
       )
       ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
-      OpenSSL::TestUtils.silent do
-        # SSLError, not RuntimeError
-        assert_raise(OpenSSL::SSL::SSLError) { ssl.connect }
+      ssl.sync_close = true
+      begin
+        OpenSSL::TestUtils.silent do
+          # SSLError, not RuntimeError
+          assert_raise(OpenSSL::SSL::SSLError) { ssl.connect }
+        end
+        assert_equal(OpenSSL::X509::V_ERR_CERT_REJECTED, ssl.verify_result)
+      ensure
+        ssl.close
       end
-      assert_equal(OpenSSL::X509::V_ERR_CERT_REJECTED, ssl.verify_result)
-      ssl.close
     }
   end
 
@@ -301,8 +325,13 @@ class OpenSSL::TestSSL < OpenSSL::SSLTes https://github.com/ruby/ruby/blob/trunk/test/openssl/test_ssl.rb#L325
       assert(ciphers_names.all?{|v| /ADH/ !~ v })
       assert(ciphers_versions.all?{|v| /SSLv2/ !~ v })
       ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
-      assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
-      assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
+      ssl.sync_close = true
+      begin
+        assert_raise(OpenSSL::SSL::SSLError){ ssl.connect }
+        assert_equal(OpenSSL::X509::V_ERR_SELF_SIGNED_CERT_IN_CHAIN, ssl.verify_result)
+      ensure
+        ssl.close
+      end
     }
   end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]