Skip to content

fix: create batch.eager for forks with eager work#18362

Merged
Rich-Harris merged 6 commits into
incremental-batchesfrom
incremental-batches-eager-fix
Jun 5, 2026
Merged

fix: create batch.eager for forks with eager work#18362
Rich-Harris merged 6 commits into
incremental-batchesfrom
incremental-batches-eager-fix

Conversation

@dummdidumm

Copy link
Copy Markdown
Member

Replaces the ad-hoc STATE_EAGER_EFFECT flag which makes eager effects over-execute with a mechanism that is two-fold:

  1. eager versions are sorted by batch. If a batch belongs to a fork, that way it's made sure it doesn't "escape" that fork
  2. batches now have an eager field, which is set for forks with eager work. It's flushed on commit just before the main commit. This way the eager work happens separately and eagerly just before the main fork commits, which itself may not be ready yet.

Additionally in sources.js we make sure active_batch is set properly when flushing eager effects.

Replaces the ad-hoc `STATE_EAGER_EFFECT` flag which makes eager effects over-execute with a mechanism that is two-fold:
1. eager versions are sorted by batch. If a batch belongs to a fork, that way it's made sure it doesn't "escape" that fork
2. batches now have an `eager` field, which is set for forks with eager work. It's flushed on commit just before the main commit. This way the eager work happens separately and eagerly just before the main fork commits, which itself may not be ready yet.

Additionally in `sources.js` we make sure `active_batch` is set properly when flushing eager effects.
@changeset-bot

changeset-bot Bot commented Jun 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5a78d3a

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

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Playground

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

@svelte-docs-bot

Copy link
Copy Markdown

@dummdidumm dummdidumm mentioned this pull request Jun 2, 2026
6 tasks

@vercel vercel Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

When a fork is discarded, the associated eager batch (batch.eager) is not discarded, causing memory leaks and potentially corrupting the batch linked list.

Fix on Vercel

Rich-Harris and others added 2 commits June 2, 2026 13:33
…er`) is not discarded, causing memory leaks and potentially corrupting the batch linked list.

This commit fixes the issue reported at packages/svelte/src/internal/client/reactivity/batch.js:1464

## Bug Explanation

The bug occurs in the `fork()` function's `discard` method in `batch.js`.

When using `$state.eager()` within a fork, the `eager_flush()` function creates a new Batch for the eager work:

```javascript
function eager_flush(batch) {
    flushSync(() => {
        const eager_batch = (current_batch = batch?.eager ?? new Batch());
        if (batch?.is_fork) {
            eager_batch.is_fork = true;  // Set to true, meaning #is_deferred() returns true
            batch.eager = eager_batch;   // Stored on the fork's batch
        }
        // ...
    });
}
```

This eager batch:
1. Is linked into the global batch linked list via `new Batch()`
2. Has `is_fork = true` set, which causes `#is_deferred()` to return true
3. Will never commit or unlink itself because `#is_deferred()` prevents progression

In the fork's `commit` method, this is properly handled:
```javascript
if (batch.eager) {
    const eager = batch.eager;
    eager.is_fork = false;  // Clear the flag
    // ... flush the batch ...
    eager.flush();
    batch.eager = null;  // Clear the reference
}
```

But in the `discard` method (before the fix), only `batch.discard()` was called:
```javascript
discard: () => {
    // ...
    if (!committed && batch.linked) {
        batch.discard();  // Only the main batch is discarded!
    }
}
```

This leaves the eager batch:
- Still linked in the batch list
- With `is_fork = true` (can never complete)
- Never cleaned up (memory leak)
- Potentially corrupting future batch operations

## Fix Explanation

The fix adds `batch.eager?.discard()` before `batch.discard()` in the fork's discard method:

```javascript
if (!committed && batch.linked) {
    batch.eager?.discard();  // Also discard the eager batch if it exists
    batch.discard();
}
```

This ensures that when a fork is discarded, any associated eager batch is also properly discarded and unlinked from the batch list, preventing memory leaks and list corruption.

Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
Co-authored-by: Rich-Harris <hello@rich-harris.dev>
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
Comment thread packages/svelte/src/internal/client/reactivity/batch.js Outdated
dummdidumm and others added 2 commits June 2, 2026 22:34
Co-authored-by: Rich Harris <hello@rich-harris.dev>
@Rich-Harris Rich-Harris merged commit 00eefb6 into incremental-batches Jun 5, 2026
17 checks passed
@Rich-Harris Rich-Harris deleted the incremental-batches-eager-fix branch June 5, 2026 21:52
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