<p dir="auto">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).</p>
<p dir="auto">Here is my code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#!/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=[&quot;180 per day&quot;,&quot;30 per hour&quot;]
)

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

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

    d = { &quot;result&quot;:[
                {&quot;id&quot;:&quot;1&quot;,&quot;name&quot;:&quot;John&quot;,&quot;email&quot;:&quot;john-flask@gmail.com&quot;},
                {&quot;id&quot;:&quot;2&quot;,&quot;name&quot;:&quot;NIkolas&quot;,&quot;email&quot;:&quot;nikolas-flask@gmail.com&quot;},
        {&quot;id&quot;:&quot;3&quot;,&quot;name&quot;:&quot;Samanta&quot;,&quot;email&quot;:&quot;samanta-flask@gmail.com&quot;}
    ]}
    return jsonify(d)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=80, debug=True)"><pre class="notranslate"><span class="pl-c">#!/usr/bin/python</span>
<span class="pl-c"># -*- coding: utf-8 -*-</span>

<span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>, <span class="pl-s1">jsonify</span>
<span class="pl-k">from</span> <span class="pl-s1">flask_limiter</span> <span class="pl-k">import</span> <span class="pl-v">Limiter</span>
<span class="pl-k">from</span> <span class="pl-s1">flask_limiter</span>.<span class="pl-s1">util</span> <span class="pl-k">import</span> <span class="pl-s1">get_remote_address</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">Flask</span>(<span class="pl-s1">__name__</span>)
<span class="pl-s1">app</span>.<span class="pl-s1">config</span>[<span class="pl-s">'JSON_AS_ASCII'</span>] <span class="pl-c1">=</span> <span class="pl-c1">False</span>

<span class="pl-s1">limiter</span> <span class="pl-c1">=</span> <span class="pl-v">Limiter</span> (
    <span class="pl-s1">app</span>,
    <span class="pl-s1">key_func</span><span class="pl-c1">=</span><span class="pl-s1">get_remote_address</span>,
    <span class="pl-s1">global_limits</span><span class="pl-c1">=</span>[<span class="pl-s">"180 per day"</span>,<span class="pl-s">"30 per hour"</span>]
)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-s1">after_request</span></span>
<span class="pl-k">def</span> <span class="pl-en">server_response</span>(<span class="pl-s1">response</span>):
    <span class="pl-s1">response</span>.<span class="pl-s1">headers</span>[<span class="pl-s">"Server"</span>] <span class="pl-c1">=</span> <span class="pl-s">"WS"</span>
    <span class="pl-k">return</span> <span class="pl-s1">response</span>

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">'/json'</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">f_json</span>():

    <span class="pl-s1">d</span> <span class="pl-c1">=</span> { <span class="pl-s">"result"</span>:[
                {<span class="pl-s">"id"</span>:<span class="pl-s">"1"</span>,<span class="pl-s">"name"</span>:<span class="pl-s">"John"</span>,<span class="pl-s">"email"</span>:<span class="pl-s">"john-flask@gmail.com"</span>},
                {<span class="pl-s">"id"</span>:<span class="pl-s">"2"</span>,<span class="pl-s">"name"</span>:<span class="pl-s">"NIkolas"</span>,<span class="pl-s">"email"</span>:<span class="pl-s">"nikolas-flask@gmail.com"</span>},
        {<span class="pl-s">"id"</span>:<span class="pl-s">"3"</span>,<span class="pl-s">"name"</span>:<span class="pl-s">"Samanta"</span>,<span class="pl-s">"email"</span>:<span class="pl-s">"samanta-flask@gmail.com"</span>}
    ]}
    <span class="pl-k">return</span> <span class="pl-en">jsonify</span>(<span class="pl-s1">d</span>)

<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-s1">app</span>.<span class="pl-en">run</span>(<span class="pl-s1">host</span><span class="pl-c1">=</span><span class="pl-s">'0.0.0.0'</span>, <span class="pl-s1">port</span><span class="pl-c1">=</span><span class="pl-c1">80</span>, <span class="pl-s1">debug</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)</pre></div>
<p dir="auto">Here is what I found in the debug, which caused the Traceback (and Flask was still running, but not answering any request).</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="191.96.249.231 - - [30/Jun/2016 19:11:47] &quot;GET /plugins/system/plugin_googlemap2/plugin_googlemap2_proxy.php?url=http://191.96.249.231/shodanhq HTTP/1.0&quot; 404 -
Exception in thread Thread-2:
Traceback (most recent call last):
  File &quot;/usr/lib/python2.7/threading.py&quot;, line 810, in __bootstrap_inner
    self.run()
  File &quot;/usr/lib/python2.7/threading.py&quot;, line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 659, in inner
    srv.serve_forever()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 499, in serve_forever
    HTTPServer.serve_forever(self)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 238, in serve_forever
    self._handle_request_noblock()
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 297, in _handle_request_noblock
    self.handle_error(request, client_address)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 321, in process_request
    self.finish_request(request, client_address)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 649, in __init__
    self.handle()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 216, in handle
    rv = BaseHTTPRequestHandler.handle(self)
  File &quot;/usr/lib/python2.7/BaseHTTPServer.py&quot;, line 340, in handle
    self.handle_one_request()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 251, in handle_one_request
    return self.run_wsgi()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 193, in run_wsgi
    execute(self.server.app)
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 184, in execute
    write(data)
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 149, in write
    self.send_response(int(code), msg)
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 260, in send_response
    self.wfile.write(hdr.encode('ascii'))
IOError: [Errno 32] Broken pipe"><pre class="notranslate"><code class="notranslate">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
</code></pre></div>
<p dir="auto">It also happened in this situation, but I believe that "Connection Reset by Peer" was already addressed and fixed, (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="122591771" data-permission-text="Title is private" data-url="https://github.com/miguelgrinberg/Flask-SocketIO/issues/184" data-hovercard-type="issue" data-hovercard-url="/miguelgrinberg/Flask-SocketIO/issues/184/hovercard" href="https://github.com/miguelgrinberg/Flask-SocketIO/issues/184">miguelgrinberg/Flask-SocketIO#184</a>), but I believe it is worth to be mentioned regarding the data which was requested to Flask (it really caught my attention...)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="169.229.3.91 - - [29/Jun/2016 09:12:37] &quot;0&lt;B0&gt;$e&lt;AE&gt;x&lt;D4&gt;^?&lt;AB&gt;&lt;AC&gt;1&lt;E8&gt;&lt;CF&gt;Oۚ&lt;9C&gt;^K@&lt;BA&gt;&lt;D2&gt;&lt;FE&gt;&lt;D4&gt;q_^]i=AS%&lt;D7&gt;`I&lt;CA&gt;`&lt;8F&gt;&lt;C8&gt;6&lt;E5&gt;E^SX)B&lt;A5&gt;-{^RoK^BT&lt;E4&gt;&lt;82&gt;}O'\&lt;BB&gt;&lt;8B&gt;&lt;E4&gt;a&lt;86&gt;&quot; 400 -
Exception in thread Thread-2:
Traceback (most recent call last):
  File &quot;/usr/lib/python2.7/threading.py&quot;, line 810, in __bootstrap_inner
    self.run()
  File &quot;/usr/lib/python2.7/threading.py&quot;, line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 659, in inner
    srv.serve_forever()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 499, in serve_forever
    HTTPServer.serve_forever(self)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 238, in serve_forever
    self._handle_request_noblock()
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 297, in _handle_request_noblock
    self.handle_error(request, client_address)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 321, in process_request
    self.finish_request(request, client_address)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File &quot;/usr/lib/python2.7/SocketServer.py&quot;, line 649, in __init__
    self.handle()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 216, in handle
    rv = BaseHTTPRequestHandler.handle(self)
  File &quot;/usr/lib/python2.7/BaseHTTPServer.py&quot;, line 340, in handle
    self.handle_one_request()
  File &quot;/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py&quot;, line 247, in handle_one_request
    self.raw_requestline = self.rfile.readline()
IOError: [Errno 104] Connection reset by peer"><pre class="notranslate"><code class="notranslate">169.229.3.91 - - [29/Jun/2016 09:12:37] "0&lt;B0&gt;$e&lt;AE&gt;x&lt;D4&gt;^?&lt;AB&gt;&lt;AC&gt;1&lt;E8&gt;&lt;CF&gt;Oۚ&lt;9C&gt;^K@&lt;BA&gt;&lt;D2&gt;&lt;FE&gt;&lt;D4&gt;q_^]i=AS%&lt;D7&gt;`I&lt;CA&gt;`&lt;8F&gt;&lt;C8&gt;6&lt;E5&gt;E^SX)B&lt;A5&gt;-{^RoK^BT&lt;E4&gt;&lt;82&gt;}O'\&lt;BB&gt;&lt;8B&gt;&lt;E4&gt;a&lt;86&gt;" 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
</code></pre></div>
<p dir="auto">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..</p>
<p dir="auto">I have a Werkzeug server running behind NGINX. When a client disconnects while waiting for the Werkzeug server to respond, NGINX closes the pipe to Werkzeug. When the python program writes the response to Werkzeug, the following exception occurs and Werkzeug crashes:</p>
<blockquote>
<p dir="auto">Traceback (most recent call last):<br>
File "server.py", line 81, in<br>
app.run(host=args.host, port=args.port, debug=False)<br>
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 843, in run<br>
run_simple(host, port, self, **options)<br>
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 694, in run_simple<br>
inner()<br>
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 659, in inner<br>
srv.serve_forever()<br>
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 499, in serve_forever<br>
HTTPServer.serve_forever(self)<br>
File "/usr/lib/python2.7/SocketServer.py", line 238, in serve_forever<br>
self._handle_request_noblock()<br>
File "/usr/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock<br>
self.handle_error(request, client_address)<br>
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock<br>
self.process_request(request, client_address)<br>
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request<br>
self.finish_request(request, client_address)<br>
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request<br>
self.RequestHandlerClass(request, client_address, self)<br>
File "/usr/lib/python2.7/SocketServer.py", line 651, in init<br>
self.finish()<br>
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish<br>
self.wfile.close()<br>
File "/usr/lib/python2.7/socket.py", line 279, in close<br>
self.flush()<br>
File "/usr/lib/python2.7/socket.py", line 303, in flush<br>
self._sock.sendall(view[write_offset:write_offset+buffer_size])<br>
socket.error: [Errno 32] Broken pipe</p>
</blockquote>
<p dir="auto">Is there some configuration option I'm missing to keep it from crashing? Normally all exceptions are caught and a 500 error returned, with the server remaining alive.</p>