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

python3 process shutdown is unreasonably slow after MongoClient (500ms)

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Python Drivers
    • 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?

      Detailed steps to reproduce the problem?

      Prior to creating a MongoClient object, exiting the python3 process is reasonably fast at ~75ms (about ~60ms of which can be attributed to pymongo):

      $ time python3 -c 'True'
      real	0m0.026s
      user	0m0.012s
      sys	0m0.016s
      
      $ time python3 -c 'import pymongo'
      real	0m0.148s
      user	0m0.140s
      sys	0m0.006s
      
      $ time python3 -c 'import pymongo'
      real	0m0.098s
      user	0m0.071s
      sys	0m0.028s
      
      $ time python3 -c 'import pymongo; import sys; sys.exit()'
      real	0m0.096s
      user	0m0.068s
      sys	0m0.029s
      
      $ time python3 -c 'import pymongo; import os; os._exit(0)'
      real	0m0.085s
      user	0m0.064s
      sys	0m0.023s
      
      $ time python3 -c 'import sys; sys.exit()'
      real	0m0.025s
      user	0m0.012s
      sys	0m0.014s
      
      $ time python3 -c 'import os; os._exit(0)'
      real	0m0.022s
      user	0m0.013s
      sys	0m0.011s
      

      However, once a MongoClient object has been created, the time increases significantly by ~500ms:

      $ time python3 -c 'import pymongo; pymongo.MongoClient()'
      real	0m0.604s
      user	0m0.096s
      sys	0m0.011s
      
      $ time python3 -c 'import pymongo; pymongo.MongoClient(); import sys; print("bye", file=sys.stderr)'
      bye
      real	0m0.611s
      user	0m0.098s
      sys	0m0.014s
      
      $ time python3 -c 'import pymongo; pymongo.MongoClient(); import sys; print("bye", file=sys.stderr); sys.exit()'
      bye
      real	0m0.605s
      user	0m0.085s
      sys	0m0.021s
      
      $ time python3 -c 'import pymongo; pymongo.MongoClient(); import sys; print("bye", file=sys.stderr); import os; os._exit(0)'
      bye
      real	0m0.089s
      user	0m0.069s
      sys	0m0.021s
      

      I can see from the mongod logs that a connection is opened and closed 1-2ms later. But the problem occurs strictly during the python3 process shutdown (ie. calling .close() on the MongoClient doesn't help, neither does del). When running the above, the process "sticks" noticably after printing "bye", but before printing the time output and returning me to my shell prompt. Whereas, when forcibly exiting python via os._exit(), there is no delay, and the total process time is similar to when the MongoClient object hasn't been created (despite including an actual connection to the mongod).

      Using os._exit() in this way is a reasonable workaround in some situations (eg. known read-only workloads), but in general is riskier than a normal graceful shutdown.

      My use case is a collection of very small utilities, each of which connects via pymongo, and does one small operation (or a few small related operations). This design is necessary because the utilities are meant to be composable and extensible. Unfortunately, this problem means that once a few (or more) of the utilities are chained together, the overall runtime balloons out unreasonably, eg. 2-3+ secs instead of 0.5 secs.

      Definition of done: what must be done to consider the task complete?

      The command

      $ time python3 -c 'import pymongo; pymongo.MongoClient(); import sys; print("bye", file=sys.stderr); sys.exit()'
      

      should run approximately as fast as

      $ time python3 -c 'import pymongo; pymongo.MongoClient(); import sys; print("bye", file=sys.stderr); import os; os._exit(0)'
      

      The exact Python version used, with patch level:

      $ python -c "import sys; print(sys.version)"

      3.10.12 (main, Sep 11 2024, 15:47:36) [GCC 11.4.0]
      

      The exact version of PyMongo used, with patch level:

      $ python -c "import pymongo; print(pymongo.version); print(pymongo.has_c())"

      4.10.1
      True
      

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      Irrelevant, I think? Happens with a standalone mongod on localhost.

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Ubuntu 22.04

      Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)

      None

      Security Vulnerabilities

      If you've identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here

      N/A

            Assignee:
            Unassigned Unassigned
            Reporter:
            kevin.pulo@mongodb.com Kevin Pulo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: