Skip to content

fix: more informative error when effects run in an infinite loop #16405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 16, 2025

Conversation

Rich-Harris
Copy link
Member

Supersedes #16247, closes #16092.

Today, if you have an effect that runs in an infinite loop, you get this somewhat unhelpful log:

image

Expanding the array usually reveals 10 copies of the same compiled function:

image

While you can click through to the [[FunctionLocation]]...

image

...even that doesn't necessarily tell you where the offending state change occurred.

We can do better. In this PR, we track the site of updates to state that occur inside an effect, then if we do encounter an infinite loop we can pinpoint those sites:

image

This works for things like array mutations, and even works for effects that ping-pong (as opposed to the probably-more-common case in which an effect invalidates itself):

$effect(() => {
  a = b + 1;
});

$effect(() => {
  b = a + 1;
});

Better yet, this is actually simpler than what we currently have — the implementation of infinite_loop_guard can be much leaner, and log_effect_stack and dev_effect_stack are no more.

This PR also adds an explanation of the error. For things like this I prefer progressive disclosure (i.e. link to the docs when it becomes relevant) rather than adding stuff that you have to read upfront, so I view this as an alternative to #16247.

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

Copy link

changeset-bot bot commented Jul 16, 2025

🦋 Changeset detected

Latest commit: 90ff2dc

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

@svelte-docs-bot
Copy link

Copy link
Contributor

Playground

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

@Ocean-OS Ocean-OS merged commit a67b586 into main Jul 16, 2025
15 checks passed
@Ocean-OS Ocean-OS deleted the better-self-invalidation-logging branch July 16, 2025 23:24
@github-actions github-actions bot mentioned this pull request Jul 16, 2025
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.

Testing of rune effects is broken
2 participants