Skip to content

fix(octane): evaluate a derived$ again once the thenable it threw settles - #1779

Merged
trueadm merged 6 commits into
mainfrom
fix/derived-redeclaration-waiter-spin
Oct 6, 2026
Merged

trueadm merged 6 commits into
mainfrom
fix/derived-redeclaration-waiter-spin

Conversation

@trueadm

@trueadm trueadm commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1775, which fixed #1763 by repairing waiter handling when a redeclared pending derived is accepted. This PR fixes the defect under that report. A synchronous derived$ whose thrown thenable settles is never evaluated again. Its readers wake, re-read the same clean pending state whose waiting promise has already settled, and an async read() spins on microtasks.

That hang still happens on main with #1775 and no redeclaration at all: a derived throws P1, then P1 settles while the computation would still throw it. The issue's reproduction, minus the redeclaration step, runs out of heap on main. With this PR it passes.

What this adds on top of #1775 (graph.ts):

  • Settlement invalidates. When a computation's thrown thenable settles, the node still waiting on it is invalidated: it re-evaluates, and its subscribers are notified. This matches the documented Suspense contract for thrown thenables.
  • No microtask loop. A thenable already seen to settle does not wake the node again. Rethrowing it keeps the node pending until a signal it reads changes, so a lagging computation cannot loop on microtasks.
  • The wakeup follows the state. commitState releases a wakeup its new state no longer presents, so an earlier thrown thenable cannot abort async work that replaced it.
  • Promotion. When fix(octane): keep pending readers attached to accepted work #1775's promotion transfers a view's wakeup to the cell, the wakeup now points at the cell, so its settlement invalidates the cell rather than the retired view.
  • Retention. A wakeup holds its node only while the node waits. Resolving the wakeup, view retirement and candidate release clear it, so a thenable that never settles does not keep a disposed owner alive.

#1775's promotion logic (sameViewResult, waiter transfer, settled/invalidated retry) is kept as merged. This PR's earlier promotion changes were dropped in the merge because #1775 covers them. Docs (docs/signals.md and the website's signals page) state the thrown-thenable contract.

Validation

Against main, which includes #1775, three of this PR's tests fail and none hang:

  • the engine test showing a derived re-evaluates once its thrown thenable settles;
  • the redeclared-definition version of that behaviour;
  • an async reader of a computation that rethrows a settled thenable.

The plain reproduction (no redeclaration) runs out of heap on main.

