Skip to content

fix(octane): release a discarded portal's target delegation once - #1895

Merged
trueadm merged 10 commits into
mainfrom
fix/portal-rollback-delegation-refcount
Oct 8, 2026
Merged

trueadm merged 10 commits into
mainfrom
fix/portal-rollback-delegation-refcount

Conversation

@trueadm

@trueadm trueadm commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Portals that share a target also share its delegated event listeners. registerDelegationTarget refcounts the target, and its listeners detach when the count reaches zero.
  • A root render that rolls back released a portal it had created twice. For example, a sibling suspends with no Suspense boundary. renderPortalState's creation undo released the portal once. The owner's teardown released it again, through unmountSlot's portalSlotSlot branch or childSlot's teardownPortalState. A committed portal in the same target then lost its listeners, and its handlers (onClick on a portal button, say) stopped firing.
  • All three release sites now go through releasePortalTarget, which nulls the slot's target as it releases. Whichever path runs first releases the portal, and the other one is a no-op. teardownPortalState already used target === null to mean "released". The creation undo and unmountSlot now do too.
  • Found while proving perf(octane): skip journal entries for Blocks a root render discards with their parent #1892. That PR doesn't change this behavior, and this one doesn't touch the functions perf(octane): skip journal entries for Blocks a root render discards with their parent #1892 changes.

Why

The two release orders that a rollback reaches on main:

  1. Creation undo, then owner teardown. The portal is created under a new owner (a component with a host-child createPortal, a component returning createPortal(...), or a new @if/@switch arm), or by a committed value hole that switches to a portal. Rollback runs the creation undo, then JOURNAL_CREATED unmounts the owner, which releases the portal again.
  2. Owner teardown, then creation undo. The same root render creates the portal and then removes it. A new component's render-phase update swaps its arm out, and that arm is unmounted immediately because it is new. The root then suspends, and rollback runs the creation undo again.

registerSlot's undo for a mounted scope would give the second order too. On main, a scope is not mounted during its own first render, so this order needs a mounted scope that reaches portal() for the first time. The change makes that order safe as well.

Changes

  • packages/octane/src/runtime.ts: add releasePortalTarget(state). Use it in unmountSlot's portalSlotSlot branch, in the renderPortalState creation undo, and in teardownPortalState.
  • packages/octane/tests/portal-rollback-delegation.test.ts and _fixtures/portal-rollback-delegation.tsrx: a committed portal button shares a target with a portal that a discarded root render created. Each test clicks the button after the held update, again after the retry commits, and again after the second portal is removed. Each test then checks that the target is empty after unmount.
  • .changeset/portal-rollback-delegation.md: octane patch.
  • benchmarks/bundle-size/{app,jsx,minimal}-budgets.json: record this change's savings (67 lowerings, no raises), as CI's ratchet requires.

Validation

  • pnpm format:check (not run repo-wide; pnpm format:files:check on the four changed files passes)
  • pnpm typecheck (not run repo-wide; pnpm typecheck:files on runtime.ts plus the new test and fixture passes)
  • pnpm test (left to CI, per the local test-load rule)
  • targeted tests: vitest run --project octane --project octane-prod packages/octane/tests/portal-rollback-delegation.test.ts → 14/14 pass.
    • On main (d1e308bb0d), 12/14 fail at the first click after the held update. That covers four shapes from order 1 and two from order 2, in both projects. The two that pass are the controls, where the new portal's content suspends and only the creation undo releases it.
    • With only the creation-undo site reverted, the four order-1 shapes fail.
    • With only unmountSlot's branch reverted, the two order-2 shapes fail.
  • pnpm sync: no generated changes.
  • perf-review: see below.

Perf review: fix/portal-rollback-delegation-refcount (origin/main..HEAD)

Scan: node scripts/perf-review-scan.mjs → 0 candidates.
Hot paths touched:

  • unmountSlot runs once per registered slot on unmount. Only its portalSlotSlot branch changed, and that branch now makes one monomorphic call per portal slot instead of two inline calls.
  • renderPortalState creates one undo closure per created portal inside a root transaction, as before. The closure now captures one variable instead of two.
