Skip to content

Cannot read property 'destroy' of undefined #9242

Open

Description

@Ponjimon

What are you doing?

Just regular selects (findAll, findById, etc..). It doesn't really matter in this case.
For some reason, I get the above error exactly at this line: https://github.com/sequelize/sequelize/blob/master/lib/dialects/abstract/connection-manager.js#L166

As I see, there is no check whatsoever if this key in this.pool actually exists and apparently, there is a case where it gets removed or not created in the first place.
The problem is: I don't know how to reproduce this or why it happens, it happens very rarely, but it happens.

What do you expect to happen?

That the connection in the pool is still there when it tries to destroy it.

What is actually happening?

The connection seems to be removed from the pools array before the destroy call, so the call fails.

Dialect: mysql
Dialect version:
Database version: AWS Aurora MySQL 5.7
Sequelize version: ^4.33.2
Tested with latest release: 4.37.4

Activity

  1. sushantdhiman commented on Mar 27, 2018

    @sushantdhiman
    Contributor

    Can you add console.log around here

    console.log(mayBeConnection.queryType);
    
    return this.pool[mayBeConnection.queryType].destroy(mayBeConnection)
      .tap(() => { debug('connection destroy'); });

    And eventually when this fails we will know what key is passed, it should be set read or write at time of acquiring connection from pool.

  2. added
    type: bug
    on Mar 27, 2018
  3. sushantdhiman commented on May 25, 2018

    @sushantdhiman
    Contributor

    Closing as there seems to be no reliable way to reproduce this

  4. hoangleitvn commented on Jan 18, 2019

    @hoangleitvn

    We are also facing this error. Here is the stack trace detail

    TypeError: Cannot read property 'destroy' of undefined
    at Object.destroy (/var/task/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:166:53)
    at Connection.errorHandler (/var/task/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:103:23)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Connection._notifyError (/var/task/node_modules/mysql2/lib/connection.js:219:18)
    at Socket. (/var/task/node_modules/mysql2/lib/connection.js:95:18)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at TCP._handle.close [as _onclose] (net.js:567:12)

    We use Sequelize connection pool on Lambda function. This is our options object
    {
    host: process.env.DB_HOST,
    port: parseInt(process.env.DB_PORT) || 3306,
    database: process.env.DB_DATABASE_NAME,
    dialect: 'mysql',
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    timezone: '-00:00',
    pool: {
    max: 5,
    min: 0,
    idle: 15000,
    acquire: 60000,
    evict: 15000
    },
    replication: {
    read: [{
    host: process.env.DB_HOST,
    username: process.env.DB_READ_USERNAME,
    password: process.env.DB_READ_PASSWORD
    }],
    write: {
    host: process.env.DB_HOST,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD
    }
    },
    operatorsAliases: false
    }

  5. BjornTM commented on Aug 13, 2019

    @BjornTM

    @sushantdhiman
    I've encountered this issue, what debugging can I contribute to help find the bug?

  6. papb commented on Aug 13, 2019

    @papb
    Member

    @BjornTM and others - The most effective way would be finding a SSCCE/MCVE/reprex)...

    If this is too hard, please try to find a pattern on when it happens. What is different when it happens and when it doesn't...

  7. reopened this on Aug 13, 2019
  8. ftuckey commented on Apr 20, 2020

    @ftuckey

    Im having the same issue

    Dialect: postgres
    Database version: AWS Aurora Postgres 9.6.8
    Sequelize version: ^5.8.1

    Screen Shot 2020-04-20 at 4 22 10 PM

  9. marcogrcr commented on Aug 26, 2020

    @marcogrcr
    Contributor

    For people experiencing issues in AWS Lambda, I have created a pull request that documents how to properly configure sequelize for Lambda. Hopefully you'll be able to see it in https://sequelize.org/master/manual/aws-lambda.html once it gets merged. In the meantime, please check #12642 out and provide feedback if you find any inaccuracies or space for improvement.

  10. added theissue type on Mar 9, 2025
  11. removed
    type: bug
    on Mar 9, 2025
  12. Americas commented on Nov 10, 2025

    @Americas
    Contributor

    Hey, I was debugging an issue where a process is being killed due to this uncaught error, and after some sleuthing I figured out a reason why this happens.

    This happens when a new connection is requested by the pool, that connection is created, but there is some setup before it is returned and added to the pool. If an error occurs during that setup, there is a call to destroy the connection, but that connection is not yet in the pool. Since the property connection.queryType is not yet set, the Cannot read property 'destroy' of undefined error is thrown and uncaught, so it crashes the process. Before v7, this only happens when replication is used, but in v7 it happens either way.

    There are multiple ways I see to fix this, looking at the main branch, we can:

    1. The simplest way, we get rid of this error, and if the connectionType is undefined, just ignore the destroy since the connection is not in any pool.
    2. A little more involved, since it requires a change in each DB package that has this issue. But basically it requires protecting this call from throwing the error from point 1, and just ignore it.
    3. Maybe a little overkill, but we could separate the create connection in two (a create and a setup). Then we can call the create, set the connection on the owningPools set, and then call setup. If something happens during the setup, the connection exists on the set so the check passes. This then calls destroy on the pool for that connection, which will do nothing since the connection is not there yet anyways.

    I can submit a PR to fix this issue, just let me know what solution is most acceptable.

  13. WikiRik commented on Nov 10, 2025

    @WikiRik
    Member

    @Americas thanks for looking into this! Nice that you provided various options for resolving at least one reason for this. I think I like option 3 most, but I will look into it more later

  14. diogotorres97 commented on Nov 19, 2025

    @diogotorres97

    @WikiRik @Americas any update on this one?

  15. WikiRik commented on Nov 20, 2025

    @WikiRik
    Member

    I'm still in favour of option 3, but I would also accept a PR that 'fixes' it using option 1 for now

  16. diogotorres97 commented on Sep 24, 2026

    @diogotorres97

    Thanks @Americas and @WikiRik for getting #18218 merged! 🙌

    Since it targets main (v7), is there any plan to backport this fix to v6 and cut a v6 release, or will it only ship with v7? The issue also affects v6 when replication is used, so a v6 patch would be really helpful for those of us who can't move to v7 yet.

  17. WikiRik commented on Sep 25, 2026

    @WikiRik
    Member

    I think we will only ship it to v7, but I can look into the impact of backporting to v6.

  18. diogotorres97 commented on Sep 29, 2026

    @diogotorres97

    I think we will only ship it to v7, but I can look into the impact of backporting to v6.

    Thank you <3

  19. diogotorres97 commented on Oct 19, 2026

    @diogotorres97

    @WikiRik any conclusion on this one?

  20. WikiRik commented on Oct 19, 2026

    @WikiRik
    Member

    @diogotorres97 thanks for asking. we've decided not to backport it to v6

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

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions