Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/held-error-recovery-before-commit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/signals": patch
---

A memo that recovers before its held error commits shows the recovered value (#4145). D-41 stages an error found while a sibling holds the frame, marking the staging with `CONFIG_STAGED_ERROR`. A later successful pass in the same hold replaced the staged error with its value but left the flag set, so the reveal threw the successful value as an error and halted the reactive system. The pass that stages a value now clears the flag.
3 changes: 3 additions & 0 deletions packages/signals/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,9 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
el._value = value;
} else {
el._pendingValue = value;
// The value replaces an error staged earlier in this hold (D-41,
// #4145): the staging is an answer again, not one to throw.
el._config &= ~CONFIG_STAGED_ERROR;
// Born held: in the transaction from birth, so a read of it this
// tick is a read of its world (the mount-during-a-hold case), not
// only after the seam parks it.
Expand Down
76 changes: 76 additions & 0 deletions packages/signals/tests/held-error-recovery-4145.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* #4145: a memo throws while an async sibling holds the frame (D-41 stages
* the error), then recomputes successfully before that frame commits. The
* success replaces the proposal: when the sibling lands, the frame shows the
* recovered value — it is not thrown as the error it replaced.
*/
import { afterEach, expect, it } from "vitest";
import {
createEffect,
createMemo,
createRenderEffect,
createRoot,
createSignal,
flush,
resetErrorHalt
} from "../src/index.js";

function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>(res => (resolve = res));
return { promise, resolve };
}
// Before the fix the reveal halts the reactive system; keep the cases apart.
afterEach(() => resetErrorHalt());

const tick = async () => {
await new Promise(r => setTimeout(r, 0));
flush();
};

it.each([1, 7])(
"a held error replaced by a value (%s) before its frame commits shows the value",
async recovered => {
const request = deferred<number>();
const boom = new Error("boom");
const [step, setStep] = createSignal(0);
const seen: unknown[] = [];
const errors: unknown[] = [];
let value!: () => number;
const dispose = createRoot(d => {
const slow = createMemo(() => (step() === 0 ? 1 : request.promise));
value = createMemo(() => {
const s = step();
if (s === 1) throw boom;
return s === 0 ? 1 : recovered;
});
const sum = createMemo(() => slow() + value());
createRenderEffect(sum, v => void seen.push(v), { schedule: true });
createEffect(sum, { effect: () => {}, error: e => void errors.push(e) });
return d;
});
try {
flush();
expect(seen).toEqual([2]);

// `slow` holds the frame; `value`'s error is staged behind it.
setStep(1);
flush();
expect(seen).toEqual([2]);

// `value` recovers while the request is still pending.
setStep(2);
flush();
expect(seen).toEqual([2]);

request.resolve(2);
await tick();
expect(errors).toEqual([]);
expect(seen).toEqual([2, 2 + recovered]);
expect(value()).toBe(recovered);
} finally {
dispose();
flush();
}
}
);
Loading