From bc82a55647745db00d2c3e565e6b1f4125d6e391 Mon Sep 17 00:00:00 2001
From: Simon H <5968653+dummdidumm@users.noreply.github.com>
Date: Wed, 29 Apr 2026 20:57:10 +0200
Subject: [PATCH] fix: ensure scheduled batch is flushed if not obsolete
(#18131)
The logic of checking that the current batch is still the generated one
is flawed. If microtasks align the current batch can be a different
value even if the batch still need to be flushed. This therefore
switches the heuristic to what it actually should express: "has this
batch run already?"
Fixes #18126 (because the batch isn't running, it later runs into the
invariant)
---
.changeset/many-pandas-add.md | 5 +++++
.../src/internal/client/reactivity/batch.js | 2 +-
.../_config.js | 15 +++++++++++++++
.../main.svelte | 18 ++++++++++++++++++
4 files changed, 39 insertions(+), 1 deletion(-)
create mode 100644 .changeset/many-pandas-add.md
create mode 100644 packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/_config.js
create mode 100644 packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/main.svelte
diff --git a/.changeset/many-pandas-add.md b/.changeset/many-pandas-add.md
new file mode 100644
index 0000000000..85de7acb35
--- /dev/null
+++ b/.changeset/many-pandas-add.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: ensure scheduled batch is flushed if not obsolete
diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js
index 82be1d1e8d..7adf3be00c 100644
--- a/packages/svelte/src/internal/client/reactivity/batch.js
+++ b/packages/svelte/src/internal/client/reactivity/batch.js
@@ -716,7 +716,7 @@ export class Batch {
if (!is_flushing_sync) {
queue_micro_task(() => {
- if (current_batch !== batch) {
+ if (!batches.has(batch) || batch.#pending.size > 0) {
// a flushSync happened in the meantime
return;
}
diff --git a/packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/_config.js b/packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/_config.js
new file mode 100644
index 0000000000..dea121c456
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/_config.js
@@ -0,0 +1,15 @@
+import { tick } from 'svelte';
+import { test } from '../../test';
+
+// Ensure that microtask timing doesn't influence whether or not a scheduled batch is flushed.
+// Timing can be such that the current_batch is reset before the scheduled flush runs, which
+// would cause the flush to skip without the fix.
+export default test({
+ async test({ assert, target }) {
+ const [btn] = target.querySelectorAll('button');
+
+ btn.click();
+ await tick();
+ assert.htmlEqual(target.innerHTML, '1 1');
+ }
+});
diff --git a/packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/main.svelte b/packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/main.svelte
new file mode 100644
index 0000000000..ebfbf4ca4e
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/async-state-updates-microtask-separated/main.svelte
@@ -0,0 +1,18 @@
+
+
+{#if a}
+ {@const toShow = await a}
+ {toShow}
+ {b}
+{:else}
+
+{/if}