'Max retries exceeded with url Caused by NewConnectionError Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

I am requesting an API using the python requests library:

My python script is run once a day by the scheduler, Once the python script gets run, I am getting this error and the PID of the python script is getting killed showing OOM. I am not getting whether it's a DNS issue or an OOM (Out of memory) issue as the process is getting killed.

Previously script was running fine.

Any clues/help will be highly appreciable.

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 170, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/connection.py", line 73, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connection.py", line 182, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f163156c160>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/ubuntu/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='<.............>', port=443): Max retries exceeded with url: /api/v2/test_connection/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f163156c160>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ubuntu/pf_cic_etl_script_v2.py", line 50, in <module>
    resp = requests.get(url, headers=headers)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='<...............>', port=443): Max retries exceeded with url: /api/v2/test_connection/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f163156c160>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
Killed

Process kill logs-

dmesg -T| grep -E -i -B100 'killed process'
[Sat Sep 25 06:08:31 2021] Tasks state (memory values in pages):
[Sat Sep 25 06:08:31 2021] [  pid  ]   uid  tgid total_vm      rss pgtables_bytes swapents oom_score_adj name
[Sat Sep 25 06:08:31 2021] [   1643]  1000  1643   517804   455512  4157440        0             0 python3
[Sat Sep 25 06:08:31 2021] [   1651]     0  1651     5954       69    86016        0             0 apport
[Sat Sep 25 06:08:31 2021] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice,task=python3,pid=1643,uid=1000
[Sat Sep 25 06:08:31 2021] Out of memory: Killed process 1643 (python3) total-vm:2071216kB, anon-rss:1822048kB, file-rss:0kB, shmem-rss:0kB, UID:1000 pgtables:4060kB oom_score_adj:0


Solution 1:[1]

I also had such an error in my Hostinger 20 GB VPS while I was trying to run my letsencrypt script. The issue is directly related to the disk running out. I run the below command to remove all docker containers and claimed 17.4 GB. The issue has been resolved afterward:

sudo docker system prune --all

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zekarias Taye Hirpo