Repository navigation
Release a replaced widget's lifetime before its replacement starts - #2573
Merged
Merged
Conversation
A revision that replaces a primary Thread reader with another one no longer throws "The page already has a Thread presentation", and the thread draft, selection and frozen widgets carry to the replacement again. #2503 moved widget registrations from connectedCallback and disconnectedCallback onto whileConnected. Its connect runs synchronously, but a departure is decided at the next arrival mutation batch. patchTree evicts the old element and then inserts its replacement within one task, so the replacement's start ran while the evicted element still held its registration, and any registration only one element may hold refused it. watchArrivals' explicit offer now reads the pending mutation records before deciding its element, so a decision made now never runs ahead of a departure already made. Every owner connects through that offer, so whatever left before it has released first. A move within one batch still keeps its resources; an element removed, then re-added after another owner has connected in between, now leaves and returns, as it did under disconnectedCallback. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
An owner returning through a mutation batch, as in old.replaceWith(R), was connected before the element it replaced released, because each record lists its added nodes before its removed ones. offerMany now decides every departure in the batch first, so the release-first rule holds for returning owners too, not only for new ones. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
approved these changes
Oct 12, 2026
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.
A revision that replaces a primary Thread reader with another one no longer throws "The page already has a Thread presentation". The thread draft, its selection and the frozen widgets carry across to the replacement again. This fixes review item #70:
test_primary_reader_revision_replaces_owner_without_losing_thread_draftwas failing onmain.Cause
Bisect over
main(781f2da..6b1210e, running only that test) names #2503 (d57bf19) as the first bad commit; its parent bb47f5f passes. #2503 movedlf-conversation-workspace's registrations offconnectedCallback/disconnectedCallbackand ontowhileConnected.whileConnected's connect runs synchronously, butwatchOwnerdecides a departure only at the next arrival mutation batch.patchTreeevicts the replaced element and then inserts its replacement in the same task, so the replacement'sstartran while the evicted element still held its registration. The single-holder registries (thread/destination.jsregister,thread/presentation.jsregisterPrimary) then refused it.dom-move.jsappears in the stack only because it is the insertion that connects the element. #2545, #2552 and #2565 did not cause it; #2552's PR notes that the test already failed without it.Fix
This is fixed in the lifetime primitive, not in each registry. Two changes in
arrivals.js:offerthatwatchArrivalsreturns now reads the pending mutation records (takeRecords) before it decides its element. A decision made now therefore never runs ahead of a departure already made.watchOwneroffers every owner as it connects, so whatever left before has released first.offerManynow decides every departure in a batch before any arrival in it. Each mutation record lists its added nodes before its removed ones, so before this, an owner returning through the batch (old.replaceWith(returning)) started beforeoldreleased.A move within one batch still keeps its resources. An element that is removed, and then re-added after another owner connected in between, now leaves and comes back, which is what
disconnectedCallbackdid before #2503. Thearrivals.jsheader andmodule-authoring.md("Connection lifetimes") now state this rule. The single-holder checks in the registries stay as they are, since they guard a real invariant: two primary readers standing at once.Why CI didn't catch it
The test is in the
nightlytier (tests/cost_tiers.json: it costs more than 8 s on CI's runner). A pull request's gate adds later-tier tests only when the change edits their own lines (--changed-since), and #2503 did not edit this one. The last scheduled run started on 2026-10-11 at 10:53 UTC, before #2503 landed, so no nightly run has executed the test since the break. The three most recent scheduled runs (10-09, 10-10 and 10-11) were all cancelled, so the nightly tier has not reported for three days.Measured (item #70)
arrivals.js+26 / −16. Of that, code is +15 / −12 and the header comment is +11 / −4. The Markdown change is excluded.an owner replaced within one batch releases before its replacement starts, with two cases. The case of a new owner fails onmainwith "already held". The case of a returning owner fails the same way with only thetakeRecordschange applied. Both pass here.Validation
pnpm run test:runtimepasses (368 tests).test_render_application_boundary.pyandtest_render_threads.pypass, across all tiers.tests/, in all tiers (-k "returns or returning or lifecycle or lifetime or disconnect or mirror or revision_replaces or retains or carry or primary_reader or workspace or external_link or reading_region or code_copy"). All pass excepttest_render_anchors.py::test_code_copy_enter_leaves_nested_links_usable, which this change does not cause. Under-n 4that test fails 2 runs in 6 withcopyAtPresentation == 2, both withmain'sarrivals.jsand with this branch's.