Repository navigation
fix(octane): release a discarded portal's target delegation once - #1895
Merged
Merged
Conversation
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]>
…elegation-refcount
…elegation-refcount
trueadm
marked this pull request as ready for review
October 8, 2026 13:07
Contributor
Benchmark report
Compares js-framework: 1k/10k row operationsAll 20 timed operations (20 without a verdict)
All 64 work counters are unchanged. Application bundle size (bytes)
9 other measured values are unchanged. Public-import reachability bundles (bytes)
37 other measured values are unchanged. |
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
5 of 8 tasks
Deploying with
|
| 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 |
Deploying with
|
| 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 |
…elegation-refcount
…elegation-refcount
…elegation-refcount
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
registerDelegationTargetrefcounts the target, and its listeners detach when the count reaches zero.renderPortalState's creation undo released the portal once. The owner's teardown released it again, throughunmountSlot'sportalSlotSlotbranch orchildSlot'steardownPortalState. A committed portal in the same target then lost its listeners, and its handlers (onClickon a portal button, say) stopped firing.releasePortalTarget, which nulls the slot'stargetas it releases. Whichever path runs first releases the portal, and the other one is a no-op.teardownPortalStatealready usedtarget === nullto mean "released". The creation undo andunmountSlotnow do too.Why
The two release orders that a rollback reaches on main:
createPortal, a component returningcreatePortal(...), or a new@if/@switcharm), or by a committed value hole that switches to a portal. Rollback runs the creation undo, thenJOURNAL_CREATEDunmounts the owner, which releases the portal again.registerSlot's undo for a mounted scope would give the second order too. On main, a scope is notmountedduring its own first render, so this order needs a mounted scope that reachesportal()for the first time. The change makes that order safe as well.Changes
packages/octane/src/runtime.ts: addreleasePortalTarget(state). Use it inunmountSlot'sportalSlotSlotbranch, in therenderPortalStatecreation undo, and inteardownPortalState.packages/octane/tests/portal-rollback-delegation.test.tsand_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:octanepatch.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:checkon the four changed files passes)pnpm typecheck(not run repo-wide;pnpm typecheck:filesonruntime.tsplus the new test and fixture passes)pnpm test(left to CI, per the local test-load rule)vitest run --project octane --project octane-prod packages/octane/tests/portal-rollback-delegation.test.ts→ 14/14 pass.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.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:
unmountSlotruns once per registered slot on unmount. Only itsportalSlotSlotbranch changed, and that branch now makes one monomorphic call per portal slot instead of two inline calls.renderPortalStatecreates one undo closure per created portal inside a root transaction, as before. The closure now captures one variable instead of two.releasePortalTargetwritesnulltoPortalSlot.target.teardownPortalStatealready wrotenullthere on main, so the field's representation doesn't change, andPortalSlotgains no new key.returnfor a null target) put the bindings framework's gzip at 108,933, 1 byte over its 108,932 budget. The helper now repeatsteardownPortalState'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.Dismissed candidates: none.
Not checked: render or commit timing. No allocation, DOM work, or scheduling changed on a per-render path.
Risk / follow-ups
unmountSlotunmounts a compiled portal slot, the slot'stargetis nownull. No code reads a disposed slot'starget. The cleanups that need it, such asevacuateInterleavedPortalRanges, run insideunmountBlock, before the release.targetbefore the portal block's ownJOURNAL_CREATEDentry unmounts it. That leavesevacuateInterleavedPortalRangeswith nothing to do there, and it had nothing to move anyway. A portal created in that render appends its markers at the end of its target, so any portal range after it was also created in that render, and the reverse replay has already removed it.--write-budgetsgives 67 lowerings and no raises, andbudget-raises.mjsconfirms that. CI's brotli can differ from local measurements, and the lowered brotli values keep 256 bytes of headroom. Theview-transition-scoped-effectsfailure seen earlier in test shard 2/4 is the flake that test(octane): hold the frame clock in the pre-render passive ViewTransition test #1902 makes deterministic.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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 clearsstate.targetafter 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.