From 14b09260089c9a333be1b67c3f6c5c729af9d3c8 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 11 Nov 2025 06:55:13 +0100 Subject: [PATCH] fix: don't deactivate other batches (#17132) There's a possibility of a race condition where `batch.deactivate()` is called while `current_batch !== batch`, and so it would deactivate another batch, which can lead to zombie batches that are never flushed, subsequently leading to wrong `batch_values`. This fixes that by checking if the current batch is the own batch. Fixes #17109 --- .changeset/eleven-moons-smash.md | 5 ++++ .../src/internal/client/reactivity/batch.js | 4 ++++ .../async-batch-timing/Component.svelte | 14 +++++++++++ .../samples/async-batch-timing/_config.js | 23 +++++++++++++++++++ .../samples/async-batch-timing/main.svelte | 15 ++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 .changeset/eleven-moons-smash.md create mode 100644 packages/svelte/tests/runtime-runes/samples/async-batch-timing/Component.svelte create mode 100644 packages/svelte/tests/runtime-runes/samples/async-batch-timing/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/async-batch-timing/main.svelte diff --git a/.changeset/eleven-moons-smash.md b/.changeset/eleven-moons-smash.md new file mode 100644 index 0000000000..d37d55d9fe --- /dev/null +++ b/.changeset/eleven-moons-smash.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: don't deactivate other batches diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js index 57aa185a31..84308ef3ed 100644 --- a/packages/svelte/src/internal/client/reactivity/batch.js +++ b/packages/svelte/src/internal/client/reactivity/batch.js @@ -299,6 +299,10 @@ export class Batch { } deactivate() { + // If we're not the current batch, don't deactivate, + // else we could create zombie batches that are never flushed + if (current_batch !== this) return; + current_batch = null; batch_values = null; } diff --git a/packages/svelte/tests/runtime-runes/samples/async-batch-timing/Component.svelte b/packages/svelte/tests/runtime-runes/samples/async-batch-timing/Component.svelte new file mode 100644 index 0000000000..275860ad9c --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/async-batch-timing/Component.svelte @@ -0,0 +1,14 @@ + + +

{ref_exists}

+ diff --git a/packages/svelte/tests/runtime-runes/samples/async-batch-timing/_config.js b/packages/svelte/tests/runtime-runes/samples/async-batch-timing/_config.js new file mode 100644 index 0000000000..1da4fdc0bd --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/async-batch-timing/_config.js @@ -0,0 +1,23 @@ +import { tick } from 'svelte'; +import { test } from '../../test'; + +// This test regresses against batches deactivating other batches than themselves +export default test({ + async test({ assert, target }) { + await tick(); // settle initial await + + const button = target.querySelector('button'); + + button?.click(); + await tick(); + assert.htmlEqual( + target.innerHTML, + ` +
div
+

true

+ +

+ ` + ); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/async-batch-timing/main.svelte b/packages/svelte/tests/runtime-runes/samples/async-batch-timing/main.svelte new file mode 100644 index 0000000000..8b582f41d0 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/async-batch-timing/main.svelte @@ -0,0 +1,15 @@ + + +
div
+ + + +{#if foo} +

+{/if}