diff --git a/cf/src/index.js b/cf/src/index.js index ffbe7aef..abd793d0 100644 --- a/cf/src/index.js +++ b/cf/src/index.js @@ -247,6 +247,8 @@ function Postgres(a, b) { ]) } catch (error) { throw error + } finally { + connection && (connection.onclose = null) } async function scope(c, fn, name) { diff --git a/cjs/src/index.js b/cjs/src/index.js index f09c61c7..d6e00ece 100644 --- a/cjs/src/index.js +++ b/cjs/src/index.js @@ -246,6 +246,8 @@ function Postgres(a, b) { ]) } catch (error) { throw error + } finally { + connection && (connection.onclose = null) } async function scope(c, fn, name) { diff --git a/cjs/tests/index.js b/cjs/tests/index.js index 85d1aa46..a41954c3 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -2420,6 +2420,21 @@ t('Ensure transactions throw if connection is closed dwhile there is no query', return ['CONNECTION_CLOSED', x.code] }) +t('Does not reject leftover begin onclose after commit', async() => { + const sql = postgres({ ...options, max: 1 }) + const rejections = [] + const onUnhandled = reason => rejections.push(reason) + process.on('unhandledRejection', onUnhandled) + try { + await sql.begin(sql => sql`select 1`) + await sql.end({ timeout: 0 }) + await delay(50) + } finally { + process.off('unhandledRejection', onUnhandled) + } + return [0, rejections.length] +}) + t('Custom socket', {}, async() => { let result const sql = postgres({ diff --git a/deno/src/index.js b/deno/src/index.js index b6d23db1..981c4056 100644 --- a/deno/src/index.js +++ b/deno/src/index.js @@ -247,6 +247,8 @@ function Postgres(a, b) { ]) } catch (error) { throw error + } finally { + connection && (connection.onclose = null) } async function scope(c, fn, name) { diff --git a/deno/tests/index.js b/deno/tests/index.js index cc2a2518..a2334361 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -2422,6 +2422,21 @@ t('Ensure transactions throw if connection is closed dwhile there is no query', return ['CONNECTION_CLOSED', x.code] }) +t('Does not reject leftover begin onclose after commit', async() => { + const sql = postgres({ ...options, max: 1 }) + const rejections = [] + const onUnhandled = reason => rejections.push(reason) + process.on('unhandledRejection', onUnhandled) + try { + await sql.begin(sql => sql`select 1`) + await sql.end({ timeout: 0 }) + await delay(50) + } finally { + process.off('unhandledRejection', onUnhandled) + } + return [0, rejections.length] +}) + t('Custom socket', {}, async() => { let result const sql = postgres({ diff --git a/src/index.js b/src/index.js index c7fba3da..d50e1d1a 100644 --- a/src/index.js +++ b/src/index.js @@ -246,6 +246,8 @@ function Postgres(a, b) { ]) } catch (error) { throw error + } finally { + connection && (connection.onclose = null) } async function scope(c, fn, name) { diff --git a/tests/index.js b/tests/index.js index 23e6c4d4..bc7be014 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2420,6 +2420,21 @@ t('Ensure transactions throw if connection is closed dwhile there is no query', return ['CONNECTION_CLOSED', x.code] }) +t('Does not reject leftover begin onclose after commit', async() => { + const sql = postgres({ ...options, max: 1 }) + const rejections = [] + const onUnhandled = reason => rejections.push(reason) + process.on('unhandledRejection', onUnhandled) + try { + await sql.begin(sql => sql`select 1`) + await sql.end({ timeout: 0 }) + await delay(50) + } finally { + process.off('unhandledRejection', onUnhandled) + } + return [0, rejections.length] +}) + t('Custom socket', {}, async() => { let result const sql = postgres({