Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database connection timeouts after some time and strapi crashed #8436

Closed
sarsonj opened this issue Oct 22, 2020 · 8 comments · Fixed by #9155
Closed

Database connection timeouts after some time and strapi crashed #8436

sarsonj opened this issue Oct 22, 2020 · 8 comments · Fixed by #9155
Labels
issue: bug Issue reporting a bug severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve source: core:database Source is core/database package source: docs Documentation changes status: confirmed Confirmed by a Strapi Team member or multiple community members

Comments

@sarsonj
Copy link

sarsonj commented Oct 22, 2020

Bug report

Describe the bug

I am using Strapi as dockerised app inside Caprover PAAS. What happens is that when some time of inactivity, the app crashed in the next request. It looks like that connection to database is terminated.

This happens with both mysql and posgresql backend.

The strapi crashes then.

Steps to reproduce the behavior

  1. install strapi as dockerized app into Caprover PAAS
  2. use one-click mysql or pgsql app

Logs:

2020-10-22T11:20:22.986874066Z [2020-10-22T11:20:22.986Z] debug GET /blog-articles?_start=0&_limit=5&_sort=Date:DESC&IsPublished=true (23 ms) 200
2020-10-22T11:20:23.181097954Z [2020-10-22T11:20:23.180Z] debug GET /blog-articles/count?IsPublished=true (15 ms) 200
2020-10-22T11:46:05.945389043Z [2020-10-22T11:46:05.945Z] error Error: read ECONNRESET
2020-10-22T11:46:05.945432223Z     at TCP.onStreamRead (internal/stream_base_commons.js:208:20)
2020-10-22T11:46:05.945438229Z     --------------------
2020-10-22T11:46:05.945442289Z     at Protocol._enqueue (/src/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2020-10-22T11:46:05.945446377Z     at Connection.query (/src/app/node_modules/mysql/lib/Connection.js:208:25)
2020-10-22T11:46:05.945450155Z     at /src/app/node_modules/knex/lib/dialects/mysql/index.js:135:18
2020-10-22T11:46:05.945453786Z     at new Promise (<anonymous>)
2020-10-22T11:46:05.945458024Z     at Client_MySQL._query (/src/app/node_modules/knex/lib/dialects/mysql/index.js:129:12)
2020-10-22T11:46:05.945461647Z     at Client_MySQL.query (/src/app/node_modules/knex/lib/client.js:169:17)
2020-10-22T11:46:05.945465213Z     at Runner.query (/src/app/node_modules/knex/lib/runner.js:151:36)
2020-10-22T11:46:05.945468775Z     at /src/app/node_modules/knex/lib/runner.js:40:23
2020-10-22T11:46:05.945472593Z     at /src/app/node_modules/knex/lib/runner.js:277:24
2020-10-22T11:46:05.946054687Z [2020-10-22T11:46:05.945Z] debug GET /blog-articles?_start=0&_limit=5&_sort=Date:DESC&IsPublished=true (7 ms) 500
2020-10-22T11:48:21.500096306Z [2020-10-22T11:48:21.499Z] error Error: read ECONNRESET
2020-10-22T11:48:21.500134178Z     at TCP.onStreamRead (internal/stream_base_commons.js:208:20)
2020-10-22T11:48:21.500139415Z     --------------------
2020-10-22T11:48:21.500143792Z     at Protocol._enqueue (/src/app/node_modules/mysql/lib/protocol/Protocol.js:145:48)
2020-10-22T11:48:21.500149650Z     at Connection.query (/src/app/node_modules/mysql/lib/Connection.js:208:25)
2020-10-22T11:48:21.500153608Z     at /src/app/node_modules/knex/lib/dialects/mysql/index.js:135:18
2020-10-22T11:48:21.500164357Z     at new Promise (<anonymous>)
2020-10-22T11:48:21.500168736Z     at Client_MySQL._query (/src/app/node_modules/knex/lib/dialects/mysql/index.js:129:12)
2020-10-22T11:48:21.500172376Z     at Client_MySQL.query (/src/app/node_modules/knex/lib/client.js:169:17)
2020-10-22T11:48:21.500176009Z     at Runner.query (/src/app/node_modules/knex/lib/runner.js:151:36)
2020-10-22T11:48:21.500179811Z     at /src/app/node_modules/knex/lib/runner.js:40:23
2020-10-22T11:48:21.500183483Z     at /src/app/node_modules/knex/lib/runner.js:277:24

System

  • Node.js version: 12 LTS
  • NPM version:
  • Strapi version: 3.2.4
  • Database: mysql or pgsql
  • Operating system: docker - using node:lts-alpine
@derrickmehaffy
Copy link
Member

error Error: read ECONNRESET

Based on this error it leads me to believe the issue lies above Strapi, either some kind of DB proxy/load balancer or an issue with the database itself. That error means Strapi is trying to reconnect to the database and is unable to (database is killing the connection).

I'm marking this as closed as without steps to reconstruct the environment to look at directly this will be almost impossible to debug. Please do feel free to respond, though I recommend in cases like this we move the discussion over to the forum as it's highly unlikely this is an issue with the code base (thus a bug) and is more likely environmental or configuration issue.

@sarsonj
Copy link
Author

sarsonj commented Oct 23, 2020

I found solution - the problem is knex related that happens together with docker swarn. Because it is not unusual deployment, I am writing solution here.

The problem is, that docker swarn closes inactive connections in about 15 minutes. However, default knex configuration is to have connection pool with min: 2 connections always available. And those 2 connections broke.

The problem is described there: https://success.mirantis.com/article/ipvs-connection-timeout-issue

The solution is to set knex to have min: 0 always on connections in connection pool. I suggest to set default options into strapi to have this value. My config now works:

module.exports = {
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      {
        client: 'mysql',
        host: 'xxxx',
        port: xxx,
      };
      options: {
        "debug": true,
        "pool": {
          "min": 0,
          "max": 10,
          "idleTimeoutMillis": 30000,
          "createTimeoutMillis": 30000,
          "acquireTimeoutMillis": 30000
        }
      }
    }
  }
};

