Skip to content

fix: don't create async deriveds for a component destroyed while waiting for its top-level await - #18931

Merged
dummdidumm merged 2 commits into
sveltejs:mainfrom
jarrednorrisdev:fix/async-destroyed-before-blockers
Oct 6, 2026
Merged

dummdidumm merged 2 commits into
sveltejs:mainfrom
jarrednorrisdev:fix/async-destroyed-before-blockers

Conversation

@jarrednorrisdev

Copy link
Copy Markdown
Contributor

Fixes #18930

The problem

With experimental.async, a component with a top-level await in its script and an await in its markup throws once the script's await settles if the component was destroyed in the meantime:

<!-- Child.svelte -->
<script>
	const x = await new Promise((r) => setTimeout(() => r(1), 500));
</script>

<p>{await Promise.resolve(x)}</p>
<!-- App.svelte -->
<script>
	import Child from './Child.svelte';

	let show = $state(true);
	setTimeout(() => (show = false), 100);
</script>

<svelte:boundary>
	{#if show}<Child />{/if}
	{#snippet pending()}loading{/snippet}
</svelte:boundary>
TypeError: Cannot read properties of null (reading 'async_deriveds')
    at async_derived (reactivity/deriveds.js)
    at run (reactivity/async.js)
    at reactivity/async.js  (flatten's blocker_promise.then callback)

It's more than an unhandled rejection. The boundary stays on its pending snippet forever, and without a boundary the root never renders. It happens with {await ...} and {#if await ...}, in dev and production builds, on 5.57.1 and main. Plain markup, $derived(await ...) in the script, and a markup await without a script await are unaffected.

The cause

In flatten, the template's await expressions wait for the script's blockers. When they settle, the callback calls restore() and then run(), which creates the async deriveds. restore() deliberately doesn't reactivate the batch for a destroyed effect, so async_derived runs with current_batch === null and throws at batch.async_deriveds. The throw happens before run attaches .finally(decrement_pending), so the pending count from increment_pending() is never released. That's why the boundary (or the root) stays pending.

The other two ways flatten resumes already handle a destroyed parent: finish returns early, and in both cases decrement_pending still runs in a .finally. The blocker callback was the one path without the check.

The fix

In the blocker callback, if parent was destroyed, call decrement_pending() and return without creating the deriveds. This matches what the fast path (blockers, no async expressions) already does through finish and .finally(decrement_pending).

Tests

runtime-runes/samples/async-destroyed-before-blockers-settle: a child with const value = await promise in its script and {await ...} in its markup is removed by an {#if} before the promise resolves, then the promise is resolved, and the test asserts that the boundary is no longer pending. Without the fix it fails in dom and hydrate with the TypeError above. With only an early return (no decrement_pending()), it fails on the stuck <p>loading...</p>.

Test plan

  • pnpm vitest run packages/svelte/tests/runtime-runes/test.ts -t async-destroyed-before-blockers-settle: 2 passed (fails on main with the TypeError)
  • pnpm test: 7,808 passed, 55 skipped, all 34 test files passing, including runtime-browser
  • pnpm lint: passes (ESLint and Prettier)
  • pnpm check: passes
  • The repro above in a Vite 8 app with experimental.async, linked to the patched package, with vite dev and vite build + vite preview: no error, boundary leaves pending and later updates render. The variants with {#if await ...}, without a boundary, and the unaffected forms behave the same, and when the child isn't removed it still renders its value.

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
  • 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.
  • 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

🤖 Generated with Claude Code

…ing for its top-level `await`

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@pkg-svelte-dev

pkg-svelte-dev Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Install the latest version of svelte from 7cd9dc6:

pnpm add https://pkg.svelte.dev/svelte/c/7cd9dc60312cc43a96c1d5ec9b09c2d656cf4a3f

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

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 Oct 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7cd9dc6

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

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@dummdidumm
dummdidumm merged commit 179f66b into sveltejs:main Oct 6, 2026
11 of 13 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 6, 2026
dummdidumm pushed a commit that referenced this pull request Oct 6, 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.57.2

### Patch Changes

- fix: don't create async deriveds for a component destroyed while
waiting for its top-level `await`
([#18931](#18931))

- fix: prevent untracked derived reads from retaining disconnected
dependencies ([#18829](#18829))

- fix: error if `{:else}` is followed by another `{:else}` or `{:else if
...}` ([#18900](#18900))

- fix: avoid tracking SvelteURLSearchParams during URL updates
([#18791](#18791))

- fix: restore hydration state when custom element attribute updates
throw ([#18467](#18467))

- fix: attach event handlers that are assigned after a top-level `await`
in production ([#18933](#18933))

- fix: flush anything pending before invoking flushSync callback
function ([#18878](#18878))

- fix: preserve location information for `await` wrappers
([#18859](#18859))

- fix: keep a reactive assignment with no dependencies when `migrate`
cannot turn it into a declaration
([#18817](#18817))

- fix: don't hang `migrate` on a declaration that shares a line with its
script tag ([#18816](#18816))

- fix: prevent duplicate subscriptions when reconnecting deriveds
([#18899](#18899))

- fix: recover when an unexpected node replaces a nested boundary
comment during hydration
([#18925](#18925))

- fix: print `{#await}` blocks that only have a pending branch
([#18908](#18908))

- fix: don't warn about a redundant `link` role on `<area>` elements
without an `href`
([#18872](#18872))

- fix: don't throw `state_unsafe_mutation` when the focused element is
removed while `bind:activeElement` is used
([#18921](#18921))

- fix: don't overwrite an unchanged spread `value`, preserving
incomplete number input
([#18864](#18864))

- fix: read batch-local array on each-block commit
([#18879](#18879))

- fix: error at compile time when a declaration in a snippet redeclares
one of its parameters
([#18842](#18842))

- fix: error when using `let:` directives on a component with a
`children` snippet
([#18873](#18873))

- fix: ignore stale boundary reset callbacks
([#18886](#18886))

- fix: handle deferred transitions aborted before initialization
([#18776](#18776))

- fix: prevent hydration mismatch recovery from being intercepted by
error boundaries
([#18841](#18841))

- fix: preserve dynamic element connections during hydration
([#18855](#18855))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
jarrednorrisdev added a commit to jarrednorrisdev/effect-atom-svelte that referenced this pull request Oct 6, 2026
Svelte 5.57.2 fixes event handlers assigned after a top-level await, which
were undefined in production builds (sveltejs/svelte#18933, JND-56), and a
component destroyed mid-await with an await in its markup throwing on
`async_deriveds` (sveltejs/svelte#18931, JND-39).

- Raise the svelte peer minimum to ^5.57.2, and the dev dependencies with it.
- Remove "Handlers after an await" from Troubleshooting, and the mentions
  on the Suspense and React migration pages and in the hydration example.
- Say Svelte 5.57.2 or later on the home and SvelteKit pages and in both
  READMEs.
- Await in the script-await fixture's markup too, so the unmount-mid-load
  tests cover it. On 5.57.1 this fails with the async_deriveds error.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
dadezzz pushed a commit to dadezzz/events-cash-register that referenced this pull request Oct 10, 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.57.1` → `5.57.2`](https://renovatebot.com/diffs/npm/svelte/5.57.1/5.57.2) | ![age](https://developer.mend.io/api/mc/badges/age/npm/svelte/5.57.2?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/svelte/5.57.1/5.57.2?slim=true) |

---

### Release Notes

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

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

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

##### Patch Changes

- fix: don't create async deriveds for a component destroyed while waiting for its top-level `await` ([#&#8203;18931](sveltejs/svelte#18931))

- fix: prevent untracked derived reads from retaining disconnected dependencies ([#&#8203;18829](sveltejs/svelte#18829))

- fix: error if `{:else}` is followed by another `{:else}` or `{:else if ...}` ([#&#8203;18900](sveltejs/svelte#18900))

- fix: avoid tracking SvelteURLSearchParams during URL updates ([#&#8203;18791](sveltejs/svelte#18791))

- fix: restore hydration state when custom element attribute updates throw ([#&#8203;18467](sveltejs/svelte#18467))

- fix: attach event handlers that are assigned after a top-level `await` in production ([#&#8203;18933](sveltejs/svelte#18933))

- fix: flush anything pending before invoking flushSync callback function ([#&#8203;18878](sveltejs/svelte#18878))

- fix: preserve location information for `await` wrappers ([#&#8203;18859](sveltejs/svelte#18859))

- fix: keep a reactive assignment with no dependencies when `migrate` cannot turn it into a declaration ([#&#8203;18817](sveltejs/svelte#18817))

- fix: don't hang `migrate` on a declaration that shares a line with its script tag ([#&#8203;18816](sveltejs/svelte#18816))

- fix: prevent duplicate subscriptions when reconnecting deriveds ([#&#8203;18899](sveltejs/svelte#18899))

- fix: recover when an unexpected node replaces a nested boundary comment during hydration ([#&#8203;18925](sveltejs/svelte#18925))

- fix: print `{#await}` blocks that only have a pending branch ([#&#8203;18908](sveltejs/svelte#18908))

- fix: don't warn about a redundant `link` role on `<area>` elements without an `href` ([#&#8203;18872](sveltejs/svelte#18872))

- fix: don't throw `state_unsafe_mutation` when the focused element is removed while `bind:activeElement` is used ([#&#8203;18921](sveltejs/svelte#18921))

- fix: don't overwrite an unchanged spread `value`, preserving incomplete number input ([#&#8203;18864](sveltejs/svelte#18864))

- fix: read batch-local array on each-block commit ([#&#8203;18879](sveltejs/svelte#18879))

- fix: error at compile time when a declaration in a snippet redeclares one of its parameters ([#&#8203;18842](sveltejs/svelte#18842))

- fix: error when using `let:` directives on a component with a `children` snippet ([#&#8203;18873](sveltejs/svelte#18873))

- fix: ignore stale boundary reset callbacks ([#&#8203;18886](sveltejs/svelte#18886))

- fix: handle deferred transitions aborted before initialization ([#&#8203;18776](sveltejs/svelte#18776))

- fix: prevent hydration mismatch recovery from being intercepted by error boundaries ([#&#8203;18841](sveltejs/svelte#18841))

- fix: preserve dynamic element connections during hydration ([#&#8203;18855](sveltejs/svelte#18855))

</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:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNDguNCIsInVwZGF0ZWRJblZlciI6IjQ0LjE0OC40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants