-
Type: Bug
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
-
Component/s: API, Config, Connection Mgmt, Error Handling
-
None
-
Environment:Python:
version 3.6.9 (default, Jul 3 2019, 07:38:46)
[GCC 8.3.0]
PyMongo:
version 3.8.0
has_c(): True
Gunicorn 19.9.0
gevent 1.4.0
Operational System:
Windows 10 Pro
Version 1809
Build 17763.615
Runnion on:
WSL GNU/Linux
Kernel release: 4.4.0-17763-Microsoft
Kernel version: #379-Microsoft Wed Mar 06 19:16:00 PST 2019
Python: version 3.6.9 (default, Jul 3 2019, 07:38:46) [GCC 8.3.0] PyMongo: version 3.8.0 has_c(): True Gunicorn 19.9.0 gevent 1.4.0 Operational System: Windows 10 Pro Version 1809 Build 17763.615 Runnion on: WSL GNU/Linux Kernel release: 4.4.0-17763-Microsoft Kernel version: #379-Microsoft Wed Mar 06 19:16:00 PST 2019
-
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