Skip to content

Commit

Permalink
fix: properly handle terminated connections (fixes #471)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 27, 2023
1 parent 5f05094 commit 8f066eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/factories/createPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions test/helpers/createIntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ export const createIntegrationTests = (
test: TestFn<TestContextType>,
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,
Expand Down

0 comments on commit 8f066eb

Please sign in to comment.