# Severity Location Dimension Finding Required evidence Provided?
1 note packages/octane/src/runtime.ts:32844 shapes releasePortalTarget writes null to PortalSlot.target. teardownPortalState already wrote null there on main, so the field's representation doesn't change, and PortalSlot gains no new key. none n/a
2 note packages/octane/src/runtime.ts:32844 bundle Raw bytes shrink in every bundle (bindings framework 339,780 → 339,757), but gzip does not track raw. The first helper form (an early return for a null target) put the bindings framework's gzip at 108,933, 1 byte over its 108,932 budget. The helper now repeats teardownPortalState's original release block verbatim, which measures 108,929. On main e14fbdf, raw drops 13 to 23 bytes per bundle, and the bindings framework is 341,232 raw / 109,384 gzip after this change. CI bundle rows yes, CI bench report

Dismissed candidates: none.
Not checked: render or commit timing. No allocation, DOM work, or scheduling changed on a per-render path.

Risk / follow-ups

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
Touches portal unmount and root-render rollback teardown in the runtime; behavior is narrow but event delegation is user-visible and the fix spans multiple teardown orderings.

Overview
Fixes broken onClick (and other delegated handlers) on portals that stay mounted when a discarded root render briefly added another portal to the same DOM target.

Shared portal targets use a refcounted delegation listener setup. Rollback could unregister twice for one ephemeral portal—via creation undo and owner teardown—dropping listeners while a committed portal still needed them. All release paths now go through releasePortalTarget, which clears state.target after the first release so the second path is a no-op.

Adds regression tests for sibling suspend-without-boundary rollback across host-child, returned-value, branch, and ternary portal shapes, plus suspended portal content and same-render create/remove cases.

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

trueadm and others added 2 commits October 8, 2026 14:04
A root render that rolls back reached a portal it created twice: through
renderPortalState's creation undo and through the owner's teardown
(unmountSlot's portalSlotSlot branch, or childSlot's teardownPortalState).
unregisterDelegationTarget is a refcount, so a committed portal sharing the
target lost its delegated listeners and its handlers stopped firing.

Route all three release sites through releasePortalTarget, which nulls the
slot's target as it releases, so whichever path runs first releases and the
other is a no-op.

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 8, 2026
@trueadm
trueadm marked this pull request as ready for review October 8, 2026 13:07
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark report

  • 🔴 bundle-size: 15 value(s) increased within budget
  • 🔴 bundle-reachability: 23 value(s) increased within budget

Compares e14fbdf9a, the merge commit's first parent with 57801588e 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 2.958 2.783 −2.7% −6.1% … ±0.0% ⚪ within noise
octane-tsrx replace 6.900 6.992 ±0.0% −3.3% … +2.6% ⚪ within noise
octane-tsrx add 2.725 2.667 ±0.0% −3.4% … +1.8% ⚪ within noise
octane-tsrx update 0.147 0.151 +0.3% −2.8% … +2.0% ⚪ within noise
octane-tsrx select 0.006 0.006 +0.5% +0.3% … +1.1% ⚪ within noise
octane-tsrx swap 0.035 0.035 +0.3% −1.7% … +1.2% ⚪ within noise
octane-tsrx remove 0.017 0.018 +0.7% −1.9% … +2.4% ⚪ within noise
octane-tsrx runlots 27.108 26.675 −0.4% −4.5% … +2.6% ⚪ within noise
octane-tsrx select_lots 0.007 0.007 +0.7% ±0.0% … +1.7% ⚪ within noise
octane-tsrx clear 49.575 50.783 +0.7% −1.7% … +4.0% ⚪ within noise
octane-jsx run 3.650 3.642 ±0.0% −4.0% … +2.9% ⚪ within noise
octane-jsx replace 6.650 7.250 +1.5% −4.4% … +7.8% ⚪ within noise
octane-jsx add 3.592 3.592 −2.7% −3.8% … ±0.0% ⚪ within noise
octane-jsx update 0.276 0.271 −0.0% −2.4% … +0.7% ⚪ within noise
octane-jsx select 0.138 0.138 −1.0% −1.6% … ±0.0% ⚪ within noise
octane-jsx swap 0.240 0.237 −0.0% −1.2% … +0.0% ⚪ within noise
octane-jsx remove 0.184 0.181 −0.6% −1.2% … +0.3% ⚪ within noise
octane-jsx runlots 32.183 31.167 −0.7% −3.8% … +1.3% ⚪ within noise
octane-jsx select_lots 1.649 1.642 ±0.0% −0.9% … +1.2% ⚪ within noise
octane-jsx clear 52.883 53.358 +2.3% +1.1% … +3.3% ⚪ within noise

