'My own api throws 10054 ConnectionResetError

I delevoped an api using Express.js. I'm currently testing it in localhost, using a Python application with requests package as a Client.

For the testing phase, I'm sending a small amount of data every 5 seconds. The request body looks like this:

{
    "phase": 10,
    "date_add": "2022-02-15T14:01:43.516713",
    "sep_time": 30,
    "cyl_press": 456.75477776776563
}

The API requires authorization, which I do at the very start of the test and store it in a requests.Session object.

Testing behavior:

  • Server authorizes client successfully.
  • Client proceeds to send packages every 5 seconds.
  • At some point (most commonly after 3-10 successful requests) server closes the connection throwing the following error on the client side. No errors are thrown on the server.
Traceback (most recent call last):
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Python310\lib\http\client.py", line 1368, in getresponse
    response.begin()
  File "C:\Python310\lib\http\client.py", line 317, in begin
    version, status, reason = self._read_status()
  File "C:\Python310\lib\http\client.py", line 278, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Python310\lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] Se ha forzado la interrupción de una conexión existente por el host remoto

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\util\retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Python310\lib\http\client.py", line 1368, in getresponse
    response.begin()
  File "C:\Python310\lib\http\client.py", line 317, in begin
    version, status, reason = self._read_status()
  File "C:\Python310\lib\http\client.py", line 278, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Python310\lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'Se ha forzado la interrupción de una conexión existente por el host remoto', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\vitroboxruntime\managers\data_manager.py", line 115, in __send_data
    r = call(data.__dict__)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\vitroboxruntime\util\vitrosep_api.py", line 85, in postRuntimeData
    return __session.post(__cycle_route() + '/runtime', json=runtimedata, headers=HEADERS)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\requests\sessions.py", line 590, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "c:\Users\Vitro_13\Documents\VitroboxRuntime\.venv\lib\site-packages\requests\adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'Se ha forzado la interrupción de una conexión existente por el host remoto', None,
10054, None))

My main problem here is that I don't know why the server is closing the connection and I don't know how to debug it either. So if you have any idea why is this happening, what are the causes that can throw is error, or how do debug it, I'd really appreciate your help..

Since I don't think linking the whole API and client code here will be that useful (it's large) I will provide some extra information that might be useful:

  • At first I was not using headers for the requests, aside from the ones that are stored in the requests.Session object. Now I'm using those:
HEADERS =  {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
    "Connection": "keep-alive",
    "Content-Type": "application/json"
    }
  • API is connected to an online mariaDB database.

I'm happy to provide extra information if needed.



Sources

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

Source: Stack Overflow

Solution Source