Repository navigation
Description
A component-local derived$ can livelock an async child reader when a pending declaration changes from one thrown promise to another. The two definitions both read the same signal; resolving the old promise while the replacement remains pending can cause the child’s read() to repeatedly retry an already-settled waiter.
Reproduction
Add these two files and the focused Vitest configuration to an Octane checkout with dependencies installed:
packages/octane/tests/_fixtures/waiter-repro.tsrx:
/** @jsxImportSource octane */
import { derived$, type SignalHandle } from "octane/signals";
function Reader(props: { source$: SignalHandle<string> }) @{
const result$ = derived$(async ({ read }) => await read(props.source$));
const result = result$.snapshot();
<span>{result.status === "ready" ? result.value : result.status}</span>
}
export function Source(props: { first: Promise<void>; next: Promise<string>; useNext: boolean }) @{
const value$ = derived$(() => {
if (props.useNext) return props.next;
throw props.first;
});
const current = value$.snapshot();
<main>
<p>{current.status === "ready" ? current.value : current.status}</p>
<Reader source$={value$} />
</main>
}
export function SwitchedSource(props: { ready$: SignalHandle<boolean>; waiting: Promise<void> }) @{
const value$ = derived$(() => {
if (!props.ready$.get()) throw props.waiting;
return "ready";
});
const current = value$.snapshot();
<main>
<p>{current.status === "ready" ? current.value : current.status}</p>
<Reader source$={value$} />
</main>
}
packages/octane/tests/waiter-repro.test.ts:
import { describe, expect, it } from "vitest";
import { createScope } from "octane/signals";
import { act, mount } from "./_helpers.js";
import { drainProducers } from "./_fixtures/signals-async-controls";
import { Source, SwitchedSource } from "./_fixtures/waiter-repro.tsrx";
function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((done) => { resolve = done; });
return { promise, resolve };
}
describe("accepted pending derived replacement", () => {
it("updates a child async reader when the replacement settles", async () => {
const first = deferred<void>();
const next = deferred<string>();
const root = mount(Source, { first: first.promise, next: next.promise, useNext: false });
try {
expect(root.find("span").textContent).toBe("pending");
await act(() => root.update(Source, { first: first.promise, next: next.promise, useNext: true }));
await act(() => next.resolve("new"));
await drainProducers();
expect(root.find("p").textContent).toBe("new");
expect(root.find("span").textContent).toBe("new");
} finally { root.unmount(); }
});
});
it("keeps a child reader pending when the old source settles first", async () => {
const first = deferred<void>();
const next = deferred<void>();
const owner = createScope({ scopeKey: "ready" });
const ready$ = owner.signal$("ready", false);
const root = mount(SwitchedSource, { ready$, waiting: first.promise });
try {
expect(root.find("span").textContent).toBe("pending");
await act(() => root.update(SwitchedSource, { ready$, waiting: next.promise }));
process.stderr.write("WAIT_REPRO_BEFORE_OLD_SETTLES\n");
first.resolve();
await drainProducers();
process.stderr.write("WAIT_REPRO_AFTER_OLD_SETTLES\n");
expect(root.find("span").textContent).toBe("pending");
await act(() => { ready$.set(true); next.resolve(); });
await drainProducers();
expect(root.find("span").textContent).toBe("ready");
} finally { root.unmount(); owner.dispose(); }
});vitest.waiter-repro.config.mjs:
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { defineConfig } from "vitest/config";
import { octane } from "./packages/octane/src/compiler/vite.js";
import { scopedSignalsProjects } from "./scripts/scoped-signals-projects.mjs";
const packageRoot = resolve("./packages/octane");
const pkg = JSON.parse(readFileSync(resolve(packageRoot, "package.json"), "utf8"));
const alias = Object.entries(pkg.exports).map(([name, target]) => ({
find: name === "." ? "octane" : "octane" + name.slice(1),
replacement: resolve(packageRoot, typeof target === "string" ? target : target.default),
})).sort((a, b) => b.find.length - a.find.length);
process.env.OCTANE_COMPILE_FROZEN_AST ??= "1";
process.env.OCTANE_COMPILE_ASSERT_LOC ??= "1";
export default defineConfig({ test: { projects: scopedSignalsProjects(octane)
.filter((p) => ["octane-signals", "octane-signals-prod", "octane-signals-strong"].includes(p.test?.name))
.map((p) => ({ ...p, root: resolve(packageRoot, "tests"), resolve: { alias },
test: { ...p.test, include: ["waiter-repro.test.ts"], exclude: [], setupFiles: [resolve(packageRoot, "tests/_per-test-setup.ts")], testTimeout: 6000 } })) } });Run the focused test with an external watchdog, because a microtask loop prevents Vitest’s timer from firing:
timeout -k 3s 30s node ./node_modules/vitest/vitest.mjs run --config vitest.waiter-repro.config.mjs --project octane-signals --maxWorkers=1 --no-file-parallelism --reporter=verbose -t "keeps a child reader"Expected: Resolving the old promise does not prevent the event loop from progressing; the child remains pending until the replacement becomes ready, then displays ready.
Actual: The test prints WAIT_REPRO_BEFORE_OLD_SETTLES, never prints WAIT_REPRO_AFTER_OLD_SETTLES, and the watchdog exits with status 124.
The separate updates a child async reader when the replacement settles test is a control. The P1-to-P2 test passes against the 0.9.1 source plus #1750 and also when only #1755 is reversed from the tested source; it hangs against an archive of main at 71d40705014cad64028d2be5218fb53ab38d457b. This may be related to the pending-wakeup hazards noted in #1755.
Activity
fix(octane): evaluate a derived$ again once the thenable it threw set…