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

Avoid awaiting coroutines when holding locks

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

      Avoid awaiting coroutines when holding locks. In the AsyncMongoClient there are various places where we await coroutines while holding the Topology or Pool locks. This is problematic because it increases lock contention.

      Instead we should remove all awaits as much as possible. For example in the pool:

                  async with self.lock:
                      while (
                          self.conns
                          and self.conns[-1].idle_time_seconds() > self.opts.max_idle_time_seconds
                      ):
                          conn = self.conns.pop()
                          await conn.close_conn(ConnectionClosedReason.IDLE)
      

      Instead we can do:

                  close_conns = []
                  async with self.lock:
                      while (
                          self.conns
                          and self.conns[-1].idle_time_seconds() > self.opts.max_idle_time_seconds
                      ):
                          close_conns.append(self.conns.pop())
                  for conn in close_conns:
                      await conn.close_conn(ConnectionClosedReason.IDLE)
      

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              None
              None
              None
              None