Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Django Errno 54 'Connection reset by peer'

Having some trouble debugging this. I get this error always when i first start my app up, then intermittently thereafter. Could someone please help me by throwing out some debugging techniques? I've tried using a proxy inspector - to no avail, i didn't see anything useful. I've tried the suggestions about setting my SITE_URL in my django settings. I've tried with and without http:// with and without the port... Here's the unhelpful error:

Exception happened during processing of request from ('127.0.0.1', 57917)
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 720, in __init__
    self.handle()
  File "/Users/ryan/.local/share/virtualenvs/portal-2PUjdB8V/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 171, in handle
    self.handle_one_request()
  File "/Users/ryan/.local/share/virtualenvs/portal-2PUjdB8V/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 179, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer

The app seems to function properly even with this connection reset but it's been driving me crazy trying to debug.

like image 453
archae0pteryx Avatar asked Jul 29 '19 20:07

archae0pteryx


3 Answers

FFS... so dumb. I noticed that it was always resetting after not finding a favicon so I added one... Even though I never explicitly loaded one, django appears to try and load a default one from the root of the project... This doesn't happen for any of the other devs working on the project either. weird. (For completeness) If anyone else stumbles upon this i used favicon io to make a simple text one. Then i loaded it into my html like so:

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="shortcut icon" href="{% static 'images/favicon.ico' %}" />
...

Be sure to set your static path correctly in settings.

like image 114
archae0pteryx Avatar answered Nov 14 '22 06:11

archae0pteryx


The same behaviour is seen if the favicon is in .png format as opposed to .ico.

Also, contrary to advice seen on other sites, downgrading Python to v3.6 does not solve the problem. screenshot of error w. png favicon

Seems to be a Django issue, It will probably be fixed permanently in a future Django release.

Following https://bugs.python.org/issue27682#msg348302 I made the changes shown: code changes

I then replaced BrokenPipeError with ConnectionAbortedError. This seems to handle the exception.

Update for Django 3: I have the following changes in basehttp.py (rev.1.2.1) to get rid of all the pesky broken pipe error messages:

Line 55: - return issubclass(exc_type, BrokenPipeError)
Line 55: + return issubclass(exc_type, (BrokenPipeError, ConnectionAbortedError, ConnectionResetError))

Line 71: - logger.info("- Broken pipe from %s\n", client_address)
Line 71: + pass
like image 36
Chris Avatar answered Nov 14 '22 05:11

Chris


I came across the same issue ConnectionResetError: [Errno 54] Connection reset by peer.

In my case the issue was trying to serve static files through development server with debug settings to False, development server can serve static files only when debug is set to True in settings file.

In my settings file i replace

DEBUG = False

with

DEBUG = True

After that, I restart my development server and it works for me!

like image 1
Zubair Hassan Avatar answered Nov 14 '22 04:11

Zubair Hassan