Skip to content

An effect cleanup that calls a parent's action throws in development when a for row is removed #89

Open

Description

@cgaaf

A child that registers something with its parent on mount has one way to undo it on unmount: return a cleanup from effect (EF-5) that calls an action the parent passed down, since only the declaring block writes a source and "others get an action to call" (W-5, O-4). The compiler accepts this. But when the child is a row of a for and the row is removed, the cleanup throws ACTION_CALLED_IN_OWNED_SCOPE in development and the action never runs.

The cause is in Solid 2: a list disposes a removed row while the list's own owner is current, and the dev-only guard in action() lacks the teardown exemption that solidjs/solid#4168 gave Solid's write guards. Reported there as solidjs/solid#5123, with a proposed fix in solidjs/solid#5124. Filed here as well because ReactiveScript's rules lead to this pattern, and because the options below are ReactiveScript's to choose.

Program

component Row(props: { id: number, release: (id: number) => void }) {
  effect () { return () => { props.release(props.id) } }
  <li>{props.id}{" "}</li>
}
export default component App() {
  source rows: { id: number }[] := [{ id: 1 }, { id: 2 }]
  source released: number[] := []
  action release(id: number) { released := [...released, id] }
  <main>
    <button onClick={() => { rows := rows.filter(r => r.id != 1) }}>remove</button>{" "}
    <ul>{for row in rows { <Row id={row.id} release={release} /> }}</ul>
    <p>{"released: " + str(released)}</p>
  </main>
}

Frames

Expected (what production shows):

instant action frame
0 mount remove 1 2 released: []
1 handler rows := rows.filter(r => r.id != 1) (the button) remove 2 released: [1]

Development with @reactivescript/compiler and @reactivescript/runtime 0.3.1 on solid-js 2.0.3: instant 1 shows remove 2 released: [], and the flush throws [ACTION_CALLED_IN_OWNED_SCOPE] Calling an action inside an owned scope (component, computation) is not allowed.

Variant, the button doing rows := [] so both rows leave in one pass: expected remove released: [2,1]; development shows remove released: [] and throws.

An if in place of the for does not hit this: Solid disposes a branch at commit, with no owner.

Versions: the published 0.3.1 packages. HEAD (eab7356) has the same effect() and action() wrappers in ref/compile/rt.ts. At the Solid commit ref/ pins (2105ffb), the action() guard is if (owner && !(owner._config & CONFIG_CHILDREN_FORBIDDEN)), also without the exemption (read from source; the program was run on 2.0.3 and on Solid main).

Driver: mounts the program in happy-dom and clicks the button once

With @reactivescript/compiler@0.3.1, @reactivescript/runtime@0.3.1, solid-js@2.0.3, @solidjs/web@2.0.3, @solidjs/signals@2.0.3 and happy-dom@15.11.7 installed:

node --conditions=browser --conditions=development rx-run.mjs rows.rx   # development
node --conditions=browser rx-run.mjs rows.rx                            # production
node --conditions=browser --conditions=development rx-run.mjs rows.rx workaround   # cleanups under runWithOwner(null)
// rx-run.mjs
import { Window } from "happy-dom";
const win = new Window();
for (const k of ["window", "document", "Node", "Element", "HTMLElement", "Text", "Comment", "DocumentFragment", "MouseEvent", "Event", "navigator"]) globalThis[k] ??= k === "window" ? win : win[k];
const { writeFileSync } = await import("node:fs");
const path = await import("node:path");
const { app: compile } = await import("@reactivescript/compiler");
const { createApp } = await import("@reactivescript/runtime/web");
const { flush, runWithOwner, DEV } = await import("solid-js");
const file = path.resolve(process.argv[2]);
const out = file.replace(/\.rx$/, ".compiled.mjs");
writeFileSync(out, JSON.parse(compile(file, "dom", "@solidjs/web", true)).modules[file].code);
const program = (await import(out)).default;
const errors = [];
process.on("uncaughtException", e => errors.push(e.message.slice(0, 70)));
process.on("unhandledRejection", e => errors.push(String(e?.message ?? e).slice(0, 70)));
const root = document.createElement("div");
document.body.appendChild(root);
const app = createApp({ root });
if (process.argv[3] === "workaround") {
  const effect = app.effect.bind(app);
  app.effect = (n, inputs, apply) => effect(n, inputs, v => { const c = apply(v); return typeof c === "function" ? () => runWithOwner(null, c) : c; });
}
const frame = () => root.textContent.replace(/\s+/g, " ").trim();
const settle = async () => { for (let i = 0; i < 5; i++) { flush(); await new Promise(r => setTimeout(r, 5)); } };
try { app.mount(program(app)); } catch (e) { errors.push("mount: " + e.message.slice(0, 60)); }
await settle();
console.log(DEV ? "development" : "production", process.argv[3] ?? "stock");
console.log("frame before:", frame());
root.querySelector("button").dispatchEvent(new win.MouseEvent("click", { bubbles: true }));
try { flush(); } catch (e) { errors.push("flush: " + e.message.slice(0, 64)); }
await settle();
console.log("frame after:", frame());
console.log("errors:", errors.length ? errors : "none");

