Hosting on Railway - 401 error after inactivity

System Information
  • Strapi Version: 4.3.3
  • Database: PostgreSQL
  • Node Version: 16.17.0
  • NPM Version: 8.11.0

I have Strapi deployed on Railway with the developer plan and after periods of inactivity, I get 401 errors when trying to hit my API endpoint or my admin panel. I’m testing with Railway to make sure it compares to my paid Heroku dynos/db. Does anyone have experience with this same issue? Railway’s developer pricing states: “Unlimited execution time. Deploys remain online indefinitely”.

I’ve asked for help on Strapi and Railway’s discords. The only response I’ve received from someone at Railway was that this is a Strapi error without any other help or suggestions:

Angelo — 08/30/2022
This is an error with Strapi to my experience.

Cooper — 08/28/2022
Seen this before with Strapi so would hazard this isn’t an issue with us

Some of the logs I’m seeing on Railway:

[2022-08-30 14:18:47.691] http: GET /api/locations (16 ms) 401
Connection Error: Connection ended unexpectedly
[2022-08-30 14:18:56.505] http: GET /api/locations (5 ms) 401
Connection Error: Connection ended unexpectedly
[2022-08-30 14:18:57.689] http: GET /api/locations (62 ms) 200
[2022-09-01 03:49:40.132] debug: ⛔️ Server wasn't able to start properly.
[2022-09-01 03:49:40.132] error: select "t0"."id" from "admin_users" as "t0" limit $1 - Connection terminated unexpectedly
Error: select "t0"."id" from "admin_users" as "t0" limit $1 - Connection terminated unexpectedly
at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:73)
at Object.onceWrapper (node:events:627:28)
at Connection.emit (node:events:513:28)
at Connection.emit (node:domain:489:12)
at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
at Socket.emit (node:events:513:28)
at Socket.emit (node:domain:489:12)
at TCP.<anonymous> (node:net:757:14)
1 Like

After doing a test deployment to render and talking more with the railway team, it does look to be an issue with Strapi. There are plenty of other people having this same issue with Railway and Render. Railway said they would make any changes needed if there’s an issue with their platform.

Cooper — Today at 3:55 PM
Appreciate it! We’re happy to make any changes to the platform required if we do indeed ascertain that it’s a platform issue!
However, I’ve seen this many times before with Strapi.

Anyone find a solution for this?

I’m having the same issue with render.com
Weird that if I just log in to render and refresh the my site, problem is gone for a while (minutes, hours), then again, error 502 Bad Gateway.

I tried with a paid plan too (someone somewhere suggest that could be because strapi needs more “power”, but a better plan doesn’t solve it.

Seems that problem was from Strapi. I just upgraded Strapi and problem is gone.

I am having the same issue with Railway.app

Noooooo, don’t tell me that, I’m planning to migrate my portfolio from Heroku to Railway!!!

Should we begin to think in migrate to another Headless CMS?

@xyzbarna The error is still present for me in Strapi 4.4.1 …

The first error log I got when I look on Railway is

Connection Error: Connection ended unexpectedly

I guess there is a problem with the connection to the DB at some point.

@LuisAlaguna I think it would be sad to leave Strapi because of this kind of issue, it will probably be fixed at some point. You could may be do some static site generation without refreshing the data when run on the client site for your portfolio. That would prevent 401 and error on client side.

I actually find a way to prevent the bug,
The issue was with the DB configuration, not completely sure why but I guess the connection pool was containing Idled connection and when making a request after a long time of inactivity, the request couldn’t succeed.

I fix it by updating my config/database.js file :

module.exports = ({env}) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('PGHOST', '127.0.0.1'),
      port: env.int('PGPORT', 5432),
      database: env('PGDATABASE', 'strapi'),
      user: env('PGUSER', 'strapi'),
      password: env('PGPASSWORD', 'password'),
      ssl: env.bool(true),
    },
    acquireConnectionTimeout: 5000,
    pool: {
      min: 0,
      max: 10,
      createTimeoutMillis: 8000,
      acquireTimeoutMillis: 8000,
      idleTimeoutMillis: 8000,
      reapIntervalMillis: 1000,
      createRetryIntervalMillis: 100,
    }
  },
});

The important part here is the “pool” object and particularly the min property that I’m setting to 0 instead of 2 by default.

1 Like

Okay, I’ll give it a try here soon. Have you had any issues since updating?

Unfortunately, after adding the pool object you included I still get the same error. I deployed Strapi from my repo, got everything up and running, all requests were good, let it sit overnight, and made a request this morning - connection error

Connection Error: Connection ended unexpectedly

Same here!!, Error 401. Does anybody findout a solution?

Come on man, don’t tell me this, I will deploy my portfolio’s backend on it.

What’s the other good alternative to Heroku?

In my case, config posted by clment305 didn’t work. I recommend to try configuration from this comment.

Thanks for your suggestion, I didn’t check it yet, but I will test it soon.