8

I run a simple flask app like this:

from flask import Flask

app = Flask(__name__) 

@app.route('/')
def welcome():
    return "OK"


app.config.update(
    DEBUG = True
)

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

when I run it and visit it, sometimes(not always) it could't response the request and throw an except:

Exception happened during processing of request from ('127.0.0.1', 54481)
Traceback (most recent call last):
  File "c:\python27\Lib\SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "c:\python27\Lib\SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "c:\python27\Lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python27\Lib\SocketServer.py", line 651, in __init__
    self.finish()
  File "c:\python27\Lib\SocketServer.py", line 710, in finish
    self.wfile.close()
  File "c:\python27\Lib\socket.py", line 279, in close
    self.flush()
  File "c:\python27\Lib\socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053]

I can't understand what cause this fault? and how can I solve it?

and how can I use try except to catch it?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
hh54188
  • 14,887
  • 32
  • 113
  • 184

3 Answers3

11

I recently ran into this error message while trying to use Flask to serve audio files. I get this error message whenever the client closes the stream before the end of the stream.

This error message doesn't originate from your Flask application, but rather from the underlying SocketServer used to dispatch request data. What is happening is the connection to the client is ending for some reason, but Flask continues to try to write data to the closed socket. You can't catch this exception from your Flask application, because Flask catches it for you. Flask prints it out as a service to you, notifying you that the stream was closed prematurely, i.e. before Flask finished writing data to the stream.

To sum it up, this error message is internal to Flask, Flask is printing it to tell you that it couldn't get all the data to the client before the connection closed. You can't catch it, and you shouldn't have any reason to catch it.

Nop
  • 188
  • 2
  • 9
  • 2
    That's correct. It is not really a flask error, but an error of the internal web server that it could not send all data to the client because the connection was closed. This can happen if you press reload fast or stop while the page is still loading. You should not use the internal server in production, everything is fine. – Bastian Jul 14 '14 at 08:17
  • Thanks for this explanation. It was getting me crazy. So it is not a bug, tough the messages are annoying. @user305883 I only see a purple ghost between a big 404 when clicking on that link. – muammar May 03 '16 at 09:03
  • @muammar https://www.odoo.com/forum/help-1/question/self-sock-sendall-broken-pipe-means-28296 try again if useful. – user305883 May 03 '16 at 11:06
  • Happened for me with ` – Bob Stein Feb 14 '18 at 16:56
  • @BobStein-VisiBone Last I checked 4 years ago, you can't catch it. Flask does not throw it to you. You could fix the issue in Flask though. I don't write servers in python these days so likely I won't get around to it. Wherever Flask tries to write to the Socket object, the call should guard against this exception and close/remove the Socket object upon this failure. Maybe log the event at debug or info level. – Nop Feb 16 '18 at 04:17
0

I've found this solution to be a good at least temporary fix.

if __name__ == '__main__':
  while True:
    try:
       app.run(use_reloader = False)
    except:
      pass

You can add your own exit logic, or leave the program with CTRL + \ which sends SIGQUIT. (important if you're running threaded flask)

You however can't:

   except KeyboardInterupt:

Because Flask already catches KeyboardInterupt exceptions and handles them.

error 10052 means you're using windows, so as far as I know, close the command window to exit the program

-3

It is probably due to the port number being used which is 54481 by looking at your error message. It might be clashing with something else. I also suggest not to use the use_reloader parameter since your DEBUG is already set to False. So flask will not reload any code changes. Can you instead do this :

if __name__ == '__main__':
    app.run(port=5000)
codegeek
  • 32,236
  • 12
  • 63
  • 63
  • 3
    I don't think that your answer is correct, because the port 54481 is the port of the request (i.e. from the web-browser). – HolgerSchurig Jul 22 '13 at 13:32
  • 1
    But by default flask use 5000 port: https://github.com/mitsuhiko/flask/blob/master/flask/app.py#L766. In your example your app should not work with 54481 port for dev server. – tbicr Jul 22 '13 at 13:53
  • FWIW, I have run into the same problem, I use a custom port and I have debug turned off. I think suggesting a different port is a red herring. – Bryan Oakley Aug 29 '14 at 14:34