Skip to content

Commit 3604fda

Browse files
fix: avoid other batches running with queued root effects of main batch (#17145)
* fix: avoid other batches running with queued root effects of main batch When `#traverse_effect_tree` runs, it can execute block effects, which in turn can create effects which are scheduled, which means `queued_root_effects` is now filled again. We didn't take that into account and assumed that in `#commit` we would have no queued root effects. As a result another batch could wrongfully run with the queued root effects of the main batch. That in turn can mean that `skipped_effects` is different on the other batch, leading to some branches not getting traversed into. As a result part of the tree is marked clean while further below batches are still not marked clean. That then breaks reactivity as soon as you schedule an effect in this still-not-clean sub tree, as it will not bubble all the way up to the root, since it comes across a not-clean branch, assuming something was already scheduled. The fix is simple: Stash the queued root effects before rebasing branches. Fixes #17118 * add note to self --------- Co-authored-by: Rich Harris <rich.harris@vercel.com>
1 parent fe50e58 commit 3604fda

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

.changeset/sixty-glasses-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: avoid other batches running with queued root effects of main batch

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export let previous_batch = null;
6868
*/
6969
export let batch_values = null;
7070

71+
// TODO this should really be a property of `batch`
7172
/** @type {Effect[]} */
7273
let queued_root_effects = [];
7374

@@ -171,6 +172,8 @@ export class Batch {
171172

172173
for (const root of root_effects) {
173174
this.#traverse_effect_tree(root, target);
175+
// Note: #traverse_effect_tree runs block effects eagerly, which can schedule effects,
176+
// which means queued_root_effects now may be filled again.
174177
}
175178

176179
if (!this.is_fork) {
@@ -418,6 +421,10 @@ export class Batch {
418421
// Re-run async/block effects that depend on distinct values changed in both batches
419422
const others = [...batch.current.keys()].filter((s) => !this.current.has(s));
420423
if (others.length > 0) {
424+
// Avoid running queued root effects on the wrong branch
425+
var prev_queued_root_effects = queued_root_effects;
426+
queued_root_effects = [];
427+
421428
/** @type {Set<Value>} */
422429
const marked = new Set();
423430
/** @type {Map<Reaction, boolean>} */
@@ -436,9 +443,10 @@ export class Batch {
436443

437444
// TODO do we need to do anything with `target`? defer block effects?
438445

439-
queued_root_effects = [];
440446
batch.deactivate();
441447
}
448+
449+
queued_root_effects = prev_queued_root_effects;
442450
}
443451
}
444452

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
let open = $state(false);
3+
let menuOptionsEl = $state(null);
4+
</script>
5+
6+
<button onclick={() => open = !open}>
7+
toggle
8+
9+
{#if open}
10+
<!-- bind:this uses effect, which is scheduled, causing queued_root_effects to be filled again -->
11+
<span bind:this={menuOptionsEl}>
12+
A
13+
</span>
14+
{/if}
15+
</button>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
B
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target }) {
6+
const [fork, commit, toggle] = target.querySelectorAll('button');
7+
8+
fork.click();
9+
await tick();
10+
assert.htmlEqual(
11+
target.innerHTML,
12+
`
13+
<button>fork</button>
14+
<button>commit</button>
15+
<button>toggle</button>
16+
`
17+
);
18+
19+
toggle.click();
20+
await tick();
21+
assert.htmlEqual(
22+
target.innerHTML,
23+
`
24+
<button>fork</button>
25+
<button>commit</button>
26+
<button>toggle <span>A</span></button>
27+
`
28+
);
29+
30+
toggle.click();
31+
await tick();
32+
assert.htmlEqual(
33+
target.innerHTML,
34+
`
35+
<button>fork</button>
36+
<button>commit</button>
37+
<button>toggle</button>
38+
`
39+
);
40+
41+
toggle.click();
42+
await tick();
43+
assert.htmlEqual(
44+
target.innerHTML,
45+
`
46+
<button>fork</button>
47+
<button>commit</button>
48+
<button>toggle <span>A</span></button>
49+
`
50+
);
51+
52+
commit.click();
53+
await tick();
54+
assert.htmlEqual(
55+
target.innerHTML,
56+
`
57+
<button>fork</button>
58+
<button>commit</button>
59+
B
60+
`
61+
);
62+
}
63+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<script lang="ts">
2+
import { fork } from 'svelte';
3+
import A from './A.svelte';
4+
import B from './B.svelte';
5+
6+
let open = $state(true);
7+
let f;
8+
</script>
9+
10+
11+
<button onclick={() => {
12+
f = fork(() => {
13+
open = !open;
14+
})
15+
}}>fork</button>
16+
<button onclick={() => {
17+
f.commit()
18+
}}>commit</button>
19+
20+
{#if open}
21+
<A />
22+
{:else}
23+
<B />
24+
{/if}

0 commit comments

Comments
 (0)