Skip to content

Commit 2cb498e

Browse files
effect-bottim-smart
andcommitted
Fix SQLite connection leak when a queued transaction is interrupted (#9962)
Co-authored-by: Tim <[email protected]>
1 parent 2d1fcd4 commit 2cb498e

5 files changed

Lines changed: 129 additions & 8 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Fix a SQLite client hanging forever after a queued `withTransaction` or `reserve` was interrupted just as it received the connection. The interrupted acquisition could keep the connection's only permit without registering its release.
6+
7+
Add `Semaphore.takeScoped`, which holds permits until the current scope closes. Waiting can be interrupted unless the caller is uninterruptible, and acquired permits are always returned when the scope closes.

‎packages/effect/src/Semaphore.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { dual, identity } from "./Function.ts"
1717
import * as core from "./internal/core.ts"
1818
import * as internal from "./internal/effect.ts"
1919
import type * as Option from "./Option.ts"
20+
import type * as Scope from "./Scope.ts"
2021

2122
/**
2223
* A counting semaphore that coordinates concurrent access with permits.
@@ -134,6 +135,24 @@ export interface Semaphore {
134135
*/
135136
take(this: Semaphore, permits: number): Effect.Effect<number>
136137

138+
/**
139+
* Acquires the specified number of permits for the lifetime of the current
140+
* scope, suspending the task if they are not yet available.
141+
*
142+
* **When to use**
143+
*
144+
* Use to hold permits until a scope closes, such as while a scoped resource
145+
* is leased.
146+
*
147+
* **Details**
148+
*
149+
* Waiting can be interrupted unless the caller is uninterruptible. The
150+
* permits are taken and their release is added to the scope in one
151+
* uninterruptible step, so acquired permits are always returned when the
152+
* scope closes.
153+
*/
154+
takeScoped(this: Semaphore, permits: number): Effect.Effect<void, never, Scope.Scope>
155+
137156
/**
138157
* Acquires the specified number of permits only if they are immediately
139158
* available.
@@ -281,6 +300,21 @@ class SemaphoreImpl implements Semaphore {
281300
return takePermits(this, n, () => internal.succeed(n))
282301
}
283302

303+
takeScoped(n: number): Effect.Effect<void, never, Scope.Scope> {
304+
return internal.uninterruptibleMask((restore) =>
305+
internal.flatMap(internal.scope, (scope) => {
306+
const acquire: Effect.Effect<void> = internal.suspend(() => {
307+
if (this.free < n) {
308+
return internal.flatMap(restore(waitForPermits(this, n, internal.void)), () => acquire)
309+
}
310+
this.taken += n
311+
return internal.scopeAddFinalizer(scope, this.release(n))
312+
})
313+
return acquire
314+
})
315+
)
316+
}
317+
284318
takeIfAvailable(n: number): Effect.Effect<boolean> {
285319
return internal.suspend(() => {
286320
if (this.free < n) return internal.succeed(false)
@@ -539,6 +573,47 @@ export const take: {
539573
(self: Semaphore, permits: number): Effect.Effect<number>
540574
} = dual(2, (self: Semaphore, permits: number) => self.take(permits))
541575

576+
/**
577+
* Acquires the specified number of permits until the current scope closes.
578+
*
579+
* **When to use**
580+
*
581+
* Use when permits should be held for the lifetime of a scope, such as while a
582+
* scoped resource is leased.
583+
*
584+
* **Details**
585+
*
586+
* The effect waits until enough permits are available. The wait can be
587+
* interrupted unless the caller is uninterruptible. The permits are taken and
588+
* their release is added to the scope in one uninterruptible step, so acquired
589+
* permits are always returned when the scope closes.
590+
*
591+
* **Example** (Holding a permit for a scope)
592+
*
593+
* ```ts import.meta.vitest
594+
* import { Effect, Semaphore } from "effect"
595+
*
596+
* const program = Effect.gen(function*() {
597+
* const semaphore = yield* Semaphore.make(1)
598+
* yield* Effect.scoped(Semaphore.takeScoped(semaphore, 1))
599+
* return yield* Semaphore.takeIfAvailable(semaphore, 1)
600+
* })
601+
*
602+
* Effect.runSync(program) // => true
603+
* ```
604+
*
605+
* @see {@link take} for acquiring permits that are released manually
606+
* @see {@link withPermits} for holding permits while one effect runs
607+
*
608+
* @stability stable
609+
* @category combinators
610+
* @since 4.0.7
611+
*/
612+
export const takeScoped: {
613+
(permits: number): (self: Semaphore) => Effect.Effect<void, never, Scope.Scope>
614+
(self: Semaphore, permits: number): Effect.Effect<void, never, Scope.Scope>
615+
} = dual(2, (self: Semaphore, permits: number) => self.takeScoped(permits))
616+
542617
/**
543618
* Acquires the specified number of permits only if they are immediately
544619
* available.

‎packages/effect/src/sql/SqlClient.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,7 @@ export const makeSqliteAcquirers = <C extends Connection.Connection>(options: {
429429
})
430430
})
431431

432-
const lease = Effect.uninterruptibleMask((restore) =>
433-
Effect.flatMap(Effect.scope, (scope) =>
434-
restore(semaphore.take(1)).pipe(
435-
Effect.andThen(Scope.addFinalizer(scope, semaphore.release(1))),
436-
Effect.andThen(available)
437-
))
438-
)
432+
const lease = Effect.uninterruptibleMask((restore) => Effect.andThen(restore(semaphore.takeScoped(1)), available))
439433

440434
return {
441435
acquirer: lease,

‎packages/effect/test/Semaphore.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert, describe, it } from "@effect/vitest"
2-
import { Duration, Effect, Fiber, Option, PartitionedSemaphore, Semaphore } from "effect"
2+
import { Duration, Effect, Exit, Fiber, Option, PartitionedSemaphore, Scope, Semaphore } from "effect"
33
import * as Scheduler from "effect/Scheduler"
44
import { TestClock } from "effect/testing"
55

@@ -529,6 +529,31 @@ describe("Semaphore", () => {
529529
assert.isTrue(yield* sem.takeIfAvailable(1))
530530
}))
531531

532+
it.effect("takeScoped holds permits until the scope closes", () =>
533+
Effect.gen(function*() {
534+
const sem = yield* Semaphore.make(2)
535+
const scope = yield* Scope.make()
536+
537+
yield* Scope.provide(sem.takeScoped(2), scope)
538+
assert.isFalse(yield* sem.takeIfAvailable(1))
539+
540+
yield* Scope.close(scope, Exit.void)
541+
assert.isTrue(yield* sem.takeIfAvailable(2))
542+
}))
543+
544+
it.effect("takeScoped takes no permits when interrupted while waiting", () =>
545+
Effect.gen(function*() {
546+
const sem = yield* Semaphore.make(1)
547+
yield* sem.take(1)
548+
// The scope stays open, so only the interruption can end the acquisition.
549+
const scope = yield* Scope.make()
550+
const waiter = yield* Effect.forkChild(Scope.provide(sem.takeScoped(1), scope), { startImmediately: true })
551+
552+
yield* Fiber.interrupt(waiter)
553+
yield* sem.release(1)
554+
assert.isTrue(yield* sem.takeIfAvailable(1))
555+
}))
556+
532557
it.effect("module-level combinators delegate to the instance api", () =>
533558
Effect.gen(function*() {
534559
const sem = yield* Semaphore.make(1)

‎packages/sql/sqlite-node/test/Client.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,26 @@ describe("Client", () => {
378378
}
379379
}))
380380

381+
it.effect("releases the connection when a queued transaction is interrupted", () =>
382+
Effect.gen(function*() {
383+
// Sweep the interrupt across the gap between taking the connection and registering its release.
384+
for (let delay = 0; delay < 31; delay++) {
385+
yield* Effect.scoped(Effect.gen(function*() {
386+
const sql = yield* SqliteClient.make({ filename: ":memory:" }).pipe(Effect.provide(Reactivity.layer))
387+
const holder = yield* Effect.forkChild(sql`SELECT 1`)
388+
const queued = yield* sql.withTransaction(sql`SELECT 2`).pipe(
389+
Effect.provideService(References.MaxOpsBeforeYield, 3),
390+
Effect.forkChild
391+
)
392+
for (let i = 0; i < delay; i++) yield* Effect.yieldNow
393+
yield* Fiber.interrupt(queued)
394+
yield* Fiber.join(holder)
395+
const next = yield* Effect.forkChild(sql`SELECT 3`, { startImmediately: true })
396+
assert.isDefined(next.pollUnsafe(), `connection leaked at delay ${delay}`)
397+
}))
398+
}
399+
}))
400+
381401
it.effect("uses a 5 second busy timeout", () =>
382402
Effect.gen(function*() {
383403
const sql = yield* makeClient

0 commit comments

Comments
 (0)