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

Connect Timeout not working for DNS timeout (mongodb+srv)

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.9
    • Affects Version/s: None
    • None
    • Environment:
    • Minor Change

      I'm trying to use a try-exception block for a connection error in my Gunicorn server when it tries to access the MongoDB Atlas cluster.

       

      I set all timeouts to 1000ms, but they're not working when I force a full network outage (disconnect Wi-Fi). It only raise an error after 30s caused by DNS operation timeout. I think timeouts provided on MongoClient() construction aren't being considered when doing resolver.query().

       

      code:

      ...

      def setup_connection(self):
          self.connection = MongoClient(self.conn_string,
                                  connect=False,
                                  socketTimeoutMS=self.timeoutMS,
                                  connectTimeoutMS=self.timeoutMS,
                              serverSelectionTimeoutMS=self.timeoutMS)
          self.connection.admin.command('ismaster')

      ...

      try:
          self.setup_connection()
      {{ except ConnectionFailure:}}
          ...

       

      Traceback:

      [2019-07-15 03:21:12 +0000] [841] [ERROR] Error handling request /tp1/v1.0/feed/insert
      Traceback (most recent call last):
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/pymongo/uri_parser.py", line 254, in _get_dns_srv_hosts}}
      {{ results = resolver.query('_mongodb._tcp.' + hostname, 'SRV')}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/dns/resolver.py", line 1102, in query}}
      {{ lifetime)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/dns/resolver.py", line 900, in query}}
      {{ timeout = self._compute_timeout(start, lifetime)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/dns/resolver.py", line 799, in _compute_timeout}}
      {{ raise Timeout(timeout=duration)}}
      dns.exception.Timeout: The DNS operation timed out after 30.00044846534729 seconds

      During handling of the above exception, another exception occurred:Traceback (most recent call last):
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 56, in handle}}
      {{ self.handle_request(listener_name, req, client, addr)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/gunicorn/workers/ggevent.py", line 160, in handle_request}}
      {{ addr)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 107, in handle_request}}
      {{ respiter = self.wsgi(environ, resp.start_response)}}
      {{ File "/mnt/c/Users/arthu/Documents/the-player-one/gunicorn/main.py", line 233, in app}}
      {{ return api.exec()}}
      {{ File "/mnt/c/Users/arthu/Documents/the-player-one/gunicorn/main.py", line 367, in exec}}
      {{ return self.insert()}}
      {{ File "/mnt/c/Users/arthu/Documents/the-player-one/gunicorn/main.py", line 347, in insert}}
      {{ self.setup_connection()}}
      {{ File "/mnt/c/Users/arthu/Documents/the-player-one/gunicorn/main.py", line 333, in setup_connection}}
      {{ self.MongoDB.setup_connection()}}
      {{ File "/mnt/c/Users/arthu/Documents/the-player-one/gunicorn/main.py", line 312, in setup_connection}}
      {{ serverSelectionTimeoutMS=self.timeoutMS)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 524, in _init_}}
      {{ res = uri_parser.parse_uri(entity, port, warn=True)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/pymongo/uri_parser.py", line 366, in parse_uri}}
      {{ nodes = _get_dns_srv_hosts(fqdn)}}
      {{ File "/home/arthur/.local/lib/python3.6/site-packages/pymongo/uri_parser.py", line 256, in _get_dns_srv_hosts}}
      {{ raise ConfigurationError(str(exc))}}
      pymongo.errors.ConfigurationError: The DNS operation timed out after 30.00044846534729 seconds

            Assignee:
            prashant.mital Prashant Mital (Inactive)
            Reporter:
            arthur.simas@dcomp.sor.ufscar.br Arthur Simas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: