Skip to content

Commit 315407e

Browse files
committed
Fix race condition with transactions on end
1 parent 6e615ae commit 315407e

File tree

9 files changed

+54
-24
lines changed

9 files changed

+54
-24
lines changed

cjs/src/connection.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,11 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
535535
return // Consider opening if able and sent.length < 50
536536

537537
connection.reserved
538-
? x[5] === 73
539-
? ending && terminate()
540-
: connection.reserved() // I
538+
? x[5] === 73 // I
539+
? ending
540+
? terminate()
541+
: (connection.reserved = null, onopen(connection))
542+
: connection.reserved()
541543
: ending
542544
? terminate()
543545
: onopen(connection)

cjs/src/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ function Postgres(a, b) {
193193
return await scope(connection, fn)
194194
} catch (error) {
195195
throw error
196-
} finally {
197-
if (connection) {
198-
connection.reserved = null
199-
onopen(connection)
200-
}
201196
}
202197

203198
async function scope(c, fn, name) {

cjs/tests/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,3 +1921,16 @@ t('Prevent premature end of connection in transaction', async() => {
19211921
result
19221922
]
19231923
})
1924+
1925+
t('Ensure reconnect after max_lifetime with transactions', { timeout: 5000 }, async() => {
1926+
const sql = postgres({
1927+
max_lifetime: 0.01,
1928+
idle_timeout,
1929+
max: 1
1930+
})
1931+
1932+
let x = 0
1933+
while (x++ < 10) await sql.begin(sql => sql`select 1 as x`)
1934+
1935+
return [true, true]
1936+
})

deno/src/connection.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,11 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
538538
return // Consider opening if able and sent.length < 50
539539

540540
connection.reserved
541-
? x[5] === 73
542-
? ending && terminate()
543-
: connection.reserved() // I
541+
? x[5] === 73 // I
542+
? ending
543+
? terminate()
544+
: (connection.reserved = null, onopen(connection))
545+
: connection.reserved()
544546
: ending
545547
? terminate()
546548
: onopen(connection)

deno/src/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,6 @@ function Postgres(a, b) {
194194
return await scope(connection, fn)
195195
} catch (error) {
196196
throw error
197-
} finally {
198-
if (connection) {
199-
connection.reserved = null
200-
onopen(connection)
201-
}
202197
}
203198

204199
async function scope(c, fn, name) {

deno/tests/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,3 +1922,16 @@ t('Prevent premature end of connection in transaction', async() => {
19221922
result
19231923
]
19241924
})
1925+
1926+
t('Ensure reconnect after max_lifetime with transactions', { timeout: 5000 }, async() => {
1927+
const sql = postgres({
1928+
max_lifetime: 0.01,
1929+
idle_timeout,
1930+
max: 1
1931+
})
1932+
1933+
let x = 0
1934+
while (x++ < 10) await sql.begin(sql => sql`select 1 as x`)
1935+
1936+
return [true, true]
1937+
})

src/connection.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,11 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
535535
return // Consider opening if able and sent.length < 50
536536

537537
connection.reserved
538-
? x[5] === 73
539-
? ending && terminate()
540-
: connection.reserved() // I
538+
? x[5] === 73 // I
539+
? ending
540+
? terminate()
541+
: (connection.reserved = null, onopen(connection))
542+
: connection.reserved()
541543
: ending
542544
? terminate()
543545
: onopen(connection)

src/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ function Postgres(a, b) {
193193
return await scope(connection, fn)
194194
} catch (error) {
195195
throw error
196-
} finally {
197-
if (connection) {
198-
connection.reserved = null
199-
onopen(connection)
200-
}
201196
}
202197

203198
async function scope(c, fn, name) {

tests/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,3 +1921,16 @@ t('Prevent premature end of connection in transaction', async() => {
19211921
result
19221922
]
19231923
})
1924+
1925+
t('Ensure reconnect after max_lifetime with transactions', { timeout: 5000 }, async() => {
1926+
const sql = postgres({
1927+
max_lifetime: 0.01,
1928+
idle_timeout,
1929+
max: 1
1930+
})
1931+
1932+
let x = 0
1933+
while (x++ < 10) await sql.begin(sql => sql`select 1 as x`)
1934+
1935+
return [true, true]
1936+
})

0 commit comments

Comments
 (0)