Why does gunicorn need to restart so often in my gcloud appengine app?

Question:

I am using Flask to run an application. The application will be deployed on gcloud appengine. Currently, when I run it on my local dev machine, there is no issue. But when I run it on gcloud appengine, it appears that the gunicorn thread is being restarted quite often.

2022-11-13 08:54:13 default[20221113t165059]  Other load
2022-11-13 08:54:13 default[20221113t165059]  post.get_by_pageid
2022-11-13 08:54:13 default[20221113t165059]  Returning posts 0 to  4
2022-11-13 08:54:15 default[20221113t165059]  "GET /view/tree/61e2b6585fc8f37d73f59218? HTTP/1.1" 201
2022-11-13 08:54:15 default[20221113t165059]  [2022-11-13 08:54:15 +0000] [9] [INFO] Starting gunicorn 20.1.0
2022-11-13 08:54:15 default[20221113t165059]  [2022-11-13 08:54:15 +0000] [9] [INFO] Listening at: http://0.0.0.0:8081 (9)
2022-11-13 08:54:15 default[20221113t165059]  [2022-11-13 08:54:15 +0000] [9] [INFO] Using worker: gthread
2022-11-13 08:54:15 default[20221113t165059]  [2022-11-13 08:54:15 +0000] [19] [INFO] Booting worker with pid: 19
2022-11-13 08:54:16 default[20221113t165059]  [2022-11-13 08:54:16 +0000] [22] [INFO] Booting worker with pid: 22
2022-11-13 08:54:16 default[20221113t165059]  I1113 08:54:16.841506    22 __init__.py:52] Initializing Cloud Debugger Python agent version: 3.1
2022-11-13 08:54:16 default[20221113t165059]  I1113 08:54:16.841692    19 __init__.py:52] Initializing Cloud Debugger Python agent version: 3.1
2022-11-13 08:54:17 default[20221113t165059]  I1113 08:54:17.034071    24 gcp_hub_client.py:377] Debuggee registered successfully, ID: gcp:431135224927:32ede2785bfa47c7, 
agent ID: 636e418c-0000-2d3e-8038-089e08203644, canary mode: CANARY_MODE_ALWAYS_ENABLED
2022-11-13 08:54:17 default[20221113t165059]  I1113 08:54:17.034071    23 gcp_hub_client.py:377] Debuggee registered successfully, ID: gcp:431135224927:32ede2785bfa47c7, agent ID: 636d1d19-0000-23d2-bc11-089e082c7780, canary mode: CANARY_MODE_ALWAYS_ENABLED
2022-11-13 08:54:17 default[20221113t165059]  secureCheckLoggedIn
2022-11-13 08:54:17 default[20221113t165059]  SCLI Not Logged In
2022-11-13 08:54:17 default[20221113t165059]  view tree
2022-11-13 08:54:17 default[20221113t165059]  page.get
2022-11-13 08:54:19 default[20221113t165059]  "GET /robots.txt HTTP/1.1" 404
2022-11-13 08:54:19 default[20221113t165059]  page.get_user_pages
2022-11-13 08:54:19 default[20221113t165059]  page.get_latest
2022-11-13 08:54:20 default[20221113t165059]  [2022-11-13 08:54:20 +0000] [11] [INFO] Starting gunicorn 20.1.0
2022-11-13 08:54:20 default[20221113t165059]  [2022-11-13 08:54:20 +0000] [11] [INFO] Listening at: http://0.0.0.0:8081 (11)
2022-11-13 08:54:20 default[20221113t165059]  [2022-11-13 08:54:20 +0000] [11] [INFO] Using worker: gthread
2022-11-13 08:54:20 default[20221113t165059]  [2022-11-13 08:54:20 +0000] [19] [INFO] Booting worker with pid: 19
2022-11-13 08:54:20 default[20221113t165059]  [2022-11-13 08:54:20 +0000] [22] [INFO] Booting worker with pid: 22
2022-11-13 08:54:21 default[20221113t165059]  I1113 08:54:21.078205    22 __init__.py:52] Initializing Cloud Debugger Python agent version: 3.1
2022-11-13 08:54:21 default[20221113t165059]  I1113 08:54:21.078326    19 __init__.py:52] Initializing Cloud Debugger Python agent version: 3.1
2022-11-13 08:54:21 default[20221113t165059]  "GET /load?c=0 HTTP/1.1" 200
2022-11-13 08:54:21 default[20221113t165059]  secureCheckLoggedIn
2022-11-13 08:54:21 default[20221113t165059]  SCLI Not Logged In

Gunicorn is restarted twice in the space of 2 seconds. Every time the thread restarts, it invalidates the previous stored session variables.

How do I fix this please? Just in case, here is my app.yaml

runtime: python38

env_variables:
    PASSWORD: "XXXXXXXXX"
    SENDGRID_API_KEY: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

app_engine_apis: true

handlers:
    - url: /static
      static_dir: static

    - url: /.*
      script: auto

P.S. secureCheckLoggedIn, Page…. are all my debug printing.

Asked By: Daniel Wong

||

Answers:

Many apologies. I found that the reason why my session (run off MongoDB) was so unstable. The reason is because for the

secretKey = os.urandom(21)  # your own secret key

So every time gunicorn reinitialized itself (I don’t know the reason why though), all the code infront of that which required to have the same secret_key was thrashed on gcloud. No such problems for my local dev machine though. I changed secretKey to a fixed string and that solved it on gcloud.

Answered By: Daniel Wong