Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-3193

Add ResourceWarning for unclosed MongoClients in __del__

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.9
    • Affects Version/s: None
    • Component/s: None
    • None
    • Completed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Currently if the user does not explicitly close() the client, most resources cleaned up when the client is garbage collected but some resources, like sessions, will not be cleaned up efficiently (since the endSessions command doesn't have a chance to run).  We think this might be the source of intermittent failures in tests.

      We should look into adding a MongoClient._del_ method that checks whether the client has been closed.  If it is not closed, issue a warning and clean up resources.

      pyzeromq uses a similar method.

      We had some concerns that using _del_ might cause a reference cycle, but the CPython docs say that "the cycle will be later detected and deleted by the cyclic garbage collector."

      If we are adopting this model, we should also use warning control in our test suite to catch these warnings.

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            steve.silvester@mongodb.com Steve Silvester
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: