Skip to content

Commit a81f965

Browse files
authored
fix: reject pending async deriveds on discard (#18308)
The rejects all async deriveds of a batch as `OBSOLETE`, so they don't hang around and bail early without triggering the batch. If we don't do this, an async derived can trigger the already done batch, which schedules an effect that is never flushed. Because it is never flushed the branches it touched on its way up are never cleared, and so anything else in that subtree is now unreactive.
1 parent 3ef761b commit a81f965

4 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎.changeset/warm-cougars-behave.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: reject pending async deriveds on discard

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { set_signal_status } from './status.js';
4141
import { legacy_is_updating_store } from './store.js';
4242
import { invariant } from '../../shared/dev.js';
4343
import { log_effect_tree } from '../dev/debug.js';
44+
import { OBSOLETE } from './deriveds.js';
4445

4546
/** @type {Batch | null} */
4647
let first_batch = null;
@@ -511,6 +512,10 @@ export class Batch {
511512
if (d) deferred.promise.then(d.resolve).catch(d.reject);
512513
}
513514

515+
// Clear them or else those that are still pending might get rejected on discard (after merged-into batch is done).
516+
// This can happen when batch Y merged into X and Y has a pending boundary and therefore still-pending async deriveds inside.
517+
batch.async_deriveds.clear();
518+
514519
// Mark is not guaranteed not touch these, so we transfer them
515520
this.transfer_effects(batch.#dirty_effects, batch.#maybe_dirty_effects);
516521

@@ -629,6 +634,10 @@ export class Batch {
629634
for (const fn of this.#discard_callbacks) fn(this);
630635
this.#discard_callbacks.clear();
631636

637+
for (const deferred of this.async_deriveds.values()) {
638+
deferred.reject(OBSOLETE);
639+
}
640+
632641
this.#unlink();
633642
this.#deferred?.resolve();
634643
}
@@ -677,12 +686,15 @@ export class Batch {
677686
}
678687
}
679688

680-
if (!batch.#started) continue;
689+
var current = [...batch.current.keys()].filter(
690+
(source) => !(/** @type {[any, boolean]} */ (batch.current.get(source))[1])
691+
);
692+
693+
// If not started yet or no sources to update (which is e.g. possible for the very first batch) then bail
694+
if (!batch.#started || current.length === 0) continue;
681695

682696
// Re-run async/block effects that depend on distinct values changed in both batches (ignoring deriveds)
683-
var others = [...batch.current.keys()].filter(
684-
(s) => !(/** @type {[any, boolean]} */ (batch.current.get(s))[1]) && !this.current.has(s)
685-
);
697+
var others = current.filter((source) => !this.current.has(source));
686698

687699
if (others.length === 0) {
688700
if (is_earlier) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { tick } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target, logs }) {
6+
await tick();
7+
const [fork, real, resolve] = target.querySelectorAll('button');
8+
9+
fork.click();
10+
await tick();
11+
resolve.click();
12+
await tick();
13+
assert.htmlEqual(
14+
target.innerHTML,
15+
`
16+
0
17+
<button>fork</button>
18+
<button>real</button>
19+
<button>resolve</button>
20+
`
21+
);
22+
assert.deepEqual(logs, [0]);
23+
24+
real.click();
25+
await tick();
26+
resolve.click();
27+
await tick();
28+
assert.htmlEqual(
29+
target.innerHTML,
30+
`
31+
1
32+
<button>fork</button>
33+
<button>real</button>
34+
<button>resolve</button>
35+
`
36+
);
37+
assert.deepEqual(logs, [0, 1]);
38+
}
39+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script>
2+
import { fork } from "svelte";
3+
4+
let count = $state(0);
5+
let queued = [];
6+
7+
function push(v) {
8+
if (!v) return v;
9+
10+
return new Promise((resolve) => {
11+
queued.push(() => resolve(v));
12+
});
13+
}
14+
15+
$effect(() => {
16+
console.log(count);
17+
})
18+
</script>
19+
20+
{await push(count)}
21+
<button onclick={() => fork(() => count++).discard()}>fork</button>
22+
<button onclick={() => count++}>real</button>
23+
<button onclick={() => queued.shift()?.()}>resolve</button>

0 commit comments

Comments
 (0)