Skip to content

fix: check boundary exists before calling error handler in async derived#18384

Merged
Rich-Harris merged 7 commits into
sveltejs:mainfrom
sijie-Z:fix/async-derived-rejection-boundary
Jun 5, 2026
Merged

fix: check boundary exists before calling error handler in async derived#18384
Rich-Harris merged 7 commits into
sveltejs:mainfrom
sijie-Z:fix/async-derived-rejection-boundary

Conversation

@sijie-Z

@sijie-Z sijie-Z commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

fixes #18383

I traced the issue to packages/svelte/src/internal/client/error-handling.js, specifically the invoke_error_boundary function. The problem is at line 56 where effect.b.error(error) is called without checking if effect.b exists.

What happens is: when an async $derived rejects inside a <svelte:boundary>, the error is handled correctly the first time. But if the boundary is destroyed before the rejection fully settles (e.g. component unmounts), a subsequent settle tries to call effect.b.error() on a destroyed boundary where effect.b is null, causing a TypeError.

The fix adds a null check for effect.b before calling error(). If the boundary has been destroyed, we skip it and continue bubbling up to the parent boundary. This way the error still gets handled properly instead of crashing.

This could also be tested by creating a component with an async $derived that rejects after the component unmounts, but I wanted to get the fix up first for review.

sijie-Z added 2 commits June 5, 2026 14:40
When an async derived value rejects, the error is handled by the
boundary. However, if the boundary is destroyed before the rejection
settles (e.g. due to component unmount), effect.b becomes null and
calling effect.b.error() throws a TypeError.

This fix adds a null check for effect.b in invoke_error_boundary,
allowing the error to bubble up to the parent boundary instead of
crashing.

Fixes sveltejs#18383
@changeset-bot

changeset-bot Bot commented Jun 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7e8e902

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

@Rich-Harris Rich-Harris 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!

@Rich-Harris Rich-Harris merged commit 71a6515 into sveltejs:main Jun 5, 2026
15 of 16 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 5, 2026
Rich-Harris pushed a commit that referenced this pull request Jun 7, 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.3

### Patch Changes

- fix: ignore errors that occur in destroyed effects
([#18384](#18384))

- fix: type BigInts in `$state.snapshot(...)` return values
([#18388](#18388))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.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.

Async derived rejection escapes inner boundary

2 participants