Skip to content

fix: release last_propagated_event after event propagation settles - #18569

Merged
dummdidumm merged 1 commit into
sveltejs:mainfrom
randalmurphal:fix/release-last-propagated-event
Aug 20, 2026
Merged

dummdidumm merged 1 commit into
sveltejs:mainfrom
randalmurphal:fix/release-last-propagated-event

Conversation

@randalmurphal

Copy link
Copy Markdown
Contributor

Fixes #18568

last_propagated_event was added in #16527 to stop Firefox from garbage collecting the event wrapper mid-propagation, which loses the __root expando and makes delegated handlers run twice (#16522). The problem is that the reference is never released. Between user interactions the module keeps holding the last delegated event, and through event.target it also holds the detached subtree of whatever the user last clicked in. In long-lived apps (SPAs left idle, desktop webviews) that can be a whole closed view sitting in memory until the next delegated event happens to arrive. I found this while doing heap snapshot analysis on a desktop app, where a closed pane's ~10k node tree was still reachable and the retainer path ended at this slot.

This PR keeps the pin but clears it in a task scheduled during dispatch. Event dispatch is synchronous within a task, and the Firefox GC issue bites at the microtask checkpoints between listeners, so propagation can never cross a task boundary. That means the clear always runs after the event has finished reaching every delegated root, and the workaround keeps working exactly as before. Retention drops from "until the next delegated event" to milliseconds. The boolean guard just makes sure there's at most one pending timer no matter how many events come in.

Verification

The Firefox bug still reproduces reliably on Firefox 141. It no longer reproduces on 142+ (looks like Mozilla fixed the underlying wrapper GC behavior), but ESR 140 is still supported, so the pin is still needed. I drove both the plain JS reduction from #16522 and a Vite-built app mirroring the #16522 REPL (delegated click toggling a 10k item {#if} list) with Playwright:

variant Firefox 141, default GC Firefox 141, small nursery
no pin (pre-#16527) 26/60 events lose the expando; 68 toggles for 40 clicks 53/60; 78 toggles for 40 clicks
pin, never cleared (current) 0 lost; 200/200 clicks correct 0 lost; 200/200 correct
pin + deferred clear (this PR) 0 lost; 200/200 correct 0 lost; 200/200 correct
pin + queueMicrotask clear 26/60 lost 50/60 lost

The last row is why the clear has to be a task and not a microtask. Instrumentation shows a microtask clear fires between the mount root listener and the document fallback listener, right inside the danger window, while the task clear never does (the fallback listener saw the pin still in place on 60/60 dispatches). The app-level runs used the minified production bundle, so the mechanism also survives DCE/minification (the #16538 concern).

One side effect worth calling out: each delegated event now leaves at most one pending zero-delay timeout behind, which fake timer test setups can see. If that's a concern, a MessageChannel port would give the same task-boundary clear without being visible to fake timers. Happy to switch if preferred.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs (last_propagated_event retains the last event's target subtree (detached DOM) until the next delegated event #18568)
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it. (The only observable here is GC behavior, since the slot is module-private and semantically invisible by design. A WeakRef + --expose-gc test that dispatches a delegated click, unmounts, and asserts the target becomes collectable does fail on main and pass with this PR. Happy to add it if a GC-dependent test is acceptable in CI. fix: double event processing in Firefox (#16522) #16527 shipped without a test for the same reason.)
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

pnpm test: 7663 passed | 69 skipped (33 files). pnpm lint: clean. pnpm check (packages/svelte): clean. pnpm build (packages/svelte) incl. treeshakeability checks: clean.

The strong reference exists to stop Firefox (<= 141) from garbage
collecting the event wrapper mid-propagation, which loses the __root
expando and causes delegated handlers to run twice (sveltejs#16522). But the
reference was never released, so between user interactions the module
retains the last delegated event - and through event.target the entire
detached subtree of whatever component the user last clicked in, until
the next delegated event happens to arrive.

Dispatch is synchronous within a task, so a task scheduled during
propagation runs strictly after the event has finished reaching every
delegated root (including all microtask checkpoints between listeners).
Clearing the reference there preserves the Firefox workaround exactly
while bounding the retention to milliseconds.

Verified against the sveltejs#16522 reproduction on Firefox 141 (where the
wrapper GC bug reproduces reliably): pin-with-deferred-clear loses zero
events across 200 clicks under GC pressure, identical to the permanent
pin, while clearing in a microtask instead reintroduces the double
processing at the same rate as having no pin at all.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of svelte from 4a0d1c7:

pnpm add https://pkg.svelte.dev/svelte/c/4a0d1c75619f9140a725cc2df7239b23d5b01ed4

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/svelte/pr/18569

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4a0d1c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@7nik

7nik commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

That Firefox bug was marked as fixed almost 1 year ago, so maybe we can just remove the last_propagated_event workaround?

@GrygrFlzr

Copy link
Copy Markdown
Member

That Firefox bug was marked as fixed almost 1 year ago, so maybe we can just remove the last_propagated_event workaround?

The next ESR 153 is only scheduled to release within the next 24 hours, so ESR 140 is still the current ESR, and ESR 140 is not EOL until later on September 15th, 2026.

@gyzerok

gyzerok commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Yeah, we've hit this as well. Technically it's not a leak, because it doesn't grow over time, but it is still annoying and also it messes up bit our CI memory leak testing. So would be great to get it fixed either way

@gyzerok

gyzerok commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@7nik can we move this forward?

@dummdidumm
dummdidumm merged commit ffc0e6e into sveltejs:main Aug 20, 2026
15 of 16 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 20, 2026
Rich-Harris pushed a commit that referenced this pull request Aug 20, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## svelte@5.56.10

### Patch Changes

- fix: preserve CSS escape sequences when printing selectors
([#18667](#18667))

- fix: parse `:nth-child(2n of.foo)` where `of` is not followed by
whitespace ([#18611](#18611))

- fix: transform expressions inside labeled statements during server
compilation ([#18617](#18617))

- docs: clarify that context lookup includes the current component and
all ancestors ([#18581](#18581))

- fix: apply CSS custom properties with falsy values on components
([#18634](#18634))

- fix: correctly print `{#await ... catch x}` et al
([#18645](#18645))

- fix: ignore comments of Program node during migration script
([#18656](#18656))

- fix: reliably resolve append_style to its correct root
([#18614](#18614))

- fix: clean up removed capture event handlers from spread attributes
([#18618](#18618))

- fix: don't corrupt renderer type during SSR's legacy `bind:` retry
loop ([#18616](#18616))

- fix: treat concise arrow function bodies as implicit returns when
calculating blockers
([#18613](#18613))

- fix: give effect teardowns the value from before the first write in a
flush ([#18620](#18620))

- fix: avoid double-calling a derived reference when destructuring
`$derived` of another `$derived` during server-side rendering
([#18668](#18668))

- fix: preserve namespaces in CSS type selectors
([#18678](#18678))

- fix: increment private state fields through a non-`this` receiver
([#18622](#18622))

- chore: deduplicate client and server context helpers
([#18580](#18580))

- fix: release `last_propagated_event` after event propagation settles
so it no longer retains the last event's target subtree
([#18569](#18569))

- fix: allow custom elements to receive async values as props
([#18661](#18661))

- fix: strip comments from inline `style` values in linear time
([#18553](#18553))

- fix: prevent declaration comments from breaking server derived
references ([#18641](#18641))

- perf: make async blocker analysis scale linearly with the number of
top-level references
([#18549](#18549))

- fix: preserve short-circuiting for logical assignments to private
state fields ([#18594](#18594))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
dadezzz pushed a commit to dadezzz/events-cash-register that referenced this pull request Aug 25, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [svelte](https://svelte.dev) ([source](https://github.com/sveltejs/svelte/tree/HEAD/packages/svelte)) | [`5.56.9` → `5.56.10`](https://renovatebot.com/diffs/npm/svelte/5.56.9/5.56.10) | ![age](https://developer.mend.io/api/mc/badges/age/npm/svelte/5.56.10?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/svelte/5.56.9/5.56.10?slim=true) |

---

### Release Notes

<details>
<summary>sveltejs/svelte (svelte)</summary>

### [`v5.56.10`](https://github.com/sveltejs/svelte/blob/HEAD/packages/svelte/CHANGELOG.md#55610)

[Compare Source](https://github.com/sveltejs/svelte/compare/svelte@5.56.9...svelte@5.56.10)

##### Patch Changes

- fix: preserve CSS escape sequences when printing selectors ([#&#8203;18667](sveltejs/svelte#18667))

- fix: parse `:nth-child(2n of.foo)` where `of` is not followed by whitespace ([#&#8203;18611](sveltejs/svelte#18611))

- fix: transform expressions inside labeled statements during server compilation ([#&#8203;18617](sveltejs/svelte#18617))

- docs: clarify that context lookup includes the current component and all ancestors ([#&#8203;18581](sveltejs/svelte#18581))

- fix: apply CSS custom properties with falsy values on components ([#&#8203;18634](sveltejs/svelte#18634))

- fix: correctly print `{#await ... catch x}` et al ([#&#8203;18645](sveltejs/svelte#18645))

- fix: ignore comments of Program node during migration script ([#&#8203;18656](sveltejs/svelte#18656))

- fix: reliably resolve append\_style to its correct root ([#&#8203;18614](sveltejs/svelte#18614))

- fix: clean up removed capture event handlers from spread attributes ([#&#8203;18618](sveltejs/svelte#18618))

- fix: don't corrupt renderer type during SSR's legacy `bind:` retry loop ([#&#8203;18616](sveltejs/svelte#18616))

- fix: treat concise arrow function bodies as implicit returns when calculating blockers ([#&#8203;18613](sveltejs/svelte#18613))

- fix: give effect teardowns the value from before the first write in a flush ([#&#8203;18620](sveltejs/svelte#18620))

- fix: avoid double-calling a derived reference when destructuring `$derived` of another `$derived` during server-side rendering ([#&#8203;18668](sveltejs/svelte#18668))

- fix: preserve namespaces in CSS type selectors ([#&#8203;18678](sveltejs/svelte#18678))

- fix: increment private state fields through a non-`this` receiver ([#&#8203;18622](sveltejs/svelte#18622))

- chore: deduplicate client and server context helpers ([#&#8203;18580](sveltejs/svelte#18580))

- fix: release `last_propagated_event` after event propagation settles so it no longer retains the last event's target subtree ([#&#8203;18569](sveltejs/svelte#18569))

- fix: allow custom elements to receive async values as props ([#&#8203;18661](sveltejs/svelte#18661))

- fix: strip comments from inline `style` values in linear time ([#&#8203;18553](sveltejs/svelte#18553))

- fix: prevent declaration comments from breaking server derived references ([#&#8203;18641](sveltejs/svelte#18641))

- perf: make async blocker analysis scale linearly with the number of top-level references ([#&#8203;18549](sveltejs/svelte#18549))

- fix: preserve short-circuiting for logical assignments to private state fields ([#&#8203;18594](sveltejs/svelte#18594))

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4zMS4wIiwidXBkYXRlZEluVmVyIjoiNDQuMzEuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Aug 30, 2026
##### [v5.56.10](https://github.com/sveltejs/svelte/blob/HEAD/packages/svelte/CHANGELOG.md#55610)

##### Patch Changes

- fix: preserve CSS escape sequences when printing selectors ([#18667](sveltejs/svelte#18667))

- fix: parse `:nth-child(2n of.foo)` where `of` is not followed by whitespace ([#18611](sveltejs/svelte#18611))

- fix: transform expressions inside labeled statements during server compilation ([#18617](sveltejs/svelte#18617))

- docs: clarify that context lookup includes the current component and all ancestors ([#18581](sveltejs/svelte#18581))

- fix: apply CSS custom properties with falsy values on components ([#18634](sveltejs/svelte#18634))

- fix: correctly print `{#await ... catch x}` et al ([#18645](sveltejs/svelte#18645))

- fix: ignore comments of Program node during migration script ([#18656](sveltejs/svelte#18656))

- fix: reliably resolve append\_style to its correct root ([#18614](sveltejs/svelte#18614))

- fix: clean up removed capture event handlers from spread attributes ([#18618](sveltejs/svelte#18618))

- fix: don't corrupt renderer type during SSR's legacy `bind:` retry loop ([#18616](sveltejs/svelte#18616))

- fix: treat concise arrow function bodies as implicit returns when calculating blockers ([#18613](sveltejs/svelte#18613))

- fix: give effect teardowns the value from before the first write in a flush ([#18620](sveltejs/svelte#18620))

- fix: avoid double-calling a derived reference when destructuring `$derived` of another `$derived` during server-side rendering ([#18668](sveltejs/svelte#18668))

- fix: preserve namespaces in CSS type selectors ([#18678](sveltejs/svelte#18678))

- fix: increment private state fields through a non-`this` receiver ([#18622](sveltejs/svelte#18622))

- chore: deduplicate client and server context helpers ([#18580](sveltejs/svelte#18580))

- fix: release `last_propagated_event` after event propagation settles so it no longer retains the last event's target subtree ([#18569](sveltejs/svelte#18569))

- fix: allow custom elements to receive async values as props ([#18661](sveltejs/svelte#18661))

- fix: strip comments from inline `style` values in linear time ([#18553](sveltejs/svelte#18553))

- fix: prevent declaration comments from breaking server derived references ([#18641](sveltejs/svelte#18641))

- perf: make async blocker analysis scale linearly with the number of top-level references ([#18549](sveltejs/svelte#18549))

- fix: preserve short-circuiting for logical assignments to private state fields ([#18594](sveltejs/svelte#18594))
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Aug 30, 2026
##### [v5.56.10](https://github.com/sveltejs/svelte/blob/HEAD/packages/svelte/CHANGELOG.md#55610)

##### Patch Changes

- fix: preserve CSS escape sequences when printing selectors ([#18667](sveltejs/svelte#18667))

- fix: parse `:nth-child(2n of.foo)` where `of` is not followed by whitespace ([#18611](sveltejs/svelte#18611))

- fix: transform expressions inside labeled statements during server compilation ([#18617](sveltejs/svelte#18617))

- docs: clarify that context lookup includes the current component and all ancestors ([#18581](sveltejs/svelte#18581))

- fix: apply CSS custom properties with falsy values on components ([#18634](sveltejs/svelte#18634))

- fix: correctly print `{#await ... catch x}` et al ([#18645](sveltejs/svelte#18645))

- fix: ignore comments of Program node during migration script ([#18656](sveltejs/svelte#18656))

- fix: reliably resolve append\_style to its correct root ([#18614](sveltejs/svelte#18614))

- fix: clean up removed capture event handlers from spread attributes ([#18618](sveltejs/svelte#18618))

- fix: don't corrupt renderer type during SSR's legacy `bind:` retry loop ([#18616](sveltejs/svelte#18616))

- fix: treat concise arrow function bodies as implicit returns when calculating blockers ([#18613](sveltejs/svelte#18613))

- fix: give effect teardowns the value from before the first write in a flush ([#18620](sveltejs/svelte#18620))

- fix: avoid double-calling a derived reference when destructuring `$derived` of another `$derived` during server-side rendering ([#18668](sveltejs/svelte#18668))

- fix: preserve namespaces in CSS type selectors ([#18678](sveltejs/svelte#18678))

- fix: increment private state fields through a non-`this` receiver ([#18622](sveltejs/svelte#18622))

- chore: deduplicate client and server context helpers ([#18580](sveltejs/svelte#18580))

- fix: release `last_propagated_event` after event propagation settles so it no longer retains the last event's target subtree ([#18569](sveltejs/svelte#18569))

- fix: allow custom elements to receive async values as props ([#18661](sveltejs/svelte#18661))

- fix: strip comments from inline `style` values in linear time ([#18553](sveltejs/svelte#18553))

- fix: prevent declaration comments from breaking server derived references ([#18641](sveltejs/svelte#18641))

- perf: make async blocker analysis scale linearly with the number of top-level references ([#18549](sveltejs/svelte#18549))

- fix: preserve short-circuiting for logical assignments to private state fields ([#18594](sveltejs/svelte#18594))
randalmurphal added a commit to randalmurphal/agent-overflow that referenced this pull request Sep 2, 2026
svelte 5.56.8 to 5.57.0 and vite 8.2.0 to 8.2.2, exact pins. TypeScript
stays 6.0.2: svelte-check peers on ^5 || ^6, so moving it is a separate
question from these two.

svelte is patched here, so the bump is a re-roll rather than a version
edit — `pnpm install` refuses a patch key that names a version nothing
resolves to, and the old patch does not apply to 5.57.0 as it stands.
Rebased three-way against pristine 5.56.8, and one of the six hunks
came out on the other side: event-slot-release is now upstream, because
5.57.0 landed sveltejs/svelte#18569, which is that hunk verbatim. The
conflict was the two copies of the same code.

So the patch has five hunks and `svelte-patch-event-slot.test.ts` joins
`svelte-patch-zombie-leak.test.ts` as a suite with no hunk left, kept as
the tripwire that must pass UNPATCHED. Both were run for real, with
`--expose-gc`, against the unpatched dispatcher: the clicked element is
collectable after a dispatch without a hunk of ours making it so. The
other five hunks (ownerless-roots, destroy-pass-errors, flush-loop-caps,
reconnect-dedupe, flip-phases) merged clean and keep their suites.

The rename reaches everything that named the file: the hunk table and
its count in `frontend/AGENTS.md`, the header of every
`svelte-patch-*.test.ts`, the retention suite's note on why it needs no
flush, `threadRowUiState.svelte.ts`'s ownerless-roots comment, and the
flush-loop-caps error message the app persists in production, which
names the patch path back at you and is asserted on in two places.
`__incidentReplayFixture.ts` keeps the old name: it is byte-faithful
transcript content from the 2026-08-29 incident, not a claim about now.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

last_propagated_event retains the last event's target subtree (detached DOM) until the next delegated event

5 participants

⌂ 换网址