Skip to content

Commit ae528b6

Browse files
committed
fix(octane): close $$stable bail holes for descendants and opaque children
Bubble suppressedUpdate and held-update checks to ancestors so a stamped parent cannot strand a child's store write, fail-closed on opaque local tags and scoped @{ } children, propagate bail-safety on a reverse-edge worklist, and arm $$compareInChain from value-position childSlot.
1 parent 28f386c commit ae528b6

4 files changed

Lines changed: 332 additions & 43 deletions

File tree

‎packages/octane/src/compiler/compile.js‎

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,7 +3974,7 @@ function collectFreeIdentifiers(root, initiallyBound, ignoreNodes = null) {
39743974

39753975
// Block scope — hoist `var`/`function` + pre-collect `let`/`const` so
39763976
// forward references work the same way they do at runtime.
3977-
if (t === 'BlockStatement') {
3977+
if (t === 'BlockStatement' || t === 'JSXCodeBlock') {
39783978
const newScope = new Set(scope);
39793979
for (const stmt of n.body || []) {
39803980
if (stmt.type === 'VariableDeclaration') {
@@ -3984,6 +3984,7 @@ function collectFreeIdentifiers(root, initiallyBound, ignoreNodes = null) {
39843984
}
39853985
}
39863986
walk(n.body, newScope);
3987+
if (t === 'JSXCodeBlock') walk(n.render, newScope);
39873988
return;
39883989
}
39893990

@@ -6687,13 +6688,13 @@ function containsAutoMemoUnsafeStructure(
66876688
if (containsDeferredRefRead(n)) found = true;
66886689
return;
66896690
}
6690-
// An `@{ }` scoped child lowers to a nested function value invoked by the
6691-
// hole machinery — the same deferred shape as the explicit `() => @{ }`
6692-
// spelling — so `$$stable` classifies its internals as child-render work
6693-
// rather than this component's render. Non-stable callsites keep walking
6694-
// it: their region guard already skips the child with everything else.
6695-
if (stable && t === 'JSXCodeBlock') {
6696-
if (containsDeferredRefRead(n)) found = true;
6691+
// An `@{ }` scoped child is invoked by the hole machinery on every parent
6692+
// render. A bailed parent keeps the last hole output, so the nested body
6693+
// is render-position work for `$$stable` — walk it rather than treating
6694+
// it as a deferred callback. The explicit `() => @{ }` spelling stays
6695+
// deferred through the function-value arm above.
6696+
if (t === 'JSXCodeBlock' && containsDeferredRefRead(n)) {
6697+
found = true;
66976698
return;
66986699
}
66996700
if (
@@ -6882,6 +6883,24 @@ function containsAutoMemoUnsafeStructure(
68826883
return;
68836884
}
68846885
const name = tag?.name;
6886+
// A component tag bound from props or another local (`const C =
6887+
// props.comp`) is the same opacity as an import: it is not a free
6888+
// name, so the admission pass never sees it, and a stamp would keep
6889+
// an unproven callee. Fail closed unless the identifier is a
6890+
// same-module component, proven context, or memo() wall.
6891+
if (
6892+
stable &&
6893+
(tag?.type === 'Identifier' || tag?.type === 'JSXIdentifier') &&
6894+
typeof name === 'string' &&
6895+
!/^[a-z]/.test(name) &&
6896+
!name.includes('-') &&
6897+
!ctx?.componentInfo?.has(name) &&
6898+
!ctx?.defaultMemoBindings?.has(name) &&
6899+
!ctx?.provenContextBindings?.has(name)
6900+
) {
6901+
found = true;
6902+
return;
6903+
}
68856904
if (
68866905
name === 'Suspense' ||
68876906
name === 'ErrorBoundary' ||
@@ -6930,10 +6949,7 @@ function collectStableDeferredFunctions(stmts, ctx) {
69306949
if (
69316950
t === 'ArrowFunctionExpression' ||
69326951
t === 'FunctionExpression' ||
6933-
t === 'FunctionDeclaration' ||
6934-
// A nested `@{ }` scoped child is a deferred function value too — the
6935-
// lowering emits it as a sibling `__tsrx$N` passed to the hole machinery.
6936-
t === 'JSXCodeBlock'
6952+
t === 'FunctionDeclaration'
69376953
) {
69386954
deferred.add(n);
69396955
return;
@@ -10895,7 +10911,7 @@ function compileInternal(
1089510911
info.stableBody = stableBody;
1089610912
info.bailSafe = bailSafeBody;
1089710913
info.stableComponentDeps = bailSafeBody ? [...new Set(stableComponentDeps)].sort() : [];
10898-
info.stable = false; // stamped only after the bailSafe fixed point below
10914+
info.stable = false; // stamped only after bailSafe reverse-edge propagation
1089910915
// Hookless check.
1090010916
// Return-JSX functions reconcile their returned descriptor through
1090110917
// renderBlock. componentSlotLite intentionally ignores return values, so
@@ -11036,23 +11052,37 @@ function compileInternal(
1103611052
// `$$stable` bail-safety is transitive over same-module component edges: a
1103711053
// bailed parent keeps the child's committed subtree, which is only equivalent
1103811054
// to re-rendering when the child is itself bail-safe — pure modulo
11039-
// runtime-tracked context reads. Iterate to a fixed point so declaration
11040-
// order cannot matter and recursive components fail closed, then stamp the
11041-
// strict bodies whose deps are all bail-safe.
11042-
let bailSafeChanged = true;
11043-
while (bailSafeChanged) {
11044-
bailSafeChanged = false;
11045-
for (const [, info] of ctx.componentInfo) {
11046-
if (!info.bailSafe) continue;
11047-
for (const name of info.stableComponentDeps) {
11048-
if (ctx.componentInfo.get(name)?.bailSafe !== true) {
11049-
info.bailSafe = false;
11050-
bailSafeChanged = true;
11051-
break;
11052-
}
11055+
// runtime-tracked context reads. Work flows from an unsafe callee to its
11056+
// dependents so declaration order cannot turn an A → B → C chain into one
11057+
// whole-map scan per edge, and already-unsafe seeds are not re-propagated.
11058+
const bailSafeDependents = new Map();
11059+
const bailSafeQueue = [];
11060+
for (const [name, info] of ctx.componentInfo) {
11061+
if (!info.bailSafe) {
11062+
bailSafeQueue.push(name);
11063+
continue;
11064+
}
11065+
for (const dependency of info.stableComponentDeps) {
11066+
let dependents = bailSafeDependents.get(dependency);
11067+
if (dependents === undefined) {
11068+
bailSafeDependents.set(dependency, (dependents = []));
11069+
}
11070+
dependents.push(name);
11071+
if (ctx.componentInfo.get(dependency)?.bailSafe !== true && info.bailSafe) {
11072+
info.bailSafe = false;
11073+
bailSafeQueue.push(name);
1105311074
}
1105411075
}
1105511076
}
11077+
for (let index = 0; index < bailSafeQueue.length; index++) {
11078+
const name = bailSafeQueue[index];
11079+
for (const dependent of bailSafeDependents.get(name) ?? []) {
11080+
const info = ctx.componentInfo.get(dependent);
11081+
if (!info?.bailSafe) continue;
11082+
info.bailSafe = false;
11083+
bailSafeQueue.push(dependent);
11084+
}
11085+
}
1105611086
for (const [, info] of ctx.componentInfo) {
1105711087
info.stable =
1105811088
info.stableBody === true &&

‎packages/octane/src/runtime.ts‎

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2744,10 +2744,11 @@ function hasHeldTransitionUpdate(slot: TransitionActionSlot<unknown>, block: Blo
27442744
}
27452745

27462746
/**
2747-
* True when a transition/urgent update targeted at this block was rolled back
2748-
* and is still waiting to re-apply. Held entries re-apply inside the state hook
2749-
* during render, so a memo/stable bail that skips the body would strand the
2750-
* pending value while keeping the rolled-back DOM — the bail must decline.
2747+
* True when a transition/urgent update targeted at this block — or a descendant
2748+
* that owns the state cells — was rolled back and is still waiting to re-apply.
2749+
* Held entries re-apply inside the child's state hook during render, so a
2750+
* memo/stable ancestor bail that skips the body would strand the pending value
2751+
* while keeping the rolled-back DOM — the bail must decline.
27512752
*/
27522753
function blockHasHeldUpdate(block: Block): boolean {
27532754
const rootHeld = block.idState.renderOwner?.transition;
@@ -2761,7 +2762,7 @@ function blockHasHeldUpdate(block: Block): boolean {
27612762
if (entries === undefined) return false;
27622763
for (const entry of entries) {
27632764
if (
2764-
entry.block === block &&
2765+
(entry.block === block || blockIsAncestorOf(block, entry.block)) &&
27652766
!entry.superseded &&
27662767
Object.is(entry.slot.value, entry.baseValue)
27672768
)
@@ -2776,6 +2777,20 @@ function blockHasHeldUpdate(block: Block): boolean {
27762777
return false;
27772778
}
27782779

2780+
/**
2781+
* A queued update dropped by an aborted transaction must be consumed by a
2782+
* later body re-run. The store write lives on this block, but a $$stable /
2783+
* memo ancestor with clean props would otherwise keep the committed subtree
2784+
* and never re-enter here — mark the whole ancestor chain so those bails
2785+
* decline. Cleared per block at the top of renderBlockInner.
2786+
*/
2787+
function markSuppressedUpdate(block: Block): void {
2788+
for (let b: Block | null = block; b !== null; b = b.parentBlock) {
2789+
if (b.suppressedUpdate === true) break;
2790+
b.suppressedUpdate = true;
2791+
}
2792+
}
2793+
27792794
function cancelHeldTransitionUpdate(slot: TransitionActionSlot<unknown>, block: Block): boolean {
27802795
if (!urgentTransitionCellUpdate(block)) return false;
27812796
const rootHeld = block.idState.renderOwner?.transition;
@@ -6786,9 +6801,10 @@ function drainQueue(): { err: any } | null {
67866801
// Once one suspends, later siblings must not publish a partial screen.
67876802
const rootTransaction = block.idState.renderOwner?.transaction;
67886803
if (rootTransaction?.aborted === true) {
6789-
// The update is deferred to the resolve pass — flag it so a memo/
6790-
// stable/implicit bail cannot skip the body that consumes it.
6791-
block.suppressedUpdate = true;
6804+
// The update is deferred to the resolve pass — flag this block and its
6805+
// ancestors so a memo/stable/implicit bail cannot skip the body that
6806+
// would re-enter the child and consume the store write.
6807+
markSuppressedUpdate(block);
67926808
continue;
67936809
}
67946810
const retired = rootTransaction?.retired;
@@ -6798,7 +6814,10 @@ function drainQueue(): { err: any } | null {
67986814
// roots with an actual deferred deletion pay this ancestor walk.
67996815
let current: Block | null = block;
68006816
while (current !== null && !retired.has(current)) current = current.parentBlock;
6801-
if (current !== null) continue;
6817+
if (current !== null) {
6818+
markSuppressedUpdate(block);
6819+
continue;
6820+
}
68026821
}
68036822
const crossRenderUpdate = block.crossRenderUpdate;
68046823
block.crossRenderUpdate = false;
@@ -6850,10 +6869,16 @@ function drainQueue(): { err: any } | null {
68506869
// retries the render; if it no longer suspends (an external store flipped
68516870
// before the suspending promise resolved), the boundary reveals now.
68526871
if (hiddenTry !== null) {
6872+
// The standalone body is dropped in favor of a boundary retry. Flag
6873+
// this block and its ancestors: the retry (or a later equal-props
6874+
// parent update) must not memo/stable/implicit-bail past the child
6875+
// that still holds the store write.
6876+
markSuppressedUpdate(block);
68536877
visibilityDriver!.reveal(hiddenTry, block.pendingMode ?? 'urgent');
68546878
continue;
68556879
}
68566880
if (hiddenActivity !== null && hiddenActivity.pendingThenable !== null) {
6881+
markSuppressedUpdate(block);
68576882
visibilityDriver!.retryActivity(hiddenActivity, true);
68586883
continue;
68596884
}
@@ -29766,12 +29791,7 @@ function componentSlotImpl(
2976629791
// ancestor chain so those bails decline. `LAZY_BODY_CHECK` marks eagerly —
2976729792
// the lazy wrapper's resolution-aware `__compare` is only installed once the
2976829793
// module resolves, after ancestors may already have committed.
29769-
if ((body as any).__compare !== undefined || (body as any)[LAZY_BODY_CHECK] !== undefined) {
29770-
for (let b: Block | null = parentBlock; b !== null; b = b.parentBlock) {
29771-
if (b.$$compareInChain === true) break;
29772-
b.$$compareInChain = true;
29773-
}
29774-
}
29794+
markCompareInChain(parentBlock, body);
2977529795
let state = parentScope.slots[slotKey] as CompSlot | undefined;
2977629796
let hydrationCursor: Node | null = null;
2977729797
if (state === undefined) {
@@ -34046,6 +34066,10 @@ export function childSlot(
3404634066
componentHasKey = value.key != null;
3404734067
}
3404834068
if (comp !== null) {
34069+
// Same veto-compare taint as componentSlot: a value-position memo/lazy
34070+
// child must still flag ancestors, or a later $$stable/memo bail keeps
34071+
// this subtree without invoking `__compare`.
34072+
markCompareInChain(parentBlock, comp);
3404934073
// A bare render-FUNCTION child (a `.tsrx` `{children}` body forwarded onto a `.ts`
3405034074
// component's host element via createElement) is re-created every render, so its
3405134075
// identity always differs — but it is the SAME slot child. Reconcile by SLOT like
@@ -34741,6 +34765,20 @@ function refreshContextConsumers(block: Block): void {
3474134765
}
3474234766
}
3474334767

34768+
/**
34769+
* Flag ancestors when a callee's compare can veto on non-prop grounds (lazy
34770+
* re-resolution, custom comparators). Shared by componentSlot and childSlot so
34771+
* a value-position `{<Wrapped />}` still taints the chain. Sticky: a stale
34772+
* flag only costs a missed bail.
34773+
*/
34774+
function markCompareInChain(parentBlock: Block | null, body: any): void {
34775+
if ((body as any).__compare === undefined && (body as any)[LAZY_BODY_CHECK] === undefined) return;
34776+
for (let b: Block | null = parentBlock; b !== null; b = b.parentBlock) {
34777+
if (b.$$compareInChain === true) break;
34778+
b.$$compareInChain = true;
34779+
}
34780+
}
34781+
3474434782
/**
3474534783
* The hazards that veto every props-equality bail (memo, $$stable, implicit):
3474634784
* each names a reason the body must re-run regardless of prop equality. A new

‎packages/octane/tests/compiler/stable-marker.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,69 @@ describe('$$stable definition-site marker emission', () => {
135135
expect(stableStampedNames(code).has('Stable')).toBe(false);
136136
});
137137

138+
it('rejects a local tag aliased from props — the callee is as opaque as an import', () => {
139+
const code = c(`
140+
export function Stable(props) @{
141+
const C = props.comp;
142+
<C label={props.label} />
143+
}
144+
`);
145+
expect(stableStampedNames(code).has('Stable')).toBe(false);
146+
});
147+
148+
it('rejects a scoped child that reads unwitnessed module state', () => {
149+
const code = c(`
150+
let serial = 0;
151+
export function Stable() @{
152+
<div>
153+
@{
154+
const n = serial;
155+
<span>{'' + n}</span>
156+
}
157+
</div>
158+
}
159+
`);
160+
expect(stableStampedNames(code).has('Stable')).toBe(false);
161+
});
162+
163+
it('rejects a scoped child that calls useFormStatus', () => {
164+
const code = c(`
165+
import { useFormStatus } from 'octane';
166+
export function Stable() @{
167+
<form>
168+
@{
169+
const status = useFormStatus();
170+
<span>{status.pending ? 'busy' : 'idle'}</span>
171+
}
172+
</form>
173+
}
174+
`);
175+
expect(stableStampedNames(code).has('Stable')).toBe(false);
176+
});
177+
178+
it('does not stamp a long same-module chain whose leaf is not bail-safe', () => {
179+
const code = c(`
180+
export function A(props) @{
181+
<section><B label={props.label} /></section>
182+
}
183+
function B(props) @{
184+
<div><C label={props.label} /></div>
185+
}
186+
function C(props) @{
187+
<span><D label={props.label} /></span>
188+
}
189+
function D(props) @{
190+
const stamped = props.stamp();
191+
<em>{stamped as string}</em>
192+
}
193+
`);
194+
const stamped = stableStampedNames(code);
195+
expect(stamped.has('D')).toBe(false);
196+
expect(stamped.has('C')).toBe(false);
197+
expect(stamped.has('B')).toBe(false);
198+
expect(stamped.has('A')).toBe(false);
199+
});
200+
138201
it('rejects a render-time useContext read', () => {
139202
const code = c(`
140203
import { createContext, useContext } from 'octane';

0 commit comments

Comments
 (0)