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

ruby-changes:34129

From: akr <ko1@a...>
Date: Wed, 28 May 2014 19:03:03 +0900 (JST)
Subject: [ruby-changes:34129] akr:r46210 (trunk): * lib/net/imap.rb (Net::IMAP#initialize): Close the opened socket when

akr	2014-05-28 19:02:54 +0900 (Wed, 28 May 2014)

  New Revision: 46210

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

  Log:
    * lib/net/imap.rb (Net::IMAP#initialize): Close the opened socket when
      any exception occur.
      This fixes a fd leak by IMAPTest#test_imaps_post_connection_check
      which start_tls_session() raises an exception.

  Modified files:
    trunk/ChangeLog
    trunk/lib/net/imap.rb
    trunk/test/net/imap/test_imap.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46209)
+++ ChangeLog	(revision 46210)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed May 28 19:00:31 2014  Tanaka Akira  <akr@f...>
+
+	* lib/net/imap.rb (Net::IMAP#initialize): Close the opened socket when
+	  any exception occur.
+	  This fixes a fd leak by IMAPTest#test_imaps_post_connection_check
+	  which start_tls_session() raises an exception.
+
 Wed May 28 18:06:13 2014  Tanaka Akira  <akr@f...>
 
 	* ext/openssl/ossl_ssl.c (ossl_ssl_close): Fix sync_close to work
Index: lib/net/imap.rb
===================================================================
--- lib/net/imap.rb	(revision 46209)
+++ lib/net/imap.rb	(revision 46210)
@@ -1043,40 +1043,43 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L1043
       @tagno = 0
       @parser = ResponseParser.new
       @sock = TCPSocket.open(@host, @port)
-      if options[:ssl]
-        start_tls_session(options[:ssl])
-        @usessl = true
-      else
-        @usessl = false
-      end
-      @responses = Hash.new([].freeze)
-      @tagged_responses = {}
-      @response_handlers = []
-      @tagged_response_arrival = new_cond
-      @continuation_request_arrival = new_cond
-      @idle_done_cond = nil
-      @logout_command_tag = nil
-      @debug_output_bol = true
-      @exception = nil
+      begin
+        if options[:ssl]
+          start_tls_session(options[:ssl])
+          @usessl = true
+        else
+          @usessl = false
+        end
+        @responses = Hash.new([].freeze)
+        @tagged_responses = {}
+        @response_handlers = []
+        @tagged_response_arrival = new_cond
+        @continuation_request_arrival = new_cond
+        @idle_done_cond = nil
+        @logout_command_tag = nil
+        @debug_output_bol = true
+        @exception = nil
 
-      @greeting = get_response
-      if @greeting.nil?
-        @sock.close
-        raise Error, "connection closed"
-      end
-      if @greeting.name == "BYE"
+        @greeting = get_response
+        if @greeting.nil?
+          raise Error, "connection closed"
+        end
+        if @greeting.name == "BYE"
+          raise ByeResponseError, @greeting
+        end
+
+        @client_thread = Thread.current
+        @receiver_thread = Thread.start {
+          begin
+            receive_responses
+          rescue Exception
+          end
+        }
+        @receiver_thread_terminating = false
+      rescue Exception
         @sock.close
-        raise ByeResponseError, @greeting
+        raise
       end
-
-      @client_thread = Thread.current
-      @receiver_thread = Thread.start {
-        begin
-          receive_responses
-        rescue Exception
-        end
-      }
-      @receiver_thread_terminating = false
     end
 
     def receive_responses
Index: test/net/imap/test_imap.rb
===================================================================
--- test/net/imap/test_imap.rb	(revision 46209)
+++ test/net/imap/test_imap.rb	(revision 46210)
@@ -505,19 +505,20 @@ class IMAPTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/imap/test_imap.rb#L505
     ths = Thread.start do
       begin
         sock = server.accept
-        sock.print("* OK test server\r\n")
-        sock.gets
-        sock.print("RUBY0001 OK completed\r\n")
-        ctx = OpenSSL::SSL::SSLContext.new
-        ctx.ca_file = CA_FILE
-        ctx.key = File.open(SERVER_KEY) { |f|
-          OpenSSL::PKey::RSA.new(f)
-        }
-        ctx.cert = File.open(SERVER_CERT) { |f|
-          OpenSSL::X509::Certificate.new(f)
-        }
-        sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
         begin
+          sock.print("* OK test server\r\n")
+          sock.gets
+          sock.print("RUBY0001 OK completed\r\n")
+          ctx = OpenSSL::SSL::SSLContext.new
+          ctx.ca_file = CA_FILE
+          ctx.key = File.open(SERVER_KEY) { |f|
+            OpenSSL::PKey::RSA.new(f)
+          }
+          ctx.cert = File.open(SERVER_CERT) { |f|
+            OpenSSL::X509::Certificate.new(f)
+          }
+          sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
+          sock.sync_close = true
           sock.accept
           sock.gets
           sock.print("* BYE terminating connection\r\n")

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

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