sveltejs / sveltejs/kit

Calling a command inside an effect causes an infinite loop

Open
#14,605 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

svelte 5
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

The following triggers an infinite loop where pending_count is constantly updated.

<script>
  import { mark_seen } from '$lib/foo.remote.ts';

  const { params } = $props();

  $effect(() => {
    $inspect.trace('mark');
    mark_seen(params.id);
  });
</script>
Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-piu37bpc?file=src%2Froutes%2F%2Bpage.svelte

Logs
$state pending_count 1
$state pending_count 2
$state pending_count 3
... etc
Severity

serious, but I can work around it

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduced case in src/routes/+page.svelte from the linked StackBlitz project and inspect the $effect call to mark_seen(params.id). Run the reproduction and compare its behavior with the pending_count logs. Done means the command no longer causes the effect to loop and pending_count stops increasing continuously.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.