Search code examples
flaskgoogle-app-enginegoogle-cloud-platformgoogle-app-engine-python

App Engine Flask App: Process terminated because the request deadline was exceeded. (Error code 123)


Im trying to deploy a flask app to App Engine standard.

The app works fine on my local machine.

It also deploys to app engine ok.

However when I request a URL, the page is not loading and the following message is appearing in the logs:

App Engine: Process terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the port defined by the PORT environment variable. (Error code 123)

I wont share the code for the entire app but I'm using the following app.run command in it:

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

Also, below are all log messages up to the point of the deadline exceeded message in GCP logs explorer:

2022-07-26 11:47:00.542 BST
App Initialising

2022-07-26 11:47:00.544 BST
 * Serving Flask app 'app' (lazy loading)

2022-07-26 11:47:00.544 BST
 * Environment: production

2022-07-26 11:47:00.544 BST
 WARNING: This is a development server. Do not use it in a production deployment.

2022-07-26 11:47:00.544 BST
 Use a production WSGI server instead.

2022-07-26 11:47:00.544 BST
 * Debug mode: off

2022-07-26 11:47:00.556 BST
 * Running on all addresses (0.0.0.0)

2022-07-26 11:47:00.556 BST
 WARNING: This is a development server. Do not use it in a production deployment.

2022-07-26 11:47:00.556 BST
 * Running on http://127.0.0.1:5000

2022-07-26 11:47:00.556 BST
 * Running on http://169.254.8.1:5000 (Press CTRL+C to quit)

2022-07-26 11:49:31.899 BST
[start] 2022/07/26 10:49:31.898979 Quitting on terminated signal

2022-07-26 11:49:31.899 BST
[start] 2022/07/26 10:49:31.899629 Start program failed: failed to detect app after start: ForAppStart(): [aborted, context canceled. subject:"app/valid" Timeout:30m0s, attempts:119910 aborted, context canceled. subject:"app/invalid" Timeout:30m0s, attempts:119886]

Here I test GET requesting the apps homepage:

2022-07-26 11:50:43.136 BST

GET 500 301.663 s Chrome 103.0.0.0 /
188.221.25.131 - - [26/Jul/2022:03:50:43 -0700] GET / HTTP/1.1 500 - - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" "<<app url>>" ms=301663 cpu_ms=0 cpm_usd=0 loading_request=0 instance=00c61b117cc57f1885aa1e4f9a85f795df32ab91d3ed079eeaf27721ad7817ec20ebabd30ca9d4226dd67175724a624eba33cd2da0023671d18f app_engine_release=1.9.71 trace_id=2ef242690a4b068b13493764e6279eb2

Resulting in time exceeded message:

**2022-07-26 11:55:44.799 BST
Process terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the port defined by the PORT environment variable. (Error code 123)**

Does anyone know what might be the issue? Or can anyone point me in the right direction for further exploration?


Solution

  • Port value on app.run must be changed to 8080, as shown in this @John Hanley comment:

    The default port is 8080. Flask listens on port 5000. Change your code:

    app.run(host='0.0.0.0', port=8080)
    

    As shown in the Flask documentation, Flask runs on port 5000:

    To run the application, use the flask command...

    $ flask run
    * Running on http://127.0.0.1:5000/
    

    Therefore, it must be changed to run on port 8080, as stated in the Create a Python app in the App Engine flexible environment documentation sample code:

    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    # Flask's development server will automatically serve static files in
    # the "static" directory. See:
    # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
    # App Engine itself will serve those files as configured in app.yaml.
    app.run(host='127.0.0.1', port=8080, debug=True)