Part removed (before the #1775 merge) Failing test
whole fix all #1763 tests
commitState releasing a wakeup it no longer presents keeps the async work (attempt aborted)
settled-thenable check async reader keeps waiting on a rethrown settled thenable
resolve() clearing the node GC test; keeps the async work
  • targeted tests, octane-signals: this PR's signals-update-depth and signals-unified plus fix(octane): keep pending readers attached to accepted work #1775's five tests (signals-waiter-equivalent, signals-waiter-race-watchdog, signals-waiter-thenable, signals-pending-promotion, signals-pending-promotion-graph): 48/48 pass on the merged code.
  • signal-local-selectors.test.ts, project octane (promotion and held transitions): 54/54 pass.
  • pnpm format:files:check and pnpm typecheck:files on the changed sources: clean. pnpm sync: no changes.
  • pnpm format:check, pnpm typecheck, pnpm test repo-wide: left to CI. Prod and Strong compile modes are left to CI.
  • Bundle size: no reachability scenario includes the signals graph. The app budgets held in this PR's earlier bench run.

Risk / follow-ups

  • Behavior change. A derived$ that throws a thenable now re-evaluates when the thenable settles. Before, it stayed pending: snapshot readers never updated, and async readers spun. Subscribers of such a value are now notified on settlement.
  • Lagging computations. A computation that rethrows an already-settled thenable now waits for a signal change instead of being polled. Client Suspense retries such a reader once per macrotask. Here, a computation that depends on non-reactive state updated after the thenable settles stays pending until one of its signals changes.
  • Untested retention paths. The GC test covers owner disposal. Released declaration views and candidate targets drop their wakeup's node by the same rule, but no test covers them.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes core signal graph pending/retry semantics for thrown thenables; subscribers now update on settlement, and computations that rethrow settled thenables stay pending until a read dependency changes.

Overview
Fixes #1763: when a synchronous derived$ throws a thenable, the graph now re-runs that computation after the thenable settles and notifies subscribers, instead of leaving readers stuck on a pending value whose wait promise has already resolved.

In graph.ts, pending wakeups track the thrown thenable and the waiting node. Settlement calls settleThrown, which invalidates the node so it evaluates again. A settledThrown weak set stops rethrowing an already-settled thenable from causing a microtask spin (async read() loops). commitState only keeps a wakeup while the new state still exposes its promise; promotion, view retirement, and transition candidate release clear wakeup.node so never-settling thenables do not retain disposed owners.

Docs (docs/signals.md, website) describe the thrown-thenable contract. Tests cover plain retry, redeclared definitions with switched thenables, async child readers, GC after dispose, and async work surviving replaced thrown waits.

Reviewed by Cursor Bugbot for commit d0bd925. Bugbot is set up for automated code reviews on this repo. Configure here.

…tles

A synchronous computation that throws a thenable is pending until it
settles. Settling it resolved the node's wakeup but never invalidated the
node, so every reader woken by that wait read the same clean pending state
with an already-settled waiting promise. An async derived$ awaiting it with
read() retried that wait on every microtask and froze the page.

A redeclaration reached the same state: when the view and the cell read the
same signals, sameViewResult kept the cell's pending read even though the
new definition threw a different thenable, so the cell kept waiting on the
replaced one (#1763).

- The thrown thenable's settlement now invalidates the node still waiting on
  it. A thenable already seen to settle waits for a dependency instead, so a
  lagging computation cannot evaluate again on every microtask.
- A node keeps its wakeup only while its state presents it, so an earlier
  thrown thenable cannot restart work that replaced it.
- sameViewResult also compares the thrown thenable, and promotion hands the
  view's wakeup to the cell.
- The wakeup holds its node only while the node waits; retirement, view
  retirement and candidate release drop it, so a thenable that never settles
  does not retain a disposed owner.

Fixes #1763

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Oct 5, 2026
@github-actions
github-actions Bot marked this pull request as draft October 5, 2026 13:13

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Promotion drops settled thenable invalidation
    • Propagated the view's Dirty flag into the promoted node's flags in promoteDeclarationView, so that when settleThrown invalidates the view and clears its wakeup before promotion, the node inherits the Dirty flag and re-evaluates instead of presenting a stale pending state with an already-settled waiting promise.

Create PR

Or push these changes by commenting:

@cursor push 197064e3fe
Preview (197064e3fe)
diff --git a/packages/octane/src/signals/graph.ts b/packages/octane/src/signals/graph.ts
--- a/packages/octane/src/signals/graph.ts
+++ b/packages/octane/src/signals/graph.ts
@@ -1194,7 +1194,9 @@
 	// A view nobody read has no state to install; the cell evaluates its new definition.
 	const unevaluated = state === undefined;
 	node.flags =
-		ReactiveFlags.Mutable | ReactiveFlags.Watching | (unevaluated ? ReactiveFlags.Dirty : 0);
+		ReactiveFlags.Mutable |
+		ReactiveFlags.Watching |
+		((unevaluated ? ReactiveFlags.Dirty : 0) | (view.flags & ReactiveFlags.Dirty));
 	// Committed consumers learn the accepted state first. The render's own
 	// consumers already presented it; move them without another notification.
 	if ((currentChanged || retainedChanged || unevaluated) && node.subs) {

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 515bdc1. Configure here.

Comment thread packages/octane/src/signals/graph.ts Resolved
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

🟢 No budget breaches, no added work, and no timing changes beyond ±3%.

Compares 4b46332e2, the merge commit's first parent with b3ac93f1c on the same runner. A byte value over its committed budget or any increase in a js-framework work counter fails this check. Wall time is a report: base and head samples alternate in one browser, and an operation is called slower or faster only when the 95% confidence interval of its ratio excludes ±3%. Workflow run

js-framework: 1k/10k row operations

All 20 timed operations (20 without a verdict)
target operation base ms head ms head/base 95% CI
octane-tsrx run 3.100 2.717 ±0.0% −6.7% … +0.0% ⚪ within noise
octane-tsrx replace 7.550 7.933 +2.3% −4.7% … +7.8% ⚪ within noise
octane-tsrx add 2.625 2.775 +1.7% ±0.0% … +4.2% ⚪ within noise
octane-tsrx update 0.132 0.134 +0.5% −4.6% … +2.6% ⚪ within noise
octane-tsrx select 0.005 0.005 −1.6% −3.1% … +1.7% ⚪ within noise
octane-tsrx swap 0.037 0.036 −0.4% −2.3% … +1.4% ⚪ within noise
octane-tsrx remove 0.021 0.021 −0.4% −3.8% … +1.4% ⚪ within noise
octane-tsrx runlots 27.650 26.325 +1.1% −7.8% … +3.3% ⚪ within noise
octane-tsrx select_lots 0.006 0.006 +1.3% −1.4% … +2.2% ⚪ within noise
octane-tsrx clear 43.792 43.758 −1.5% −3.2% … +0.8% ⚪ within noise
octane-jsx run 3.175 3.242 ±0.0% ±0.0% … +4.5% ⚪ within noise
octane-jsx replace 6.200 6.492 +0.0% −2.1% … +6.5% ⚪ within noise
octane-jsx add 3.050 3.017 ±0.0% −3.1% … +0.0% ⚪ within noise
octane-jsx update 0.243 0.240 +0.0% −0.7% … +1.3% ⚪ within noise
octane-jsx select 0.123 0.123 +0.6% ±0.0% … +1.2% ⚪ within noise
octane-jsx swap 0.233 0.225 +0.8% −1.1% … +1.9% ⚪ within noise
octane-jsx remove 0.180 0.172 −0.3% −4.4% … +1.7% ⚪ within noise
octane-jsx runlots 31.083 30.750 −2.5% −7.7% … +0.7% ⚪ within noise
octane-jsx select_lots 1.767 1.669 −1.4% −6.0% … +2.4% ⚪ within noise
octane-jsx clear 50.875 52.008 +4.2% +1.4% … +6.2% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

No changes across 60 measured values.

Public-import reachability bundles (bytes)

No changes across 105 measured values.

A declaration view invalidated after its render, such as by the thenable it
threw settling, still held the pending result of that render. Promotion
committed it and left the cell clean, so the cell could present a wait that
had already settled. The cell now takes the view's Dirty or Pending flags and
notifies its committed consumers, so it evaluates the accepted definition
instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm marked this pull request as ready for review October 6, 2026 11:23
@trueadm
trueadm merged commit 3fb1bec into main Oct 6, 2026
30 checks passed
@trueadm
trueadm deleted the fix/derived-redeclaration-waiter-spin branch October 6, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pending derived redeclaration can spin an async reader on a resolved waiter

1 participant