Svelte

fix: prevent scheduler deadlock when $state is written in getter during flush

#17892

Closing issue

Pull request

Closed
V
vdg
Mar 10, 2026, 10:08 AM

Description

Fixes

When a getter lazily writes $state on first read, and that getter is read during the flush/commit phase (e.g., in a style: binding), the scheduler deadlocks silently in 5.53.8+.

The bug

In Batch.schedule(), when walking up the effect tree, if a branch is already "dirty" (CLEAN bit is 0), the method bails out early:

if ((flags & CLEAN) === 0) {
  // branch is already dirty, bail
  return;
}

The assumption is: if the branch is dirty, the root must already be scheduled. But this breaks when a new batch is created during flush — the branches are dirty from the previous batch's traversal, but the root isn't scheduled in the new batch.

The fix

Don't bail when branch is dirty — just skip marking it (since it's already dirty) and continue to the root. Add deduplication check to prevent pushing the same root multiple times:

if ((flags & (ROOT_EFFECT | BRANCH_EFFECT)) !== 0) {
  if ((flags & CLEAN) !== 0) {
    e.f ^= CLEAN;
  }
  // Don't bail — continue to root
}

// ...

if (!includes.call(this.#roots, e)) {
  this.#roots.push(e);
}

Test

Added state-write-in-getter-during-flush runtime test that reproduces the exact pattern from the bug report.


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

(Unable to run locally — CI will validate)

Info

Closed at Mar 10, 2026, 2:11 PM
Assignees None
Reviewers None
Labels None
Milestone None

Pro tip: You can prefix GitHub URLs of issues, PRs or discussions with svcl.dev/ to view them on this page! Also try it on a GitHub release ;)