Skip to content

Commit 21a446f

Browse files
committed
fix(shared): a write sent to the writer worker finishes before its caller is interrupted
On the event loop a statement always completed before an interrupt took effect. With the writer in a worker, an interrupted caller released the write lock while the worker was still applying its statement. Writer requests now wait for the reply uninterruptibly; reads stay cancellable.
1 parent 25a4338 commit 21a446f

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎packages/shared/src/nodeSqliteClient.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ it.effect("lets the event loop run while a write waits for another connection's
267267
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
268268
);
269269

270+
it.effect("finishes a write already sent to the writer before its caller is interrupted", () =>
271+
Effect.gen(function* () {
272+
const { filename, other } = yield* makeTempDatabase;
273+
yield* Effect.gen(function* () {
274+
const sql = yield* SqlClient.SqlClient;
275+
yield* sql`PRAGMA busy_timeout = 5000`;
276+
yield* Effect.sync(() => other.exec("BEGIN IMMEDIATE"));
277+
const write = yield* Effect.forkChild(
278+
sql`UPDATE counters SET value = value + 1 WHERE id = 1`,
279+
{ startImmediately: true },
280+
);
281+
yield* eventLoopTurn;
282+
const interrupted = yield* Effect.forkChild(Fiber.interrupt(write), {
283+
startImmediately: true,
284+
});
285+
yield* eventLoopTurn;
286+
// The write is waiting in the worker for the lock, so the interrupt has to wait too.
287+
assert.isUndefined(interrupted.pollUnsafe());
288+
yield* Effect.sync(() => other.exec("COMMIT"));
289+
yield* Fiber.join(interrupted);
290+
assert.deepEqual(yield* sql`SELECT value FROM counters WHERE id = 1`.values, [[1]]);
291+
}).pipe(Effect.provide(SqliteClient.layer({ filename, writerWorker: true })));
292+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
293+
);
294+
270295
it.effect("lets the writer commit while a read-only transaction is open", () =>
271296
withReaderClient((sql) =>
272297
Effect.gen(function* () {

‎packages/shared/src/nodeSqliteClient.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
397397
: Effect.fail(failedWith({ message: stoppedMessage }, "execute"));
398398
}
399399
const safeIntegers = Boolean(Context.get(fiber.context, Client.SafeIntegers));
400-
return Effect.callback<ReadonlyArray<any>, SqlError>((resume) => {
400+
const reply = Effect.callback<ReadonlyArray<any>, SqlError>((resume) => {
401401
const id = nextRequestId++;
402402
pending.set(id, {
403403
endsTransaction,
@@ -413,6 +413,9 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
413413
// oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
414414
worker.postMessage({ id, safeIntegers, ...input, cached });
415415
});
416+
// A posted write runs to the end, as it did on the event loop; the caller and its lock
417+
// must not move on while the worker is still applying it. Reads stay cancellable.
418+
return options.role === "writer" ? Effect.uninterruptible(reply) : reply;
416419
});
417420

418421
const connection = identity<Connection>({

0 commit comments

Comments
 (0)