Skip to content

TimeoutError: ResourceRequest timed out #7884

Closed

Description

@clutariomark

What you are doing?

Testing the connection to a postgres amazon rds instance

const sequelize = new Sequelize('dbname', 'username', 'password', {
  host: 'example.rds.amazonaws.com',
  dialect: 'postgres'
});


sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

What do you expect to happen?

App should connect and should output in console: "Connection has been established successfully"

What is actually happening?

Unable to connect to the database: { TimeoutError: ResourceRequest timed out
    at ResourceRequest._fireTimeout (\myapp\node_modules\generic-pool\lib\ResourceRequest.js:58:17)
    at Timeout.bound (\myapp\node_modules\generic-pool\lib\ResourceRequest.js:8:15)
    at ontimeout (timers.js:380:14)
    at tryOnTimeout (timers.js:244:5)
    at Timer.listOnTimeout (timers.js:214:5) name: 'TimeoutError' }

Dialect: postgres
Database version: 9.6
Sequelize version: 4.2.1

Activity

  1. felixfbecker commented on Jul 5, 2017

    @felixfbecker
    Contributor

    I cannot reproduce the error. Obviously there was a timeout in connecting to the DB server, it doesn't look like a bug in Sequelize to me.

  2. clutariomark commented on Jul 5, 2017

    @clutariomark
    Author

    Tried the same endpoint and other credentials using pg-promise, and I was successfully connected. Could you tell what db server you tried it on? Of course, the app connects to my local db.

  3. felixfbecker commented on Jul 5, 2017

    @felixfbecker
    Contributor

    Own applications running on AWS that use Sequelize.
    I am sorry, but even if you can connect with pg-promise, I have no way to find out why it wouldn't work with Sequelize without a repro.

  4. clutariomark commented on Jul 5, 2017

    @clutariomark
    Author

    please use this and try if you can reproduce the same error
    postgres://testuser:[email protected]:5432/safepath

    Already tried connecting to it using node-postgres and it worked, same error for sequelize though.

  5. afituri commented on Jul 6, 2017

    @afituri

    I have the same exact issue!

  6. afituri commented on Jul 6, 2017

    @afituri

    screenshot from 2017-07-06 02-14-13

  7. pavelkrcil commented on Jul 8, 2017

    @pavelkrcil

    Same issue here. The connection is established but when the connection of any connection is lost, the pool is getting smaller and after death of all threads application goes down. We had no troubles with this with 3+ version of Sequelize, so I guess it's not problem of db connection. Also tried downgrade from 4.2.1 (latest) to 4.1.0 and same problem.

  8. afituri commented on Jul 8, 2017

    @afituri

    I have fixed the issue by maximizing the acquire option of pool configuration in sequelize

    pool: {
        max: 5,
        min: 0,
        idle: 20000,
        acquire: 20000
    }

    I guess Amazon RDS runs a lot of checks when asking for resources.

    Also I am running the Free Tier of Amazon RDS.

  9. swordfish444 commented on Jul 10, 2017

    @swordfish444
    Contributor

    I'm having the exact same issue! The above pool configuration did NOT work. In our production environment, in a 24h period this error is logged ~5,000 times. It's been consistent for the last 27 days. I too am on the latest v4 of sequelize and using AWS RDS. It would be great to have this looked into more closely as it's a serious risk in the latest release.

    image

  10. swordfish444 commented on Jul 10, 2017

    @swordfish444
    Contributor

    @felixfbecker It looks like this is the same as #7882

    Also #7616 is likely related to the same problem.

  11. pavelkrcil commented on Jul 10, 2017

    @pavelkrcil

    @afituri This saved my life. Thank you!

  12. philipdbrown commented on Jul 10, 2017

    @philipdbrown

    Yeah, I'm having this same issue on a DigitalOcean server as well. I've tried the configuration from above with no luck.

  13. 118 remaining items

  14. polaroi8d commented on Jul 15, 2019

    @polaroi8d

    Still experiencing the bug on v4.44.1.

    It might be worth just migrating to v5 and to see if it's been resolved there.

    @Joshua-Turner are u successfully migrated to v5 and find everything perfect? Do you have any experience with the v5?

  15. lancedikson commented on Jul 15, 2019

    @lancedikson

    We have upgraded to v5 and seems like it helped for some reason. But it was such a shot in the dark.

  16. bbbeeeee commented on Aug 1, 2019

    @bbbeeeee
    Contributor

    We upgraded to v5 as well. With no further sequelize (i. e. pooling) configuration other than that, we still get SequelizeConnectionAcquireTimeoutError: Operation timeout. Anecdotally, this timeout-ing seems to occur less, but haven't measured/monitored closely in our deployment.

    Will update if we find a configuration or bugfix that eradicates our issues completely!

  17. bbbeeeee commented on Aug 2, 2019

    @bbbeeeee
    Contributor

    Update:

    Found this issue yesterday #10976

    Turns out that we had a case of a concurrent nested transaction stalling a parent transaction, which resulted in a connection to hang on the parent transaction. When this case was reached n (where n = our max pool size) times that filled our pool with hanging connections, we'd get bursts of time that ended up degrading our service. We have health checker that will kill degraded instances, so this didn't give us complete downtime; instantiating new instances put off the problem until we reached that all-connections-in-pool-hanging state again.

    We solved this by refactoring to split out the concurrent nested transaction from the parent (they weren't actually dependent on each other). Our initial code would have the nested transaction fail silently, leaving the parent waiting for it to finish. Also did a pass through the codebase to ensure that anywhere we were doing nested transaction calls, we were passing transactions properly.

    We were able to replicate the issue in staging doing something similar to the code in the issue above, so I'm fairly confident this was our issue; after shipping our fix below we aren't seeing the same results.

    Disclaimer: may or may not understand the full intricacies of connections/transactions/pooling, so take with a grain of salt!

  18. mickhansen commented on Aug 5, 2019

    @mickhansen
    Contributor

    There's always been some issues with having N concurrent transactions where N is greater than your pool.max, i forgot the specific case but you could end up with code waiting for a connection to do something to finish up the transactions but the transactions had all the connection etc.

  19. mickhansen commented on Aug 5, 2019

    @mickhansen
    Contributor

    For reference my company has been running Sequelize against RDS for 4+ years with no issues, if there were ever connection issues it's cause there were actually connection issues.

  20. derakhshanfar commented on Aug 21, 2019

    @derakhshanfar

    as @BTROO and @mickhansen said, This issue happened when the count of concurrent transactions is greater than your pool.max. I'm trying to describe it in a simple way, Let's see what happens:
    What is the connection pool?

    When your application needs to retrieve data from the database, it creates a database connection. Creating this connection involves some overhead of time and machine resources for both your application and the database. Many database libraries and ORM's will try to reuse connections when possible so that they do not incur the overhead of establishing that DB connection over and over again. The pool is the collection of these saved, reusable connections that

    what is concurrent transactions?
    in a simple way, if you are trying to run some CRUD command in a nested transaction, it means you are using concurrent transactions. let's assume our Sequlize config is like this:

      pool: {
        max: 7,
        min: 0,
        acquire: 30000,
        idle: 10000,
      },
    

    we are trying to run this command:

          let nested = function() {
            // here we are making a new transaction:
            return db.sequelize.transaction(t1 => {
              // as you see in bellow command we won't use t1 transaction
              // which means our DB (in my case postgress) run below command in other transaction
              // and this is where the problem lies!
              // when we'll execute nested function, we'll create a concurrent transaction per each call
              return db.user.findOne();
            });
          };
          const arr = [];
          for (let i = 0; i < 7; i++) {
            arr.push(nested());
          }
          Promise.all(arr)
            .then(() => {
              console.log('done');
            })
            .catch(err => {
              console.log(err);
            });
    

    The workaround is just run db.user.findOne() in t1 transaction to avoid concurrent transactions and it will be fixed:
    db.user.findOne({transaction: t1})
    you can also use continuation-local-storage to pass transaction automatically, just check below link:
    https://sequelize.org/master/manual/transactions.html#concurrent-partial-transactions

  21. ashutoshpw commented on Sep 9, 2019

    @ashutoshpw

    In pool settings just keep the minimum count to about 2 to 4.
    This solved the issue for me.

      pool: {
        max: 7,
        min: 2,
        acquire: 30000,
        idle: 10000,
      },
    
  22. hrabizadeh commented on Dec 29, 2019

    @hrabizadeh

    We've bumped into the same error. It's weird, but we don't have any transactions in that piece of code. It looks like a bunch of asynchronous queries to the DB, two of them use sequelize.query() to query data and the rest 8 are made with models. I don't know why exactly, but calling one of the .query() after the others resolved somehow fixed the issue. So, we first run all those 9 queries simultaneously, and after they resolved we run the latest one. Only this worked for us.

    Thanks, Your solution solved my issue.

  23. intellix commented on May 29, 2020

    @intellix

    I don't understand how solving acquire timeout errors is fixed by reducing the timeout from the default of 60 seconds to 20?

    #7884 (comment)

    edit: because the default is actually 10 sec... not sure where I saw a default of 60

  24. val-pidburtnyi commented on Sep 1, 2020

    @val-pidburtnyi

    I don't understand how solving acquire timeout errors is fixed by reducing the timeout from the default of 60 seconds to 20?

    #7884 (comment)

    edit: because the default is actually 10 sec... not sure where I saw a default of 60

    I guess the key here is that 2 connections are never released ("min: 2")...

  25. johnfazzietempus commented on Oct 21, 2020

    @johnfazzietempus

    Looks like the acquire timeout is 60 seconds in sequelize v5 and v6.

  26. added 2 commits that reference this issue on Apr 11, 2022
    f086f00
    b8eae79
  27. yusufbayrk commented on Jun 3, 2022

    @yusufbayrk

    I was also getting the "Unhandled rejection SequelizeConnectionAcquireTimeoutError: Operation timeout error." I tried everything and the only one solution I got remove setınterval commands:

    setInterval(
    ( ) => sequelize.query("REFRESH MATERIALIZED VIEW tables"),
    2601e3,

    I remove this commands and problem is solved. I hope your solution will be solved very soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dependencytype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions