Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-983

Close socket leaked from initailizing a resolver

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.1.0
    • Affects Version/s: 2.0.6, 2.1.0
    • Component/s: None
    • None
    • Environment:
      Ruby 2.1.5, OSX, MongoDB 2.4.10

      After initiating a client in the ruby driver version 2.1.0.beta, I can see that a connection is kept around until I call `GC.start`:

      $ irb
      irb(main):001:0> require 'mongo'
      => true
      irb(main):002:0> Mongo::Logger.logger.level = Logger::WARN
      => 2
      irb(main):003:0> #client=Mongo::Client.new(['127.0.0.1:27017'])
      irb(main):004:0* $$
      => 45358
      irb(main):005:0> client=Mongo::Client.new(['127.0.0.1:27017'])
      => #<Mongo::Client:0x70321682829200 cluster=127.0.0.1:27017>
      

      in another window:

      $ sudo lsof -p 45358 |grep 27017
      ruby    45358 agis    9u  IPv4 0x9bacde664dd2b0e1       0t0      TCP localhost:64412->localhost:27017 (ESTABLISHED)
      ruby    45358 agis   10u  IPv4 0x9bacde664daa80e1       0t0      TCP localhost:64413->localhost:27017 (ESTABLISHED)
      

      Now if I call on `GC.start`:

      irb(main):006:0> GC.start
      => nil
      

      we can see that the first connection (fd 9u) is garbage collected:

      $ sudo lsof -p 45358 |grep 27017
      ruby    45358 agis   10u  IPv4 0x9bacde664daa80e1       0t0      TCP localhost:64413->localhost:27017 (ESTABLISHED)
      

      After a little bit of investigation, I've found that this connection is created like so:

      While running the `Monitor` for a server for the first time, eventually #initialize_resolver! is called, which in turn calls Mongo::Socket::TCP#connect! which in turn creates the connection in this line.

      However, I'm not sure why this connection is created at all since it's garbage collected in the next GC.

      This may cause problems on setups with low connection limits.

      Thanks,
      Agis

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            agis Agis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: