Flask not processing other HTTP requests after Chrome browser accesses the web-site

Enable threading.

app.run(host='0.0.0.0', port=80, debug=True, threaded=True)

TL;DR

The problem is still valid. It seems that Chrome does not close the connection when page prefetch is enabled, and it blocks the execution of the server, hence the processing of subsequent requests.

In my case, the problem even worst since Android-based phones also use this prefetch feature with the same results, and I can not change the settings every client.

My solution/workaround is to enable the threading option in the underlying werkzeug server (https://werkzeug.palletsprojects.com/en/0.16.x/serving/#werkzeug.serving.run_simple). Of course, it is more resources heavy on the server-side, but it allows us to separate the ill behaving requests/clients in a separate thread without blocking other requests.

if __name__ == '__main__':
    logger.info('starting web server life cycle')
    app.run(host='0.0.0.0', port=80, debug=True, threaded=True)

I also checked that the request processing is finished correctly, and it does, at least in the Flask side. So the problem must be either in Chrome / other clients or in the underlying werkzeug server.

@app.before_request
def filter_prefetch():
    logger.debug("before request")
    logger.debug(request.headers)
# uncomment these to filter Chrome specific prefetch requests.
#    if 'Purpose' in request.headers and request.headers.get('Purpose') == 'prefetch':
#        logger.debug("prefetch requests are not allowed")
#        return '', status.HTTP_403_FORBIDDEN


@app.after_request
def debug_after(response):
    logger.debug("after request")
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "0"
    response.headers['Cache-Control'] = 'public, max-age=0'
    response.headers['Connection'] = 'close'
    return response


I've ran into the same problem twice.

The same environment: pure Flask (no reverse proxy), the simplest application.

After you've open URL with Chrome/Chromium -- Flask will hang and won't respond to other clients (curl, postman, firefox, python-request, ..).

Workaround for Chrome

Disable URL-prediction services in Chrome/Chromium (Actual names of options are on the screenshot)

chromium-settings

Real solution (for Flask)

Comming soon (contributions are welcome!).