Railway crashes when trying to access API endpoint Flask (Application failed to respond)

jaxsss
TRIAL

a year ago

Hey,

I have Flask app which connects to PostgreSQL that I also have on railway. Problem is whatever I try (either use SQLAlchemy or psycopg2 to connect to the database, or use the default Flask connector -> app.config['SQLALCHEMYDATABASEURI'] = … and db = SQLAlchemy(app)) it doesnt work, I wrapped the connection part of the code in try except block with exception to see what fails, but instead the whole Railway backend crashes and says "Application failed to respond".

I believe I have everything set up correctly. The backend works, on default "/" endpoint I see the message I specified in that endpoint, when I try to open the endpoint with DB connection it crashes.

I also have set up my db model - "class Table(db.Model)", note that at first when developing locally I had SQLite table based on that model, so I'm not so sure how does it work when connecting to the Postgres remotely. Also when I first deployed to Railway, I still had the SQLite .db file in my repository, so it took data I presume from that file and it also worked.

All of the options (SQLAlchemy, psycopg, default Flask connector) I mentioned work locally no problem.

Do you guys have any idea what this could be caused by?

Solved

5 Replies

a year ago

I would just like to clarify that this wouldn't be the whole Railway backend crashing, this is only your app that's crashing.

Are you seeing any errors when your app crashes?

Do you have SQLALCHEMY_DATABASE_URI Service variable added to the service?

This also might be an out of memory issue, can you attach a screenshot of service metrics?


jaxsss
TRIAL

a year ago

I don't see any errors.

I do have the database URI specified directly in my Flask app.

app.config['SQLALCHEMYDATABASEURI'] = f'postgresql://{dbuser}:{dbpassword}@{dbhost}/{dbdatabase}'
db = SQLAlchemy(app)

And yeah, I noticed that when I tried to access that API endpoint, the service usage (Memory) came down to 0, but just a few times, now it doesn't happen. I don't have a screenshot since it was yesterday.


a year ago

Unfortunately without any errors I can't be super helpful, about all I can say is make sure you catch all errors and have all the database related stuff setup correctly.


jaxsss
TRIAL

a year ago

Thanks for the help anyway.

I resolved it by adding parameters to Gunicorn app - I added "--threads 3" and "--timeout 120", thanks to these parameters, I was able to see the log on my API endpoint (returned in json response), the Railway always threw the message "Application failed to respond" but with these settings it gave me the error code I needed.

It was caused by incorrectly specified port on the database - on my local machine I had .env file, and there I specified DBHOST variable -> it had port number at the end of the Postgres database url "postgresurl.com:5432", however in my Railway variables, I didnt include the port number. That's why it was failing. I always love how small mistake can lead to hours upon hours of investigation what happened :D


Status changed to Solved Railway over 1 year ago


a year ago

Happy you got it fixed!


Railway crashes when trying to access API endpoint Flask (Application failed to respond) - Railway Help Station