flask icon indicating copy to clipboard operation
flask copied to clipboard

Flask fails to reload upon Traceback

Open ivanleoncz opened this issue 10 years ago • 3 comments

I have the following Flask app running in a Stage server, and I realized that some requests were affecting its operation, causing Tracebacks and with this, the Flask app was still running as a process, but not answering any sort of request, neither giving a kind of Internal Server Error (5xx).

Here is my code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from flask import Flask, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

limiter = Limiter (
    app,
    key_func=get_remote_address,
    global_limits=["180 per day","30 per hour"]
)

@app.after_request
def server_response(response):
    response.headers["Server"] = "WS"
    return response

@app.route('/json')
def f_json():

    d = { "result":[
                {"id":"1","name":"John","email":"[email protected]"},
                {"id":"2","name":"NIkolas","email":"[email protected]"},
        {"id":"3","name":"Samanta","email":"[email protected]"}
    ]}
    return jsonify(d)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)

Here is what I found in the debug, which caused the Traceback (and Flask was still running, but not answering any request).

191.96.249.231 - - [30/Jun/2016 19:11:47] "GET /plugins/system/plugin_googlemap2/plugin_googlemap2_proxy.php?url=http://191.96.249.231/shodanhq HTTP/1.0" 404 -
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 659, in inner
    srv.serve_forever()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 499, in serve_forever
    HTTPServer.serve_forever(self)
  File "/usr/lib/python2.7/SocketServer.py", line 238, in serve_forever
    self._handle_request_noblock()
  File "/usr/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock
    self.handle_error(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 216, in handle
    rv = BaseHTTPRequestHandler.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 251, in handle_one_request
    return self.run_wsgi()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 193, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 184, in execute
    write(data)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 149, in write
    self.send_response(int(code), msg)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 260, in send_response
    self.wfile.write(hdr.encode('ascii'))
IOError: [Errno 32] Broken pipe

It also happened in this situation, but I believe that "Connection Reset by Peer" was already addressed and fixed, (https://github.com/miguelgrinberg/Flask-SocketIO/issues/184), but I believe it is worth to be mentioned regarding the data which was requested to Flask (it really caught my attention...)

169.229.3.91 - - [29/Jun/2016 09:12:37] "0<B0>$e<AE>x<D4>^?<AB><AC>1<E8><CF>Oۚ<9C>^K@<BA><D2><FE><D4>q_^]i=AS%<D7>`I<CA>`<8F><C8>6<E5>E^SX)B<A5>-{^RoK^BT<E4><82>}O'\<BB><8B><E4>a<86>" 400 -
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 659, in inner
    srv.serve_forever()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 499, in serve_forever
    HTTPServer.serve_forever(self)
  File "/usr/lib/python2.7/SocketServer.py", line 238, in serve_forever
    self._handle_request_noblock()
  File "/usr/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock
    self.handle_error(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 216, in handle
    rv = BaseHTTPRequestHandler.handle(self)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 247, in handle_one_request
    self.raw_requestline = self.rfile.readline()
IOError: [Errno 104] Connection reset by peer

I believe that it would be interesting to have a kind of "recovery" for Flask, or a better way to deal with it, without staying hanged..

ivanleoncz avatar Jul 01 '16 16:07 ivanleoncz

Encountering the same problem in Server Sent Events

gnanam336 avatar Jul 05 '16 04:07 gnanam336

duplicate of pallets/werkzeug#954

davidism avatar Jul 05 '16 15:07 davidism

Thank you @davidism

ivanleoncz avatar Jul 05 '16 16:07 ivanleoncz