@devries-cameron20 / t3code Mirror · read-only / commits / 15341e8643
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 23:15 UTC

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

Co-authored-by: Julius Marminge <julius0216@outlook.com>

ObliviousObserver committed Oct 20, 2026 at 00:16 UTC 15341e8643e6278c6dff1ccbead870c6e770c724
2 files changed +54 -12
packages/shared/src/nodeSqliteClient.test.ts
+37
@@ -707,6 +707,43 @@ it.effect("lets a read be cancelled while its worker is still starting", () =>
707 ),
708 );
709
710 +it.effect.each(["cold", "warm", "stalled"])(
711 + "completes an interrupted shutdown finalizer's read with a %s reader",
712 + (state) => {
713 + const { warnings, layer: logs } = captureWarnings();
714 + return withReaderClient((sql) => {
715 + const shutdown = Effect.gen(function* () {
716 + const read = sql.withTransaction(countEntries(sql)).pipe(SqliteClient.readOnly);
717 + if (state === "warm") yield* read;
718 + const registered = yield* Deferred.make<void>();
719 + const finalizing = yield* Deferred.make<void>();
720 + let observed: number | undefined;
721 + const fiber = yield* Effect.scoped(
722 + Effect.gen(function* () {
723 + yield* Effect.addFinalizer(() =>
724 + Effect.gen(function* () {
725 + yield* Deferred.succeed(finalizing, undefined);
726 + observed = yield* read;
727 + }).pipe(Effect.orDie),
728 + );
729 + yield* Deferred.succeed(registered, undefined);
730 + return yield* Effect.never;
731 + }),
732 + ).pipe(Effect.forkChild);
733 + yield* Deferred.await(registered);
734 + const interrupted = yield* Effect.forkChild(Fiber.interrupt(fiber));
735 + yield* Deferred.await(finalizing);
736 + if (state === "stalled") yield* TestClock.adjust("10 seconds");
737 + yield* Fiber.join(interrupted);
738 +
739 + assert.equal(observed, 1);
740 + assert.lengthOf(warnings, state === "stalled" ? 1 : 0);
741 + });
742 + return state === "stalled" ? silently(shutdown) : shutdown;
743 + }).pipe(Effect.provide(logs));
744 + },
745 +);
746 +
747 it.effect("logs one warning when concurrent first reads find the reader unavailable", () => {
748 const { warnings, layer: logs } = captureWarnings();
749 return withFileClient((sql, filename) =>
packages/shared/src/nodeSqliteClient.ts
+17 -12
@@ -48,9 +48,11 @@ export interface SqliteClientConfig {
48 readonly checkpointWorker?: boolean | undefined;
49 }
50
51 -const ReadOnly = Context.Reference<boolean>("@t3tools/shared/nodeSqliteClient/ReadOnly", {
52 - defaultValue: () => false,
53 -});
51 +// SqlClient masks transaction acquisition, so readOnly carries the caller's interruption policy.
52 +const ReadOnly = Context.Reference<typeof Effect.interruptible | undefined>(
53 + "@t3tools/shared/nodeSqliteClient/ReadOnly",
54 + { defaultValue: () => undefined },
55 +);
56
57 /**
58 * Wrap the whole transaction, as in
@@ -59,7 +61,9 @@ const ReadOnly = Context.Reference<boolean>("@t3tools/shared/nodeSqliteClient/Re
61 * with "attempt to write a readonly database".
62 */
63 export const readOnly = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
62 - Effect.provideService(effect, ReadOnly, true);
64 + Effect.uninterruptibleMask((restore) =>
65 + restore(Effect.provideService(effect, ReadOnly, restore)),
66 + );
67
68 interface Acquired {
69 readonly connection: "writer" | "reader";
@@ -441,15 +445,16 @@ const makeReader = Effect.fnUntraced(function* (options: SqliteClientConfig) {
445 } catch (cause) {
446 return turnOff(undefined, String(cause));
447 }
444 - // SqlClient acquires transaction connections uninterruptibly. Waiting for the
445 - // worker happens before any lock is taken, so let a cancelled request leave.
448 return Effect.flatMap(
447 - Effect.interruptible(
448 - Effect.timeoutOption(
449 - Effect.promise(() => reader.started),
450 - startupTimeout,
451 - ),
452 - ),
449 + Effect.withFiber((fiber) => {
450 + const restore = Context.get(fiber.context, ReadOnly) ?? identity;
451 + return restore(
452 + Effect.timeoutOption(
453 + Effect.promise(() => reader.started),
454 + startupTimeout,
455 + ),
456 + );
457 + }),
458 (startup): Effect.Effect<Connection | undefined> =>
459 // Another acquire may have timed out and retired this worker while this one
460 // waited; a late "ready" must not hand out a worker that is being terminated.