Results

Frame after the click, for the program (one row leaves) and the variant (both leave):

Runtime Solid Mode One row Both rows
stock 2.0.3, or main at 99db9bc development throws, released: [] throws, released: []
stock 2.0.3, or main at 99db9bc production released: [1] released: [2,1]
stock main at 99db9bc with the proposed fix development and production released: [1] released: [2,1]
cleanups wrapped in runWithOwner(null, …) any of the above development and production released: [1] released: [1]
no-await actions compiled as plain functions any of the above development and production released: [1] released: [2,1]

The last two rows are options on the ReactiveScript side.

Options on the ReactiveScript side

  • Rely on the Solid fix. With it, both programs give the expected frames in development, and ReactiveScript's interpreter, oracle and compiled runners pass on the pinned Solid with the same change applied (oracle and compiled: 130 of 130 scenarios, identical to without it; ssr and types not run). No scenario covers a cleanup calling an action; this program could become one, and I'm happy to write it.
  • Wrap returned cleanups in runWithOwner(null, …) in effect(). This stops the throw on today's Solid, but an action run with no owner reads committed state. When two rows leave in one pass and the action reads the source directly (released := [...released, id]), both calls read [] and one release is lost ([1] instead of [2,1]). An action that uses a functional update (released := old => [...old, id]) gives [2,1] under the wrapper too.
  • Compile an action with no await as a plain function. The reference says such an action "is an ordinary instant", and a plain write in a cleanup is allowed since fix(signals): a removed row's cleanup can write during teardown (#4165) solid#4168. Simulated by rewriting the compiled module (__rt.action(function* …) → a plain function), both programs give the expected frames on unpatched Solid, in development and production. Not checked: how this fits AC-1 (a handler joining the action's change), R-f-6 (an action catching its own failures), callers that await the action, and the ReactiveScript suite, whose runners use the reference emitter, not this rewrite.

A question for the spec

With both rows leaving in one pass, today's production and the Solid fix give [2,1], and runWithOwner(null) gives [1], which is also what a handler calling release(2); release(1) gives today, in development and production. Which result should the spec promise for actions called from cleanups in one teardown?

Related

When the flush that removes the row also delivers an async read's answer, Solid catches the cleanup's error at the end of that flush and records it as the read's failure. In ReactiveScript that shows up as an unrelated catch firing: with raw = await loadItems() and items = raw catch (e) => [], the guard error emptied the list. Covered in the Solid issue; mentioned here because it is what a ReactiveScript user sees.


AI disclosure. GPT 6.2 Astra (in Codex) first hit this error in a ReactiveScript component library. Claude Opus 5.5 (in Claude Code) traced it to Solid's action() guard, wrote the program and driver above, ran every configuration in the results table, simulated the plain-function lowering, and ran ReactiveScript's interpreter, oracle and compiled runners with the Solid fix. Claude Fable 5.2 and Gemini 4 Carbon (in the Antigravity CLI) independently reviewed the Solid report this builds on; Gemini suggested reproducing with a synchronous source and lowering no-await actions to plain functions, both of which Claude Opus 5.5 then tested.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions