Skip to content

Suspended root-prop transitions make useSyncExternalStore miss updates to the visible selection #1341

Closed

Description

@jonkwheeler

A root-prop transition that changes a useSyncExternalStore selector and suspends leaves the old selection visible, but subsequent notifications consult the pending selector. The displayed selection can therefore stay stale until the suspended transition resolves.

Environment

  • Octane 0.6.0, commit 1bebd3b2f3cadd672a772d7f27c444a14e7a0fa5 (default branch fetched September 27, 2026).
  • macOS, Node 22.22.2, pnpm 11.15.1, Vitest 4.1.10, jsdom 30.0.1.
  • Client mount using octane({hmr:false}) (production compiler path); ordinary Vitest runtime. No hydration or production-browser-bundle claim.
  • Matched public-API React/ReactDOM 19.2.7 control tested in the same jsdom harness.

Reproduction

At the revision above, run pnpm install --frozen-lockfile.

Save packages/octane/tests/_fixtures/evidence-a8f2-store.tsrx (the state-driven component is retained from the passing control):

import { use, useState, useSyncExternalStore, useTransition } from 'octane';
type Field = 'a' | 'b';
export interface Store {
 get: () => Record<Field, number>;
 subscribe: (fn: () => void) => () => void;
}
interface Props {
 store: Store;
 pending: Promise<void>;
 tail: Promise<void>;
 bind: (select: (field: Field) => void) => void;
 observe: (field: Field) => void;
}
function Reader({ store, field, pending, tail, observe }: { store: Store; field: Field; pending: Promise<void>;
 tail: Promise<void>; observe: Props['observe'] }) @{
 const value = useSyncExternalStore(store.subscribe, () => { observe(field); return store.get()[field]; });
 if (field === 'b') { use(pending); use(tail); }
 <span id="value">{field + ':' + value}</span>
}
export function StoreTransition(props: Props) @{
 const [field, setField] = useState<Field>('a');
 const [isPending, start] = useTransition();
 props.bind((next) => start(() => setField(next)));
 <div>
  <span id="pending">{isPending ? 'pending' : 'idle'}</span>
  @try {
   <Reader store={props.store} field={field} pending={props.pending} tail={props.tail} observe={props.observe} />
  } @pending {
   <span id="fallback">Loading</span>
  }
 </div>
}

export function RootSelection(props: Omit<Props, 'bind'> & { field: Field }) @{
 @try {
  <Reader store={props.store} field={props.field} pending={props.pending} tail={props.tail} observe={props.observe} />
 } @pending {
  <span id="fallback">Loading</span>
 }
}

Save packages/octane/tests/evidence-a8f2-store.test.ts:

import { expect, it } from 'vitest';
import { mount, act } from './_helpers';
import { startTransition } from 'octane';
import { RootSelection } from './_fixtures/evidence-a8f2-store.tsrx';

it('keeps a root-prop selection live while its transition suspends', async () => {
 let current = { a: 0, b: 0 };
 const listeners = new Set<() => void>();
 const store = { get: () => current, subscribe: (fn: () => void) => { listeners.add(fn); return () => listeners.delete(fn); } };
 let resolve!: () => void;
 const pending = new Promise<void>(done => { resolve = done; });
 const reads: string[] = [];
 const props = { store, pending, tail: pending, observe: (field: 'a' | 'b') => reads.push(field) };
 const root = mount(RootSelection, { ...props, field: 'a' });
 try {
  await act(() => {});
  expect(root.find('#value').textContent).toBe('a:0');
  await act(() => startTransition(() => root.root.render(RootSelection, { ...props, field: 'b' })));
  expect(root.find('#value').textContent).toBe('a:0');
  reads.length = 0;
  await act(() => { current = { a: 1, b: 0 }; for (const listener of listeners) listener(); });
  console.log('root-prop after notify', JSON.stringify({ html: root.html(), reads }));
  expect.soft(root.find('#value').textContent).toBe('a:1');
  await act(() => resolve());
  expect(root.find('#value').textContent).toBe('b:0');
 } finally { root.unmount(); }
});

Save evidence-a8f2-store.config.mjs at the repository root:

import { defineConfig } from 'vitest/config';
import { octane } from './packages/octane/src/compiler/vite.js';
export default defineConfig({
 plugins: [octane({ hmr: false })],
 test: { cache: false, environment: 'jsdom', include: ['packages/octane/tests/evidence-a8f2-store.test.ts'], setupFiles: ['packages/octane/tests/_per-test-setup.ts'] },
});

Run:

pnpm exec vitest run --config evidence-a8f2-store.config.mjs --reporter=verbose --silent=false

Expected / actual

  1. Render field a from {a:0,b:0}: displays a:0.
  2. Call startTransition(() => root.render(RootSelection, {...props, field:'b'})). Field b suspends; a:0 remains visible.
  3. Replace the store with {a:1,b:0} and synchronously notify every subscriber.
  4. Expected: the retained view updates to a:1. Actual: it remains a:0.
  5. Resolve the promise: both Octane and React finish at b:0.

The Octane failure reproduced in 3/3 full-suite runs, then again with the isolated test above. An equivalent React test using root.render(createElement(...)), Suspense, useSyncExternalStore, use, and startTransition passed 2/2 runs: it updated the retained view to a:1. State-driven Octane transitions also passed, including resuspension after an initial promise resolves.

Contract and likely cause

The store contract promises updates for notifying stores. The documented optimization exception concerns a mutation without notification during render-to-commit; this repro explicitly notifies after the suspended render. The per-swap limitation describes early content/structural commits outside the held boundary; this failure loses a store update inside its retained content.

The likely cause is inst.getSnapshot = getSnapshot during render. When notified, checkStoreChanged compares the last committed 0 against pending selector b=0, suppressing the update even though the visible selector a now returns 1. Diagnostic callbacks confirm the notification reads b; the failing assertion is rendered text, not callback counts. No production patch was used to establish causality.

Impact: roots driven by external navigation/embedding props can display stale subscribed data while the new selection is loading. No last-good revision was established. Historical #962 concerns a different TanStack Router selection cache; open #1066 addresses subscription-time initialization and retains this render-time getter assignment.

Activity

  1. jonkwheeler commented on Oct 27, 2026

    @jonkwheeler
    ContributorAuthor

    Re-checked on current main (232464b399, after the runtime redesign in #2103): this still reproduces. After the store notifies during the held root transition, the screen stays at a:0 instead of a:1.

    There are two causes, and fixing only the first makes things worse.

    1. The selector write isn't rolled back. On an update render, useSyncExternalStore assigns inst.getSnapshot = getSnapshot on the shared store cell (packages/octane/src/runtime/engine.ts, around line 4628) without journaling it. When the root transition suspends into the @try hold, the cell keeps the pending selector (b). checkStoreChanged (src/runtime/effects.ts:513) then compares the committed value against b's value, sees no change, and drops the notification. Journaling the write fixes this step, and the notification check reads a again:

    if (inst.getSnapshot !== getSnapshot) {
    	journalRootProperty(inst, 'getSnapshot', inst.getSnapshot);
    	inst.getSnapshot = getSnapshot;
    }

    2. The re-render uses the held transition's props. A root.render transition folds no hook work, so it holds per boundary. Content outside the suspended boundary commits early, including the root component's props. The re-render the notification forces then reaches the consumer through renderVisibleTry with k: 'b'. It suspends on b and shows the @pending fallback. This is the limitation in packages/octane/audit/SUSPENSE_DIVERGENCE.md §4 ("Remaining limitation — transitions not driven by hook state").

    With only the first change applied, the user sees a loading flash instead of a stale value, so it shouldn't land alone. The second cause needs root requests to drop the whole root transaction when they suspend into a boundary hold, as hook-driven transitions already do since #2029. #2043 is closed, but packages/octane/audit/SUSPENSE_DIVERGENCE.md §4 still lists this limitation on main, so no open issue tracks this part of the fix yet.

    Regression test for whoever picks this up. It fails on main at the a:1 assertion:

    Fixture and test

    packages/octane/tests/_fixtures/external-store-behavior.tsrx:

    function HeldSelectionConsumer(props: {
    	store: ExternalStore<{ a: number; b: number }>;
    	k: 'a' | 'b';
    	pending: Promise<void>;
    }) @{
    	const value = useSyncExternalStore(props.store.subscribe, () => props.store.getState()[props.k]);
    	if (props.k === 'b') use(props.pending);
    	<output id="held-selection-value">{props.k + ':' + value}</output>
    }
    
    export function HeldSelectionBoundary(props: {
    	store: ExternalStore<{ a: number; b: number }>;
    	k: 'a' | 'b';
    	pending: Promise<void>;
    }) @{
    	@try {
    		<HeldSelectionConsumer store={props.store} k={props.k} pending={props.pending} />
    	} @pending {
    		<span id="held-selection-fallback">{'loading'}</span>
    	}
    }

    packages/octane/tests/external-store-behavior.test.ts:

    it('updates the held screen when its store notifies during a suspended root transition', async () => {
    	const store = makeStore({ a: 0, b: 0 });
    	const pending = deferred();
    	const r = mount(HeldSelectionBoundary, { store, k: 'a', pending: pending.promise });
    	flushEffects();
    	try {
    		await act(() =>
    			startTransition(() =>
    				r.root.render(HeldSelectionBoundary, { store, k: 'b', pending: pending.promise }),
    			),
    		);
    		expect(r.find('#held-selection-value').textContent).toBe('a:0');
    
    		// The held screen still selects `a`, so this notification must update it.
    		await act(() => store.setState({ a: 1, b: 0 }));
    		expect(r.find('#held-selection-value').textContent).toBe('a:1');
    
    		await act(() => pending.resolve());
    		expect(r.find('#held-selection-value').textContent).toBe('b:0');
    	} finally {
    		r.unmount();
    		flushEffects();
    	}
    });

    Agent-authored investigation.

  2. added a commit that references this issue on Nov 4, 2026
    70e0de0
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