Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - socket.error: [Errno 10053] An established connection was aborted by the software in your host machine [duplicate]

Re-opening this question upon request (error: [Errno 10053]), providing the minimal testable example:

import time
from flask import Flask, render_template
app = Flask(__name__, static_folder='static', template_folder='templates')

@app.route('/')
def main():
    return render_template('test.html')

@app.route('/test')
def test():
    print "Sleeping. Hit Stop button in browser now"
    time.sleep(10)
    print "Woke up. You should see a stack trace from the problematic exception below."
    return render_template('test.html')

if __name__ == '__main__':
    app.run()

HTML:

<html>
<body>
<a href="/test">test</a>
</body>
</html>

Guide: Run the app, navigate to localhost:port, click on the link, then hit Stop button in your browser. You should see the exception once the sleep finishes. The sleep is necessary to simulate any sort of activity happening on the server. It could be just a few seconds: if user manages to navigate away from the page - Flask will crash.

socket.error: [Errno 10053] An established connection was aborted by the software in your host machine

Why does the server stop serving the application? What other server can I use for my Flask application to avoid this?

like image 392
tonysepia Avatar asked Oct 25 '16 18:10

tonysepia


People also ask

How do you fix an established connection was aborted by the software in your host machine?

Answers. Moty is right, in most of the cases this exception indicates something on your machine is blocking your connection. Try running it with firewall/antivirus disabled. This is not permanent, just for testing if this is setup issue or code issue.

What is ConnectionAbortedError?

This error message... ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine. ... implies that the initialization of a new WebBrowsing Session i.e. Firefox Browser session was aborted.


1 Answers

This is an issue with the Python 2 implementation of the SocketServer module, it is not present in Python 3 (where the server keeps on serving).

Your have 3 options:

  • Don't use the built-in server for production systems (it is a development server after all). Use a proper WSGI server like gunicorn or uWSGI,
  • Enable threaded mode with app.run(threaded=True); the thread dies but a new one is created for future requests,
  • Upgrade to Python 3.
like image 110
Martijn Pieters Avatar answered Sep 20 '22 13:09

Martijn Pieters