authoravatarclover caruso<git@paperclover.net>2026-10-14 17:55:55-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-11 11:13:55-08:00
loge974dee2fb62fa115dc200354f85ff69145c36c7
treec8c6ff8378be22d4c82ac6ee72805e87ba2e517d
parent7d66362e0dbb074ed16b613b8bab554f5b231875
signature Signed by SSH key SHA256:cOKiuRFOeSRxne6EWgHtdQQSlBxjOXm2hOCFnCdLQbQ

fix: route synchronous mutate throws into the error path

#executeNext marked the channel "mutating" and then chained .then() onto the return value of mutate. A mutate (or useAsyncCallback callback) that threw synchronously escaped to the caller instead, leaving the channel "mutating" forever: isMutating and isDisabled stuck true and later calls queued behind it. Assisted-by: claude-opus-5.5 Assisted-by: claude-opus-5

3 files changed, 30 insertions(+), 1 deletions(-)

readme.changes.md+2
......@@ -5,6 +5,8 @@
55### bugfixes
66
77- resolve calls made while a failed mutation refetches never running
8- resolve a synchronous throw from `mutate` escaping to the caller and leaving
9 the mutation stuck in its mutating state
810
911## v3.0.2
1012
src/mutation.ts+1-1
......@@ -599,7 +599,7 @@ export class BlockingMutation<
599599 channel.status = "mutating";
600600 this.#notify(channel, "mutating");
601601
602 this.#options.mutate.call(context, ...args).then((result) => {
602 new Promise<Result>((resolve) => resolve(this.#options.mutate.call(context, ...args))).then((result) => {
603603 // remove rollbacks and apply optimistic success handlers
604604 channel.rollbacks.splice(0, item.rollbacks);
605605 onSuccess.forEach((cb) => cb(result));
test/ordering.test.ts+27
......@@ -464,3 +464,30 @@ test("a call made during the post-failure refetch runs once the refetch settles"
464464 assertEquals(calls, ["mutate-1", "mutate-2"]);
465465 assertEquals(successMessages, ["Completed 2"]);
466466});
467
468test("a synchronous throw from mutate fails the call without wedging the channel", async () => {
469 vi.useFakeTimers();
470 const { client, errorMessages, successMessages } = createTestMutationClient();
471 const calls: string[] = [];
472
473 const mutTest = client.define({
474 id: "test-11",
475 mutate: (id: number) => {
476 calls.push(`mutate-${id}`);
477 if (id === 1) throw new Error("Sync failure");
478 return Promise.resolve(`result-${id}`);
479 },
480 describe: "test",
481 describeResult: (ctx) => `Completed ${ctx.args[0]}`,
482 optimistic: () => {},
483 });
484
485 mutTest.run(1);
486 await vi.advanceTimersByTimeAsync(0);
487 mutTest.run(2);
488 await vi.advanceTimersByTimeAsync(0);
489
490 assertEquals(calls, ["mutate-1", "mutate-2"]);
491 assertEquals(errorMessages.map((e) => e.message), ["Could not test: Sync failure"]);
492 assertEquals(successMessages, ["Completed 2"]);
493});