diff --git a/src/factories/createPool.ts b/src/factories/createPool.ts index 61cceec1..31188be0 100644 --- a/src/factories/createPool.ts +++ b/src/factories/createPool.ts @@ -72,6 +72,16 @@ export const createPool = async ( }, }); + // https://github.com/gajus/slonik/issues/471 + pool.on('error', (error) => { + poolLog.error( + { + error: serializeError(error), + }, + 'client error', + ); + }); + poolStateMap.set(pool, { ended: false, mock: false, diff --git a/test/helpers/createIntegrationTests.ts b/test/helpers/createIntegrationTests.ts index 5c5c6dc4..9504eb1b 100644 --- a/test/helpers/createIntegrationTests.ts +++ b/test/helpers/createIntegrationTests.ts @@ -140,6 +140,25 @@ export const createIntegrationTests = ( test: TestFn, PgPool: new () => PgPoolType, ) => { + test('properly handles terminated connections', async (t) => { + const pool = await createPool(t.context.dsn, { + PgPool, + }); + + await Promise.all([ + pool.connect(() => Promise.resolve()), + pool.connect(() => Promise.resolve()), + ]); + + await t.notThrowsAsync( + pool.query(sql.unsafe` + SELECT pg_terminate_backend(pid) + FROM pg_stat_activity + WHERE pid != pg_backend_pid() + `), + ); + }); + test('produces syntax error with the original SQL', async (t) => { const pool = await createPool(t.context.dsn, { PgPool,