You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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)
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:
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.
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.
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.
@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
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.
Activity
sushantdhiman commentedon Mar 27, 2018
Can you add
console.logaround hereAnd eventually when this fails we will know what key is passed, it should be set
readorwriteat time of acquiring connection from pool.sushantdhiman commentedon May 25, 2018
Closing as there seems to be no reliable way to reproduce this
hoangleitvn commentedon Jan 18, 2019
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
}
BjornTM commentedon Aug 13, 2019
@sushantdhiman
I've encountered this issue, what debugging can I contribute to help find the bug?
papb commentedon Aug 13, 2019
@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...
ftuckey commentedon Apr 20, 2020
Im having the same issue
Dialect: postgres
Database version: AWS Aurora Postgres 9.6.8
Sequelize version: ^5.8.1
marcogrcr commentedon Aug 26, 2020
For people experiencing issues in AWS Lambda, I have created a pull request that documents how to properly configure
sequelizefor 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.Americas commentedon Nov 10, 2025
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.queryTypeis not yet set, theCannot read property 'destroy' of undefinederror 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:
connectionTypeis undefined, just ignore the destroy since the connection is not in any pool.owningPoolsset, 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.
WikiRik commentedon Nov 10, 2025
@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
diogotorres97 commentedon Nov 19, 2025
@WikiRik @Americas any update on this one?
WikiRik commentedon Nov 20, 2025
I'm still in favour of option 3, but I would also accept a PR that 'fixes' it using option 1 for now
diogotorres97 commentedon Sep 24, 2026
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.WikiRik commentedon Sep 25, 2026
I think we will only ship it to v7, but I can look into the impact of backporting to v6.
diogotorres97 commentedon Sep 29, 2026
Thank you <3
diogotorres97 commentedon Oct 19, 2026
@WikiRik any conclusion on this one?
WikiRik commentedon Oct 19, 2026
@diogotorres97 thanks for asking. we've decided not to backport it to v6