Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/shared/src/nodeSqliteClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
29 changes: 17 additions & 12 deletions packages/shared/src/nodeSqliteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand Down Expand Up @@ -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.
Expand Down
Loading