Skip to content

fix: don't assume boundary exists during increment/decrement#18289

Merged
Rich-Harris merged 5 commits into
mainfrom
effect-root-async-update-fix
May 26, 2026
Merged

fix: don't assume boundary exists during increment/decrement#18289
Rich-Harris merged 5 commits into
mainfrom
effect-root-async-update-fix

Conversation

@dummdidumm

Copy link
Copy Markdown
Member

Follow-up to #18273 (not merged yet hence no changeset here): We can run into a null-pointer when wanting to increment/decrement inside an effect root that is outside a the component tree. Similarly, if not using the component logic to unset context at the right time we gotta do it "manually" inside save.

Follow-up to #18273 (not merged yet hence no changeset here): We can run into a null-pointer when wanting to increment/decrement inside an effect root that is outside a the component tree. Similarly, if not using the component logic to unset context at the right time we gotta do it "manually" inside `save`.
@github-actions

Copy link
Copy Markdown
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@18289

@svelte-docs-bot

Copy link
Copy Markdown

…-effect-root/main.svelte

Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented May 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 150fde9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@Rich-Harris

Copy link
Copy Markdown
Member

Haven't fully understood this yet but: the fact that the same bug presents if you comment out the setTimeout and $effect.root calls...

...makes me think the root cause is subtly different and requires a different fix

Comment on lines +162 to +170
// If there's a boundary, the surrounding system will call unset_context at the appropriate time
if (!active.b) {
// If this is the last in a chain of async operations, the context would stay around
// until the next async operation happens, which can result in weird/buggy behavior
// because other sync work might suddenly be associated with an async batch.
queue_micro_task(() => {
if (batch === current_batch) unset_context();
});
}

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.

pretty sure we need to always unset a microtask after restoring. will add a test to confirm this

Suggested change
// If there's a boundary, the surrounding system will call unset_context at the appropriate time
if (!active.b) {
// If this is the last in a chain of async operations, the context would stay around
// until the next async operation happens, which can result in weird/buggy behavior
// because other sync work might suddenly be associated with an async batch.
queue_micro_task(() => {
if (batch === current_batch) unset_context();
});
}
queue_micro_task(unset_context);

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.

actually i think this is better: #18295

@Rich-Harris Rich-Harris merged commit b40c359 into main May 26, 2026
21 checks passed
@Rich-Harris Rich-Harris deleted the effect-root-async-update-fix branch May 26, 2026 21:37
@Rich-Harris

Copy link
Copy Markdown
Member

Oops, I wrote #18295 (comment) into the wrong PR. We need to revert this

@Rich-Harris

Copy link
Copy Markdown
Member

#18299

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.

2 participants