From f757343ec45bdf8dd42b3f1d07c8a5f99d0f3abd Mon Sep 17 00:00:00 2001 From: Chris Gaafary <11409288+cgaaf@users.noreply.github.com> Date: Fri, 20 Nov 2026 09:52:23 -0500 Subject: [PATCH] fix(signals): a removed row's cleanup can call an action during teardown (#5123) #4168 let a cleanup write during teardown by standing the owned-scope write guard down while disposalDepth is raised. The action() guard, which its comment calls the same scope test as setSignal, did not get the exemption, so a keyed row whose cleanup invokes an action threw ACTION_CALLED_IN_OWNED_SCOPE in development while production ran it. Co-Authored-By: Claude Opus 5.5 --- .changeset/cleanup-action-during-teardown.md | 5 + packages/signals/src/core/action.ts | 13 +- .../tests/for-row-cleanup-action.test.ts | 118 ++++++++++++++++++ .../web/test/for-row-cleanup-action.spec.tsx | 44 +++++++ 4 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 .changeset/cleanup-action-during-teardown.md create mode 100644 packages/signals/tests/for-row-cleanup-action.test.ts create mode 100644 packages/web/test/for-row-cleanup-action.spec.tsx diff --git a/.changeset/cleanup-action-during-teardown.md b/.changeset/cleanup-action-during-teardown.md new file mode 100644 index 000000000..210d5f727 --- /dev/null +++ b/.changeset/cleanup-action-during-teardown.md @@ -0,0 +1,5 @@ +--- +"@solidjs/signals": patch +--- + +Allow calling an action from a cleanup during teardown, as #4165 allowed writes. Removing a keyed row ran its cleanups inside the list's pass, so an action invoked from an `onCleanup` or an effect's returned cleanup threw `ACTION_CALLED_IN_OWNED_SCOPE` in development, while the same cleanup under a branch switching away ran. The dev-only action guard now stands down while cleanups run, like the owned-scope write guard. Production builds are unchanged. diff --git a/packages/signals/src/core/action.ts b/packages/signals/src/core/action.ts index f6551bfdc..88a2462d4 100644 --- a/packages/signals/src/core/action.ts +++ b/packages/signals/src/core/action.ts @@ -1,6 +1,6 @@ import { CONFIG_CHILDREN_FORBIDDEN } from "./constants.js"; import { isThenable } from "./async.js"; -import { enterCallback, exitCallback, inRefCallback } from "./core.js"; +import { disposalDepth, enterCallback, exitCallback, inRefCallback } from "./core.js"; import { getOwner } from "./owner.js"; import type { Owner } from "./types.js"; import { emitDiagnostic } from "./dev.js"; @@ -136,10 +136,17 @@ export function action( // and a computation tracking what its action writes livelocks (every // write retriggers the compute, which fires a fresh invocation). Same // scope test as setSignal: leaf imperative scopes (tracked effects, - // onSettled) stay legal. + // onSettled) stay legal, and so do cleanups during teardown — a cleanup + // is not part of the computation disposing its owner (a list pass + // removing a row), so invoking an action there is imperative (#4165). if (__DEV__) { const owner = getOwner(); - if (owner && !(owner._config & CONFIG_CHILDREN_FORBIDDEN) && !inRefCallback()) { + if ( + owner && + !(owner._config & CONFIG_CHILDREN_FORBIDDEN) && + !disposalDepth && + !inRefCallback() + ) { emitDiagnostic({ code: "ACTION_CALLED_IN_OWNED_SCOPE", kind: "write", diff --git a/packages/signals/tests/for-row-cleanup-action.test.ts b/packages/signals/tests/for-row-cleanup-action.test.ts new file mode 100644 index 000000000..05f0d4233 --- /dev/null +++ b/packages/signals/tests/for-row-cleanup-action.test.ts @@ -0,0 +1,118 @@ +/** + * #4165 let a cleanup write during teardown: a cleanup is teardown, not part + * of whichever computation is disposing its owner. Invoking an action from a + * cleanup is the same imperative act, so the action guard stands down there + * too. Without it, removing a keyed row whose cleanup calls an action threw + * ACTION_CALLED_IN_OWNED_SCOPE in development, while the same cleanup under a + * branch switching away ran. + */ +import { afterEach, expect, it } from "vitest"; +import { + action, + createEffect, + createMemo, + createRenderEffect, + createRoot, + createSignal, + flush, + mapArray, + onCleanup, + untrack +} from "../src/index.js"; + +afterEach(() => flush()); + +it("a removed row's onCleanup can call an action", async () => { + const [rows, setRows] = createSignal([1, 2]); + const [count, setCount] = createSignal(0); + const release = action(function* () { + setCount(n => n + 1); + }); + const seen: unknown[] = []; + createRoot(() => { + const mapped = mapArray(rows, r => { + onCleanup(() => void release()); + return r; + }); + createRenderEffect( + () => mapped(), + v => void seen.push([...v]) + ); + }); + flush(); + setRows([2]); + flush(); + await Promise.resolve(); + flush(); + setRows([2, 3]); + flush(); + expect(seen).toEqual([[1, 2], [2], [2, 3]]); + expect(count()).toBe(1); +}); + +it("a removed row's effect-returned cleanup can call an action", async () => { + const [rows, setRows] = createSignal([1, 2]); + const [count, setCount] = createSignal(0); + const release = action(function* () { + setCount(n => n + 1); + }); + createRoot(() => { + const mapped = mapArray(rows, r => { + createEffect( + () => r, + () => () => void release() + ); + return r; + }); + createRenderEffect(mapped, () => {}); + }); + flush(); + setRows([2]); + flush(); + await Promise.resolve(); + flush(); + expect(count()).toBe(1); +}); + +it("a branch's cleanup can call an action when the branch switches away", async () => { + const [on, setOn] = createSignal(true); + const [count, setCount] = createSignal(0); + const release = action(function* () { + setCount(n => n + 1); + }); + createRoot(() => { + const branch = createMemo(() => { + if (!on()) return null; + return untrack(() => { + onCleanup(() => void release()); + return 1; + }); + }); + createRenderEffect(branch, () => {}); + }); + flush(); + setOn(false); + flush(); + await Promise.resolve(); + flush(); + expect(count()).toBe(1); +}); + +it("still rejects an action called in a row's mapping body", () => { + const [rows] = createSignal([1]); + const noop = action(function* () {}); + let caught: unknown; + createRoot(() => { + const mapped = mapArray(rows, r => { + try { + noop(); + } catch (e) { + caught = e; + } + return r; + }); + createRenderEffect(mapped, () => {}); + }); + flush(); + expect(String(caught)).toMatch(/ACTION_CALLED_IN_OWNED_SCOPE/); +}); diff --git a/packages/web/test/for-row-cleanup-action.spec.tsx b/packages/web/test/for-row-cleanup-action.spec.tsx new file mode 100644 index 000000000..6f5959cc4 --- /dev/null +++ b/packages/web/test/for-row-cleanup-action.spec.tsx @@ -0,0 +1,44 @@ +/** + * @jsxImportSource @solidjs/web + * @vitest-environment jsdom + */ +// #4165 let a removed row's cleanup write; invoking an action from the +// same cleanup is the same imperative act and must not be rejected as a call +// in the list's owned scope. +import { afterEach, expect, test } from "vitest"; +import { action, createSignal, flush, For, onCleanup, onSettled, resetErrorHalt } from "solid-js"; +import { render } from "../src/index.js"; + +afterEach(() => resetErrorHalt()); + +for (const [label, register] of [ + ["onSettled cleanup", (fn: () => void) => onSettled(() => fn)], + ["onCleanup", (fn: () => void) => onCleanup(fn)] +] as const) + test(`a row's ${label} can call an action when the row is removed`, async () => { + const [rows, setRows] = createSignal([1, 2]); + const [released, setReleased] = createSignal([]); + const release = action(function* (n: number) { + yield Promise.resolve(); + setReleased(r => [...r, n]); + }); + function Row(props: { n: number }) { + register(() => void release(props.n)); + return {props.n}; + } + const div = document.createElement("div"); + const dispose = render(() => {n => }, div); + flush(); + expect(div.textContent).toBe("12"); + setRows([2]); + flush(); + await Promise.resolve(); + await Promise.resolve(); + flush(); + expect(div.textContent).toBe("2"); + expect(released()).toEqual([1]); + setRows([2, 3]); + flush(); + expect(div.textContent).toBe("23"); + dispose(); + });