Skip to content

Commit bc23693

Browse files
fix(subscriptions): do not release the pooled connection a second time on an abort
knex releases a streamed query's connection twice over: from `ensureConnection`'s finally when the pg query stream ends, and from its own stream's 'close'. The wrapper added for #1243 defers destroying that stream until the cursor can close, so a query that ends inside that window — a small result, which is the common case for a REQ — has already released its connection when the deferred destroy fires 'close' and releases it again. tarn matches a release by resource identity, so the second release frees whatever lease holds that connection by then: the pool can hand one pg client to two requests. Measured on the wrapper's own fake of knex 3.2.9's semantics: one release before the guard, two after a source 'end' inside the poll window, one with the guard. The new case fails on the previous commit (released 2, expected 1) and passes here. The destroy now runs only for a query that is still open and whose source has not ended, so the connection is released by the path that owns it.
1 parent f42a762 commit bc23693

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎src/utils/query-stream.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,33 @@ const whenCursorCanClose = (source: QuerySource | undefined, fn: () => void): vo
5555
export const abortableQueryStream = (query: Readable, signal: AbortSignal): Readable => {
5656
let attached = false
5757
let source: QuerySource | undefined
58+
let sourceEnded = false
5859
query.once('pipe', (piped: QuerySource) => {
5960
attached = true
6061
source = piped
62+
piped.once('end', () => {
63+
sourceEnded = true
64+
})
65+
piped.once('close', () => {
66+
sourceEnded = true
67+
})
6168
})
6269

6370
const output = new PassThrough({ objectMode: true })
6471

65-
const destroyQuery = () => whenCursorCanClose(source, () => query.destroy())
72+
const destroyQuery = () =>
73+
whenCursorCanClose(source, () => {
74+
// knex releases the connection twice over a query's life: from `ensureConnection`'s finally
75+
// when the query stream ends, and from its own stream's 'close'. Destroying a stream whose
76+
// query already ended fires that 'close' a second time, and tarn matches a release by
77+
// resource identity, so the connection can be freed again after the pool has handed it to
78+
// another request — two requests then share one client. Only destroy a query that is still
79+
// running, so the connection is released by the path that owns it.
80+
if (query.destroyed || query.readableEnded || sourceEnded) {
81+
return
82+
}
83+
query.destroy()
84+
})
6685

6786
const release = () => {
6887
if (query.destroyed || query.readableEnded) {

‎test/unit/utils/query-stream.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,34 @@ describe('abortableQueryStream', () => {
161161
expect(state.sourceDestroyed).to.equal(true)
162162
})
163163

164+
it('does not release the connection again when the query ends while the abort waits for its cursor', async () => {
165+
const { stream, attach, state } = fakeKnexStream()
166+
const controller = new AbortController()
167+
168+
const done = pipeline(abortableQueryStream(stream, controller.signal), drain())
169+
const source = attach('busy')
170+
// knex releases from `ensureConnection`'s finally too, when the query stream ends.
171+
const releaseFromFinally = () => {
172+
state.released += 1
173+
}
174+
source.once('end', releaseFromFinally)
175+
176+
controller.abort()
177+
await expect(done).to.eventually.be.rejected.and.have.property('name', 'AbortError')
178+
await tick()
179+
180+
// The query ends by itself before the poll that waits for the cursor fires: the connection is
181+
// already back in the pool, so a destroy now would release it a second time, and tarn matches
182+
// a release by identity — a connection it has since handed to another request would be freed
183+
// under that request.
184+
source.cursor = { state: 'done' }
185+
source.emit('end')
186+
await new Promise((resolve) => setTimeout(resolve, 30))
187+
188+
expect(state.released).to.equal(1)
189+
expect(stream.destroyed).to.equal(false)
190+
})
191+
164192
it('is already aborted when handed an aborted signal, without touching an unattached query', async () => {
165193
const { stream, attach, state } = fakeKnexStream()
166194
const controller = new AbortController()

0 commit comments

Comments
 (0)