Skip to content
TypeScript

fix(shared): preserve SQLite reader finalizers during shutdown (#20305)

ObliviousObserver15341e8643e6
diff --git a/packages/shared/src/nodeSqliteClient.test.ts b/packages/shared/src/nodeSqliteClient.test.ts
index a50fda417eb..1ad763de4bf 100644
--- a/packages/shared/src/nodeSqliteClient.test.ts
+++ b/packages/shared/src/nodeSqliteClient.test.ts
@@ -707,6 +707,43 @@ it.effect("lets a read be cancelled while its worker is still starting", () =>
),
);
+it.effect.each(["cold", "warm", "stalled"])(
+ "completes an interrupted shutdown finalizer's read with a %s reader",
+ (state) => {
+ const { warnings, layer: logs } = captureWarnings();
+ return withReaderClient((sql) => {
+ const shutdown = Effect.gen(function* () {
+ const read = sql.withTransaction(countEntries(sql)).pipe(SqliteClient.readOnly);
+ if (state === "warm") yield* read;
+ const registered = yield* Deferred.make<void>();
+ const finalizing = yield* Deferred.make<void>();
+ let observed: number | undefined;
+ const fiber = yield* Effect.scoped(
+ Effect.gen(function* () {
+ yield* Effect.addFinalizer(() =>
+ Effect.gen(function* () {
+ yield* Deferred.succeed(finalizing, undefined);
+ observed = yield* read;
+ }).pipe(Effect.orDie),
+ );
+ yield* Deferred.succeed(registered, undefined);
+ return yield* Effect.never;
+ }),
+ ).pipe(Effect.forkChild);
+ yield* Deferred.await(registered);
+ const interrupted = yield* Effect.forkChild(Fiber.interrupt(fiber));
+ yield* Deferred.await(finalizing);
+ if (state === "stalled") yield* TestClock.adjust("10 seconds");
+ yield* Fiber.join(interrupted);
+
+ assert.equal(observed, 1);
+ assert.lengthOf(warnings, state === "stalled" ? 1 : 0);
+ });
+ return state === "stalled" ? silently(shutdown) : shutdown;
+ }).pipe(Effect.provide(logs));
+ },
+);
+
it.effect("logs one warning when concurrent first reads find the reader unavailable", () => {
const { warnings, layer: logs } = captureWarnings();
return withFileClient((sql, filename) =>
diff --git a/packages/shared/src/nodeSqliteClient.ts b/packages/shared/src/nodeSqliteClient.ts
index 7ee8292cb71..03283a614ee 100644
--- a/packages/shared/src/nodeSqliteClient.ts
+++ b/packages/shared/src/nodeSqliteClient.ts
@@ -48,9 +48,11 @@ export interface SqliteClientConfig {
readonly checkpointWorker?: boolean | undefined;
}
-const ReadOnly = Context.Reference<boolean>("@t3tools/shared/nodeSqliteClient/ReadOnly", {
- defaultValue: () => false,
-});
+// SqlClient masks transaction acquisition, so readOnly carries the caller's interruption policy.
+const ReadOnly = Context.Reference<typeof Effect.interruptible | undefined>(
+ "@t3tools/shared/nodeSqliteClient/ReadOnly",
+ { defaultValue: () => undefined },
+);
/**
* Wrap the whole transaction, as in
@@ -59,7 +61,9 @@ const ReadOnly = Context.Reference<boolean>("@t3tools/shared/nodeSqliteClient/Re
* with "attempt to write a readonly database".
*/
export const readOnly = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
- Effect.provideService(effect, ReadOnly, true);
+ Effect.uninterruptibleMask((restore) =>
+ restore(Effect.provideService(effect, ReadOnly, restore)),
+ );
interface Acquired {
readonly connection: "writer" | "reader";
@@ -441,15 +445,16 @@ const makeReader = Effect.fnUntraced(function* (options: SqliteClientConfig) {
} catch (cause) {
return turnOff(undefined, String(cause));
}
- // SqlClient acquires transaction connections uninterruptibly. Waiting for the
- // worker happens before any lock is taken, so let a cancelled request leave.
return Effect.flatMap(
- Effect.interruptible(
- Effect.timeoutOption(
- Effect.promise(() => reader.started),
- startupTimeout,
- ),
- ),
+ Effect.withFiber((fiber) => {
+ const restore = Context.get(fiber.context, ReadOnly) ?? identity;
+ return restore(
+ Effect.timeoutOption(
+ Effect.promise(() => reader.started),
+ startupTimeout,
+ ),
+ );
+ }),
(startup): Effect.Effect<Connection | undefined> =>
// Another acquire may have timed out and retired this worker while this one
// waited; a late "ready" must not hand out a worker that is being terminated.