Flask server is live but the site throws 502 Bad Gateway (sometimes loads infinitely))

Hello, I have written a flask server and this code brings up the server:

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 10000))
    logging.info(f"Starting server on port {port}")
    app.run(debug=True, host="0.0.0.0", port=port)

The Docker container works well on my localhost. The same Docker image is on the Docker Hub. When I deployed on Render, the logs showed this:

==> Starting service...
==> No open ports detected, continuing to scan...
==> Docs on specifying a port: https://render.com/docs/web-services#port-binding
Creating new Ultralytics Settings v0.0.6 file โœ… 
View Ultralytics Settings with 'yolo settings' or at '/root/.config/Ultralytics/settings.json'
Update Settings with 'yolo settings key=value', i.e. 'yolo settings runs_dir=path/to/dir'. For help see https://docs.ultralytics.com/quickstart/#ultralytics-settings.
INFO:root:Starting server on port 10000
 * Serving Flask app 'index'
 * Debug mode: on
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:10000
 * Running on http://10.220.212.144:10000
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug: * Restarting with stat
==> Your service is live ๐ŸŽ‰
==> No open HTTP ports detected on 0.0.0.0, continuing to scan...

The deployment page also says itโ€™s live.

But when I open the site, it sometimes says Bad Gateway or loads for infinity.
I have also opened a ticket (ID #15790).

Could anyone help me?

1 Like

Hi, we are having similar issue since last sunday.

Sometimes any request to web service (including health check endpoint) ends up with 502 status code, which triggers service restart.

When health check endpoint fails to respond within 5s, the service is evaluated as Server Unhealthy (see Health check protocol - Render docs) and render sends SIGRTERM signal and restarts it.

During this time it responds with 502 BAD Gateway code.

The challenge is to find out whatโ€™s causing responses to load so long.

I suspect it might be related to number of requests per (over 40/ per minute) - check in your dashboard Metrics page

You can find possible solutions here: Troubleshooting Your Deploy โ€“ Render Docs