@derrickmehaffy
Copy link
Member

Ah yes you are correct @Satrya2405 for some reason I though Knex's default was 0, but it is in fact 2: http://knexjs.org/#Installation-pooling

I'll add this to our documentation backlog, though I don't think it's good to provide these by default in the code-base as it really only applies to containers that kill connections.

@derrickmehaffy
Copy link
Member

Reopening to get @alexandrebodin's thoughts on this, as our existing documentation and structure doesn't really give any suggestions on modifying the database.js within the container.

@alexandrebodin is this better in the docs or should we just apply the min: 0 globally to Bookshelf DBs?

@derrickmehaffy derrickmehaffy added severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve source: core:database Source is core/database package source: docs Documentation changes status: confirmed Confirmed by a Strapi Team member or multiple community members issue: bug Issue reporting a bug labels Oct 26, 2020
@derrickmehaffy derrickmehaffy added this to To Review in [Experiment] Issue Board via automation Oct 26, 2020
@bykof
Copy link
Contributor

bykof commented Nov 12, 2020

We had also postgres connection problems, which caused Connection terminated unexpectedly.
Setting min: 0 has fixed it.

@NgyAnthony
Copy link
Contributor

Bumping this for visibility. I initially thought it was a network issue since all my services (including DB) are behind Traefik/on Docker. If you could add that min:0 is maybe needed in the docs that would be great.

@alexandrebodin
Copy link
Member

we coudl certainly add min:0 as default. if any one wants to make a PR at some point feel free to do it & reference this issue :)

NgyAnthony added a commit to NgyAnthony/strapi that referenced this issue Jan 18, 2021
[Experiment] Issue Board automation moved this from To Review to Fixed Jan 18, 2021
alexandrebodin pushed a commit that referenced this issue Jan 18, 2021
* Add min:0 to templates of Bookshelf DBs. Issue #8436

* Moving default property from db template to knex.js

* Move min to pool
@derrickmehaffy
Copy link
Member

This issue has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/strapi-service-that-stops-when-it-loses-its-database-connection/11782/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: low If the issue only affects a very niche base of users and an easily implemented workaround can solve source: core:database Source is core/database package source: docs Documentation changes status: confirmed Confirmed by a Strapi Team member or multiple community members
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

5 participants