All 64 work counters are unchanged.

Application bundle size (bytes)

target metric base head Δ
octane-tsrx js_raw 113,177 113,164 −13 (−0.0%) 🟢
octane-tsrx js_gzip 36,909 36,912 +3 (+0.0%) 🔴
octane-tsrx js_brotli 32,693 32,660 −33 (−0.1%) 🟢
octane-tsrx app_brotli 2,041 2,031 −10 (−0.5%) 🟢
octane-tsrx fw_raw 106,231 106,218 −13 (−0.0%) 🟢
octane-tsrx fw_gzip 34,573 34,576 +3 (+0.0%) 🔴
octane-tsrx fw_brotli 30,652 30,629 −23 (−0.1%) 🟢
octane-tsrx todo_js_raw 121,562 121,549 −13 (−0.0%) 🟢
octane-tsrx todo_js_gzip 39,708 39,718 +10 (+0.0%) 🔴
octane-tsrx todo_js_brotli 34,962 34,951 −11 (−0.0%) 🟢
octane-tsrx todo_app_gzip 2,220 2,221 +1 (+0.0%) 🔴
octane-tsrx todo_app_brotli 1,880 1,891 +11 (+0.6%) 🔴
octane-tsrx todo_fw_raw 116,944 116,931 −13 (−0.0%) 🟢
octane-tsrx todo_fw_gzip 37,488 37,497 +9 (+0.0%) 🔴
octane-tsrx todo_fw_brotli 33,082 33,060 −22 (−0.1%) 🟢
octane-tsrx chat_js_raw 120,311 120,298 −13 (−0.0%) 🟢
octane-tsrx chat_js_gzip 39,531 39,534 +3 (+0.0%) 🔴
octane-tsrx chat_js_brotli 34,897 34,860 −37 (−0.1%) 🟢
octane-tsrx chat_app_brotli 2,388 2,389 +1 (+0.0%) 🔴
octane-tsrx chat_fw_raw 114,546 114,533 −13 (−0.0%) 🟢
octane-tsrx chat_fw_gzip 36,815 36,818 +3 (+0.0%) 🔴
octane-tsrx chat_fw_brotli 32,509 32,471 −38 (−0.1%) 🟢
octane-tsrx weather_js_raw 156,865 156,852 −13 (−0.0%) 🟢
octane-tsrx weather_js_gzip 50,928 50,913 −15 (−0.0%) 🟢
octane-tsrx weather_js_brotli 44,455 44,451 −4 (−0.0%) 🟢
octane-tsrx weather_app_gzip 7,872 7,871 −1 (−0.0%) 🟢
octane-tsrx weather_app_brotli 6,659 6,660 +1 (+0.0%) 🔴
octane-tsrx weather_fw_raw 134,711 134,698 −13 (−0.0%) 🟢
octane-tsrx weather_fw_gzip 43,056 43,042 −14 (−0.0%) 🟢
octane-tsrx weather_fw_brotli 37,796 37,791 −5 (−0.0%) 🟢
octane-tsrx bindings_js_raw 688,861 688,838 −23 (−0.0%) 🟢
octane-tsrx bindings_js_gzip 225,037 225,018 −19 (−0.0%) 🟢
octane-tsrx bindings_js_brotli 191,253 191,303 +50 (+0.0%) 🔴
octane-tsrx bindings_app_brotli 96,224 96,258 +34 (+0.0%) 🔴
octane-tsrx bindings_fw_raw 341,223 341,200 −23 (−0.0%) 🟢
octane-tsrx bindings_fw_gzip 109,371 109,352 −19 (−0.0%) 🟢
octane-tsrx bindings_fw_brotli 95,029 95,045 +16 (+0.0%) 🔴
octane-tsrx full_fw_raw 568,692 568,669 −23 (−0.0%) 🟢
octane-tsrx full_fw_gzip 171,259 171,251 −8 (−0.0%) 🟢
octane-tsrx full_fw_brotli 144,030 144,091 +61 (+0.0%) 🔴
octane-jsx js_raw 187,752 187,729 −23 (−0.0%) 🟢
octane-jsx js_gzip 59,567 59,541 −26 (−0.0%) 🟢
octane-jsx js_brotli 51,791 51,754 −37 (−0.1%) 🟢
octane-jsx app_gzip 2,858 2,856 −2 (−0.1%) 🟢
octane-jsx app_brotli 2,409 2,403 −6 (−0.2%) 🟢
octane-jsx fw_raw 179,487 179,464 −23 (−0.0%) 🟢
octane-jsx fw_gzip 56,709 56,685 −24 (−0.0%) 🟢
octane-jsx fw_brotli 49,382 49,351 −31 (−0.1%) 🟢
octane-jsx full_fw_raw 568,692 568,669 −23 (−0.0%) 🟢
octane-jsx full_fw_gzip 171,259 171,251 −8 (−0.0%) 🟢
octane-jsx full_fw_brotli 144,030 144,091 +61 (+0.0%) 🔴

