Persistent Error 502 When Deploying Flask app
kimagutgeorge
HOBBYOP

3 months ago

- Python 3.12 Flask app with Flask-SocketIO and eventlet

- Running on Railway with a custom Dockerfile

- PostgreSQL database via SQLAlchemy

- Background scheduler running alongside the app

Start command:

bashpython scheduler.py & gunicorn --worker-class eventlet -w 1 --timeout 120 -b 0.0.0.0:$PORT app:app

The problem:

All routes return a 502 "Application failed to respond" from Railway, even though the deployment shows as Online and route hits are visible in logs. No response is ever sent back.

What I've tried:

- Switching hardcoded port 5000 to $PORT / os.environ.get('PORT')

- Adding eventlet.monkey_patch() at the top of app.py

- Creating a separate wsgi.py entry point to ensure monkey patching runs first

- Running directly with python app.py instead of gunicorn

- Setting debug=False

1 RLock(s) were not greened, to fix this error make sure you run

eventlet.monkey_patch() before importing any other modules.

This persists even with wsgi.py as the entry point, suggesting one of the wildcard imports (from models import , from function import , etc.) is loading a threading module before the patch can apply.

The app has been inactive for about 2 months due to payment. However, after payment yesterday is when the error started occuring

Has anyone successfully run Flask-SocketIO + eventlet on Railway? Is there a recommended pattern for structuring the entry point and start command to avoid this RLock issue?

$10 Bounty

1 Replies

Status changed to Awaiting Railway Response Railway 3 months ago


You can either swap eventlet for gevent , or downgrade to Python 3.11. Because eventlet doesn't work well with Python 3.12.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...