Unable capture the exception and Existing without control from application

**Getting the following error frequently sometimes and even not able to capture inside the exception block to handle the error. Please provide help on the below issue.**

Jul 21 11:[2022-07-21 11:10:41,919] ERROR in resilientsession: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
Jul 21 11:Traceback (most recent call last):
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
Jul 21 11:httplib_response = self._make_request(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 449, in _make_request
Jul 21 11:six.raise_from(e, None)
Jul 21 11:File "<string>", line 3, in raise_from
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 444, in _make_request
Jul 21 11:httplib_response = conn.getresponse()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 1347, in getresponse
Jul 21 11:response.begin()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 307, in begin
Jul 21 11:version, status, reason = self._read_status()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 268, in _read_status
Jul 21 11:line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
Jul 21 11:File "/usr/lib64/python3.8/socket.py", line 669, in readinto
Jul 21 11:return self._sock.recv_into(b)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1241, in recv_into
Jul 21 11:return self.read(nbytes, buffer)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1099, in read
Jul 21 11:return self._sslobj.read(len, buffer)
Jul 21 11:ConnectionResetError: [Errno 104] Connection reset by peer
Jul 21 11:During handling of the above exception, another exception occurred:
Jul 21 11:Traceback (most recent call last):
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/adapters.py", line 489, in send
Jul 21 11:resp = conn.urlopen(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 785, in urlopen
Jul 21 11:retries = retries.increment(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/util/retry.py", line 550, in increment
Jul 21 11:raise six.reraise(type(error), error, _stacktrace)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/packages/six.py", line 769, in reraise
Jul 21 11:raise value.with_traceback(tb)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
Jul 21 11:httplib_response = self._make_request(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 449, in _make_request
Jul 21 11:six.raise_from(e, None)
Jul 21 11:File "<string>", line 3, in raise_from
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 444, in _make_request
Jul 21 11:httplib_response = conn.getresponse()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 1347, in getresponse
Jul 21 11:response.begin()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 307, in begin
Jul 21 11:version, status, reason = self._read_status()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 268, in _read_status
Jul 21 11:line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
Jul 21 11:File "/usr/lib64/python3.8/socket.py", line 669, in readinto
Jul 21 11:return self._sock.recv_into(b)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1241, in recv_into
Jul 21 11:return self.read(nbytes, buffer)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1099, in read
Jul 21 11:return self._sslobj.read(len, buffer)
Jul 21 11:urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
Jul 21 11:During handling of the above exception, another exception occurred:
Jul 21 11:Traceback (most recent call last):
Jul 21 11:File "/var/app/current/src/resilientsession.py", line 34, in request
Jul 21 11:r = super(ResilientSession, self).request(method, url, **kwargs)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/sessions.py", line 587, in request
Jul 21 11:resp = self.send(prep, **send_kwargs)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/sessions.py", line 701, in send
Jul 21 11:r = adapter.send(request, **kwargs)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/adapters.py", line 547, in send
Jul 21 11:raise ConnectionError(err, request=request)
Jul 21 11:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
Jul 21 11:[2022-07-21 11:10:41,919] ERROR in resilientsession: Got recoverable error [ConnectionError] from POST https://qaecs-wwwwww.zzzzz.com/calculations/execute?request_id=input_XXXXX_XXXSanity_35, retry #1 in 0s
Jul 21 11:Traceback (most recent call last):
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
Jul 21 11:httplib_response = self._make_request(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 449, in _make_request
Jul 21 11:six.raise_from(e, None)
Jul 21 11:File "<string>", line 3, in raise_from
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 444, in _make_request
Jul 21 11:httplib_response = conn.getresponse()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 1347, in getresponse
Jul 21 11:response.begin()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 307, in begin
Jul 21 11:version, status, reason = self._read_status()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 268, in _read_status
Jul 21 11:line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
Jul 21 11:File "/usr/lib64/python3.8/socket.py", line 669, in readinto
Jul 21 11:return self._sock.recv_into(b)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1241, in recv_into
Jul 21 11:return self.read(nbytes, buffer)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1099, in read
Jul 21 11:return self._sslobj.read(len, buffer)
Jul 21 11:ConnectionResetError: [Errno 104] Connection reset by peer
Jul 21 11:During handling of the above exception, another exception occurred:
Jul 21 11:Traceback (most recent call last):
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/adapters.py", line 489, in send
Jul 21 11:resp = conn.urlopen(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 785, in urlopen
Jul 21 11:retries = retries.increment(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/util/retry.py", line 550, in increment
Jul 21 11:raise six.reraise(type(error), error, _stacktrace)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/packages/six.py", line 769, in reraise
Jul 21 11:raise value.with_traceback(tb)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
Jul 21 11:httplib_response = self._make_request(
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 449, in _make_request
Jul 21 11:six.raise_from(e, None)
Jul 21 11:File "<string>", line 3, in raise_from
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/urllib3/connectionpool.py", line 444, in _make_request
Jul 21 11:httplib_response = conn.getresponse()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 1347, in getresponse
Jul 21 11:response.begin()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 307, in begin
Jul 21 11:version, status, reason = self._read_status()
Jul 21 11:File "/usr/lib64/python3.8/http/client.py", line 268, in _read_status
Jul 21 11:line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
Jul 21 11:File "/usr/lib64/python3.8/socket.py", line 669, in readinto
Jul 21 11:return self._sock.recv_into(b)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1241, in recv_into
Jul 21 11:return self.read(nbytes, buffer)
Jul 21 11:File "/usr/lib64/python3.8/ssl.py", line 1099, in read
Jul 21 11:return self._sslobj.read(len, buffer)
Jul 21 11:urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
Jul 21 11:During handling of the above exception, another exception occurred:
Jul 21 11:Traceback (most recent call last):
Jul 21 11:File "/var/app/current/src/resilientsession.py", line 34, in request
Jul 21 11:r = super(ResilientSession, self).request(method, url, **kwargs)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/sessions.py", line 587, in request
Jul 21 11:resp = self.send(prep, **send_kwargs)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/sessions.py", line 701, in send
Jul 21 11:r = adapter.send(request, **kwargs)
Jul 21 11:File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/requests/adapters.py", line 547, in send
Jul 21 11:raise ConnectionError(err, request=request)
Jul 21 11:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

**code**
from requests import Session
import time
import logging
import requests
from requests.exceptions import ConnectionError
from urllib3.exceptions import MaxRetryError, ProtocolError

class ResilientSession(Session):
    RETRY_COUNT = 4

    def request(self, method, url, **kwargs):
        counter = 0 
        delay = 0

        while counter < ResilientSession.RETRY_COUNT:
            counter += 1
            try:
                r = super(ResilientSession, self).request(method, url, **kwargs)
                if r.status_code in [ 502, 503, 504 ]:
                    delay = 10 * counter
                    logging.warn("Got recoverable error [%s] from %s %s, retry #%s in %ss" % (r.status_code, method, url, counter, delay))
                    time.sleep(delay)
                    continue
            except (ConnectionResetError, ConnectionError, MaxRetryError, ProtocolError ) as e:
                logging.exception(e)
                logging.exception("Got recoverable error [%s] from %s %s, retry #%s in %ss" % (type(e).__name__, method, url, counter, delay))
                time.sleep(delay)
                if ResilientSession.RETRY_COUNT == counter: raise e
                continue
            # except (Exception, ) as e:
            #     logging.exception(e)
            #     logging.exception("Got unrecoverable error [%s] from %s %s, retry #%s in %ss" % (type(e).__name__, method, url, counter, delay))
            #     raise e
            return r

C:\>python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "charset_normalizer": {
    "version": "2.0.10"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.3"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.8.8"
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.28.1"
  },
  "system_ssl": {
    "version": "1010109f"
  },
  "urllib3": {
    "version": "1.26.8"
  },
  "using_charset_normalizer": false,
  "using_pyopenssl": false
}

Hi @gudipudipradeep, it looks like this is a request for help. The error being received is due to the server resetting the connection unexpectedly. Requests has no ability to affect this and we won't be able to provide assistance.

We'd recommend trying a platform like StackOverflow for questions like this.