9 other measured values are unchanged.

Public-import reachability bundles (bytes)

target metric base head Δ
cli-spa-starter raw 102,227 102,212 −15 (−0.0%) 🟢
cli-spa-starter gzip 34,024 34,010 −14 (−0.0%) 🟢
cli-spa-starter brotli 29,881 29,877 −4 (−0.0%) 🟢
root-static-specialized raw 72,738 72,725 −13 (−0.0%) 🟢
root-static-specialized gzip 23,852 23,854 +2 (+0.0%) 🔴
root-static-specialized brotli 21,305 21,315 +10 (+0.0%) 🔴
root-chained-jsx raw 74,616 74,603 −13 (−0.0%) 🟢
root-chained-jsx gzip 24,354 24,359 +5 (+0.0%) 🔴
root-chained-jsx brotli 21,755 21,842 +87 (+0.4%) 🔴
root-static raw 165,569 165,546 −23 (−0.0%) 🟢
root-static gzip 52,321 52,312 −9 (−0.0%) 🟢
root-static brotli 45,602 45,521 −81 (−0.2%) 🟢
root-static-local raw 72,737 72,724 −13 (−0.0%) 🟢
root-static-local gzip 23,839 23,840 +1 (+0.0%) 🔴
root-static-local brotli 21,308 21,310 +2 (+0.0%) 🔴
root-callback-local raw 74,642 74,629 −13 (−0.0%) 🟢
root-callback-local gzip 24,358 24,363 +5 (+0.0%) 🔴
root-callback-local brotli 21,774 21,771 −3 (−0.0%) 🟢
hooks-state raw 84,459 84,446 −13 (−0.0%) 🟢
hooks-state gzip 27,810 27,817 +7 (+0.0%) 🔴
hooks-state brotli 24,803 24,774 −29 (−0.1%) 🟢
prop-attributes raw 84,713 84,700 −13 (−0.0%) 🟢
prop-attributes gzip 27,808 27,814 +6 (+0.0%) 🔴
prop-attributes brotli 24,781 24,801 +20 (+0.1%) 🔴
context raw 96,644 96,631 −13 (−0.0%) 🟢
context gzip 31,508 31,501 −7 (−0.0%) 🟢
context brotli 27,966 27,978 +12 (+0.0%) 🔴
hydrate-root raw 138,113 138,100 −13 (−0.0%) 🟢
hydrate-root gzip 44,218 44,203 −15 (−0.0%) 🟢
hydrate-root brotli 38,678 38,670 −8 (−0.0%) 🟢
deferred-hydration raw 221,381 221,375 −6 (−0.0%) 🟢
deferred-hydration gzip 69,726 69,736 +10 (+0.0%) 🔴
deferred-hydration brotli 60,155 60,189 +34 (+0.1%) 🔴
hydrate-frame raw 366,086 366,063 −23 (−0.0%) 🟢
hydrate-frame gzip 113,539 113,535 −4 (−0.0%) 🟢
hydrate-frame brotli 97,065 97,035 −30 (−0.0%) 🟢
suspense-transition raw 141,573 141,560 −13 (−0.0%) 🟢
suspense-transition gzip 44,764 44,757 −7 (−0.0%) 🟢
suspense-transition brotli 39,289 39,264 −25 (−0.1%) 🟢
component-owned-effects raw 77,101 77,088 −13 (−0.0%) 🟢
component-owned-effects gzip 25,196 25,203 +7 (+0.0%) 🔴
component-owned-effects brotli 22,488 22,535 +47 (+0.2%) 🔴
binding-hooks raw 82,959 82,945 −14 (−0.0%) 🟢
binding-hooks gzip 27,452 27,449 −3 (−0.0%) 🟢
binding-hooks brotli 24,520 24,522 +2 (+0.0%) 🔴
binding-base-ui-vite raw 199,772 199,749 −23 (−0.0%) 🟢
binding-base-ui-vite gzip 62,787 62,770 −17 (−0.0%) 🟢
binding-base-ui-vite brotli 54,639 54,553 −86 (−0.2%) 🟢
binding-base-ui-esbuild raw 215,303 215,280 −23 (−0.0%) 🟢
binding-base-ui-esbuild gzip 69,754 69,741 −13 (−0.0%) 🟢
binding-base-ui-esbuild brotli 60,516 60,529 +13 (+0.0%) 🔴
binding-radix-vite raw 201,807 201,784 −23 (−0.0%) 🟢
binding-radix-vite gzip 63,396 63,376 −20 (−0.0%) 🟢
binding-radix-vite brotli 55,115 55,093 −22 (−0.0%) 🟢
binding-radix-esbuild raw 217,127 217,104 −23 (−0.0%) 🟢
binding-radix-esbuild gzip 70,304 70,270 −34 (−0.0%) 🟢
binding-radix-esbuild brotli 61,085 60,984 −101 (−0.2%) 🟢
binding-mantine-hooks-vite raw 86,516 86,503 −13 (−0.0%) 🟢
binding-mantine-hooks-vite brotli 25,312 25,325 +13 (+0.1%) 🔴
binding-mantine-hooks-esbuild raw 90,889 90,876 −13 (−0.0%) 🟢
binding-mantine-hooks-esbuild gzip 30,541 30,551 +10 (+0.0%) 🔴
binding-mantine-hooks-esbuild brotli 27,213 27,244 +31 (+0.1%) 🔴
binding-apollo-client-vite raw 342,224 342,201 −23 (−0.0%) 🟢
binding-apollo-client-vite gzip 105,071 105,065 −6 (−0.0%) 🟢
binding-apollo-client-vite brotli 91,758 91,631 −127 (−0.1%) 🟢
binding-apollo-client-esbuild raw 365,446 365,423 −23 (−0.0%) 🟢
binding-apollo-client-esbuild gzip 116,092 116,070 −22 (−0.0%) 🟢
binding-apollo-client-esbuild brotli 101,250 101,190 −60 (−0.1%) 🟢
binding-usehooks-ts-vite raw 87,286 87,273 −13 (−0.0%) 🟢
binding-usehooks-ts-vite gzip 28,637 28,640 +3 (+0.0%) 🔴
binding-usehooks-ts-vite brotli 25,555 25,567 +12 (+0.0%) 🔴
binding-usehooks-ts-esbuild raw 91,657 91,644 −13 (−0.0%) 🟢
binding-usehooks-ts-esbuild gzip 30,785 30,784 −1 (−0.0%) 🟢
binding-usehooks-ts-esbuild brotli 27,433 27,449 +16 (+0.1%) 🔴

