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

ruby-changes:54533

From: nagachika <ko1@a...>
Date: Mon, 7 Jan 2019 21:05:26 +0900 (JST)
Subject: [ruby-changes:54533] nagachika:r66748 (ruby_2_5): merge revision(s) 62671: [Backport #14571]

nagachika	2019-01-07 21:05:16 +0900 (Mon, 07 Jan 2019)

  New Revision: 66748

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

  Log:
    merge revision(s) 62671: [Backport #14571]
    
    resolv.rb: close socket
    
    * lib/resolv.rb (UnconnectedUDP#lazy_initialize): store new
      sockets before binding, so the sockets get closed when the
      requester is closing.
    
    * lib/resolv.rb (ConnectedUDP#lazy_initialize): ditto.
    
    * lib/resolv.rb (UnconnectedUDP#close): synchronize to get rid of
      race condition.
    
    * lib/resolv.rb (ConnectedUDP#close): ditto.
    
    [ruby-core:85901] [Bug #14571]
    
    From: quixoten (Devin Christensen) <quixoten@g...>

  Modified directories:
    branches/ruby_2_5/
  Modified files:
    branches/ruby_2_5/lib/resolv.rb
    branches/ruby_2_5/test/resolv/test_dns.rb
    branches/ruby_2_5/version.h
Index: ruby_2_5/version.h
===================================================================
--- ruby_2_5/version.h	(revision 66747)
+++ ruby_2_5/version.h	(revision 66748)
@@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1
 #define RUBY_VERSION "2.5.4"
-#define RUBY_RELEASE_DATE "2019-01-02"
-#define RUBY_PATCHLEVEL 124
+#define RUBY_RELEASE_DATE "2019-01-07"
+#define RUBY_PATCHLEVEL 125
 
 #define RUBY_RELEASE_YEAR 2019
 #define RUBY_RELEASE_MONTH 1
-#define RUBY_RELEASE_DAY 2
+#define RUBY_RELEASE_DAY 7
 
 #include "ruby/version.h"
 
Index: ruby_2_5/test/resolv/test_dns.rb
===================================================================
--- ruby_2_5/test/resolv/test_dns.rb	(revision 66747)
+++ ruby_2_5/test/resolv/test_dns.rb	(revision 66748)
@@ -3,6 +3,7 @@ require 'test/unit' https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/resolv/test_dns.rb#L3
 require 'resolv'
 require 'socket'
 require 'tempfile'
+require 'minitest/mock'
 
 class TestResolvDNS < Test::Unit::TestCase
   def setup
@@ -246,4 +247,22 @@ class TestResolvDNS < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/resolv/test_dns.rb#L247
     }
     assert_operator(2**14, :<, m.to_s.length)
   end
+
+  def assert_no_fd_leak
+    socket = assert_throw(self) do |tag|
+      Resolv::DNS.stub(:bind_random_port, ->(s, *) {throw(tag, s)}) do
+        yield.getname("8.8.8.8")
+      end
+    end
+
+    assert_predicate(socket, :closed?, "file descriptor leaked")
+  end
+
+  def test_no_fd_leak_connected
+    assert_no_fd_leak {Resolv::DNS.new(nameserver_port: [['127.0.0.1', 53]])}
+  end
+
+  def test_no_fd_leak_unconnected
+    assert_no_fd_leak {Resolv::DNS.new}
+  end
 end
Index: ruby_2_5/lib/resolv.rb
===================================================================
--- ruby_2_5/lib/resolv.rb	(revision 66747)
+++ ruby_2_5/lib/resolv.rb	(revision 66748)
@@ -734,35 +734,47 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/ruby_2_5/lib/resolv.rb#L734
         def initialize(*nameserver_port)
           super()
           @nameserver_port = nameserver_port
-          @socks_hash = {}
-          @socks = []
-          nameserver_port.each {|host, port|
-            if host.index(':')
-              bind_host = "::"
-              af = Socket::AF_INET6
-            else
-              bind_host = "0.0.0.0"
-              af = Socket::AF_INET
-            end
-            next if @socks_hash[bind_host]
-            begin
-              sock = UDPSocket.new(af)
-            rescue Errno::EAFNOSUPPORT
-              next # The kernel doesn't support the address family.
-            end
-            sock.do_not_reverse_lookup = true
-            DNS.bind_random_port(sock, bind_host)
-            @socks << sock
-            @socks_hash[bind_host] = sock
+          @initialized = false
+          @mutex = Thread::Mutex.new
+        end
+
+        def lazy_initialize
+          @mutex.synchronize {
+            next if @initialized
+            @initialized = true
+            @socks_hash = {}
+            @socks = []
+            @nameserver_port.each {|host, port|
+              if host.index(':')
+                bind_host = "::"
+                af = Socket::AF_INET6
+              else
+                bind_host = "0.0.0.0"
+                af = Socket::AF_INET
+              end
+              next if @socks_hash[bind_host]
+              begin
+                sock = UDPSocket.new(af)
+              rescue Errno::EAFNOSUPPORT
+                next # The kernel doesn't support the address family.
+              end
+              @socks << sock
+              @socks_hash[bind_host] = sock
+              sock.do_not_reverse_lookup = true
+              DNS.bind_random_port(sock, bind_host)
+            }
           }
+          self
         end
 
         def recv_reply(readable_socks)
+          lazy_initialize
           reply, from = readable_socks[0].recvfrom(UDPSize)
           return reply, [from[3],from[1]]
         end
 
         def sender(msg, data, host, port=Port)
+          lazy_initialize
           sock = @socks_hash[host.index(':') ? "::" : "0.0.0.0"]
           return nil if !sock
           service = [host, port]
@@ -774,9 +786,14 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/ruby_2_5/lib/resolv.rb#L786
         end
 
         def close
-          super
-          @senders.each_key {|service, id|
-            DNS.free_request_id(service[0], service[1], id)
+          @mutex.synchronize {
+            if @initialized
+              super
+              @senders.each_key {|service, id|
+                DNS.free_request_id(service[0], service[1], id)
+              }
+              @initialized = false
+            end
           }
         end
 
@@ -800,20 +817,32 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/ruby_2_5/lib/resolv.rb#L817
           super()
           @host = host
           @port = port
-          is_ipv6 = host.index(':')
-          sock = UDPSocket.new(is_ipv6 ? Socket::AF_INET6 : Socket::AF_INET)
-          @socks = [sock]
-          sock.do_not_reverse_lookup = true
-          DNS.bind_random_port(sock, is_ipv6 ? "::" : "0.0.0.0")
-          sock.connect(host, port)
+          @mutex = Thread::Mutex.new
+          @initialized = false
+        end
+
+        def lazy_initialize
+          @mutex.synchronize {
+            next if @initialized
+            @initialized = true
+            is_ipv6 = @host.index(':')
+            sock = UDPSocket.new(is_ipv6 ? Socket::AF_INET6 : Socket::AF_INET)
+            @socks = [sock]
+            sock.do_not_reverse_lookup = true
+            DNS.bind_random_port(sock, is_ipv6 ? "::" : "0.0.0.0")
+            sock.connect(@host, @port)
+          }
+          self
         end
 
         def recv_reply(readable_socks)
+          lazy_initialize
           reply = readable_socks[0].recv(UDPSize)
           return reply, nil
         end
 
         def sender(msg, data, host=@host, port=@port)
+          lazy_initialize
           unless host == @host && port == @port
             raise RequestError.new("host/port don't match: #{host}:#{port}")
           end
@@ -824,10 +853,15 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/ruby_2_5/lib/resolv.rb#L853
         end
 
         def close
-          super
-          @senders.each_key {|from, id|
-            DNS.free_request_id(@host, @port, id)
-          }
+          @mutex.synchronize do
+            if @initialized
+              super
+              @senders.each_key {|from, id|
+                DNS.free_request_id(@host, @port, id)
+              }
+              @initialized = false
+            end
+          end
         end
 
         class Sender < Requester::Sender # :nodoc:
@@ -841,6 +875,7 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/ruby_2_5/lib/resolv.rb#L875
 
       class MDNSOneShot < UnconnectedUDP # :nodoc:
         def sender(msg, data, host, port=Port)
+          lazy_initialize
           id = DNS.allocate_request_id(host, port)
           request = msg.encode
           request[0,2] = [id].pack('n')
@@ -850,6 +885,7 @@ class Resolv https://github.com/ruby/ruby/blob/trunk/ruby_2_5/lib/resolv.rb#L885
         end
 
         def sender_for(addr, msg)
+          lazy_initialize
           @senders[msg.id]
         end
       end
Index: ruby_2_5
===================================================================
--- ruby_2_5	(revision 66747)
+++ ruby_2_5	(revision 66748)

Property changes on: ruby_2_5
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
   Merged /trunk:r62671

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

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