From c415b16616871073fa0aa11a4965e2ba86db60a0 Mon Sep 17 00:00:00 2001 From: Mike Roelens Date: Thu, 1 Aug 2024 10:50:08 -0400 Subject: [PATCH] fix: close all connections in pool.end() (#633) * fix: close all connections in pool.end() * chore: add changeset --- .changeset/giant-chicken-help.md | 5 +++++ packages/slonik/src/factories/createConnectionPool.ts | 6 +++++- packages/slonik/src/helpers.test/createIntegrationTests.ts | 4 +--- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 .changeset/giant-chicken-help.md diff --git a/.changeset/giant-chicken-help.md b/.changeset/giant-chicken-help.md new file mode 100644 index 00000000..7c381cdb --- /dev/null +++ b/.changeset/giant-chicken-help.md @@ -0,0 +1,5 @@ +--- +"slonik": major +--- + +fix: close all connections in pool.end(). Previously a subset idleConnections would hang until idleTimeout was reached diff --git a/packages/slonik/src/factories/createConnectionPool.ts b/packages/slonik/src/factories/createConnectionPool.ts index d0fe4a10..3b4e00fc 100644 --- a/packages/slonik/src/factories/createConnectionPool.ts +++ b/packages/slonik/src/factories/createConnectionPool.ts @@ -108,7 +108,11 @@ export const createConnectionPool = ({ // e.g. "waits for all connections to be established before attempting to terminate the pool" test await delay(0); - await Promise.all(connections.map((connection) => connection.destroy())); + // Make a copy of `connections` array as items are removed from it during the map iteration. + // If `connections` array is used directly, the loop will skip some items. + await Promise.all( + [...connections].map((connection) => connection.destroy()), + ); }; const acquire = async () => { diff --git a/packages/slonik/src/helpers.test/createIntegrationTests.ts b/packages/slonik/src/helpers.test/createIntegrationTests.ts index 07a2181a..b9b7d0ec 100644 --- a/packages/slonik/src/helpers.test/createIntegrationTests.ts +++ b/packages/slonik/src/helpers.test/createIntegrationTests.ts @@ -926,7 +926,7 @@ export const createIntegrationTests = ( const pool = await createPool(t.context.dsn, { driverFactory, - idleTimeout: 500, + idleTimeout: 'DISABLE_TIMEOUT', maximumPoolSize: 5, }); @@ -968,8 +968,6 @@ export const createIntegrationTests = ( await pool.end(); - await delay(600); - t.deepEqual(pool.state(), { acquiredConnections: 0, idleConnections: 0,