37 other measured values are unchanged.

trueadm and others added 2 commits October 8, 2026 14:25
Write releasePortalTarget's body as teardownPortalState's original release
block. The earlier early-return form was 22 raw bytes smaller than main but
16 gzip bytes larger in the bindings app's framework chunk, 1 byte over its
budget. This form is 23 raw bytes smaller and fits (108,929 of 108,932).

Record the savings the change makes elsewhere: --write-budgets lowers nine
reachability and JSX gzip/brotli budgets and raises none.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…elegation-refcount

# Conflicts:
#	benchmarks/bundle-size/jsx-budgets.json
#	benchmarks/bundle-size/minimal-budgets.json
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website-mcp 4404841 Commit Preview URL

Branch Preview URL
Oct 08 2026, 02:19 PM

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
octane-website 4404841 Commit Preview URL

Branch Preview URL
Oct 08 2026, 02:16 PM

trueadm and others added 5 commits October 8, 2026 15:28
run-minimal.mjs and run.mjs --write-budgets on the merged tree (main
e14fbdf plus this change): 67 lowerings, no raises. Raw drops 13 to 23
bytes per bundle, the shared releasePortalTarget replacing three inline
release sequences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trueadm
trueadm merged commit d30025a into main Oct 8, 2026
2 of 4 checks passed
@trueadm
trueadm deleted the fix/portal-rollback-delegation-refcount branch October 8, 2026 15:24
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.

1 participant