Skip to content

Async: an update applied while a new boundary's read is pending is undone by the next unrelated update, and the page never recovers #19233

Open

Description

@anton-osphim

Describe the bug

With experimental.async, an update that mounts a new <svelte:boundary> with a pending snippet is applied to the DOM at once, but its batch stays linked until the boundary's reads settle. While it is linked, any later batch that doesn't write one of the same sources undoes its writes in apply(). Effects that later batch re-runs render from the state before the first update, and nothing re-runs them when the first batch commits. The page keeps showing the old state for good.

In the reproduction below, step 1 writes rows and mounts a row whose await is pending. The page correctly shows rows: 1, saveable: true. Step 2 replaces rows[0], a different source, which dirties the saveable derived and the {#each}. That batch reads rows through batch_values, which apply() has set back to []. The row disappears, and the page shows rows: 0, saveable: false with Save disabled. Step 3 resolves the read and the first batch commits, but the page stays at rows: 0, although rows holds one row.

  • Expected after step 3: a (per hour), rows: 1, saveable: true, Save enabled.
  • Actual: rows: 0, saveable: false, Save disabled, no row.
  • Control: step 1 then step 3, without step 2, ends correctly at a (per minute), rows: 1, saveable: true.

We hit this in an app where loading a saved view mounts rows whose reads are slow. An edit made while they load left Save and the other actions disabled until a reload.

Why it happens

As far as I can tell:

  1. In #process, the boundary's reads count toward #pending but not toward #blocking_pending, so #is_deferred() is false. The batch flushes its effects, but because #pending > 0 it is not unlinked or committed.
  2. In a later batch's apply(), the loop over first_batch … #next undoes every non-intersecting batch's changes from batch.previous. That includes this batch, whose values are already on screen.
  3. When the first batch commits after its reads resolve, the effects the later batch rendered from the undone values are not dirty, so they are never re-run.

This looks related to #19047 (a mixed-version UI that persists after all promises resolve) and #19157 (a value leaking between batches), but the trigger here is a flushed, still-linked batch, not a derived's first read. #18997 makes it easier to hit, since re-runs multiply while batches overlap.

What we do for now

We ship a local patch that skips undoing a batch whose effects have already been flushed:

 	#started = false;
+	#flushed = false;
@@ #process()
 		flush_queued_effects(render_effects);
 		flush_queued_effects(effects);
 		previous_batch = null;
+		this.#flushed = true;
@@ apply()
-			if (batch === this || batch.is_fork) continue;
+			if (batch === this || batch.is_fork || batch.#flushed) continue;

It fixes the reproduction and our app, and the rest of our test suite passes with it. It is not a complete fix. If a later batch merges into a flushed batch (#merge → #process) and that re-process is deferred by new blocking work, the merged values are visible to other batches before they are on screen. We also tried tracking the flushed values per source, so that only drawn values are protected and later merged ones are still undone. That broke a different flow in our app, so we didn't pursue it.

Reproduction

Playground (5.57.3). Click 1, 2, 3 in order.

<script>
	let rows = $state([]);
	// A derived over the rows, like "is there anything to save?"
	const saveable = $derived(rows.filter((row) => row.name).length > 0);

	const gate = Promise.withResolvers();
	const slow = (value) => gate.promise.then(() => value);
</script>

<p>
	<button onclick={() => (rows = [{ name: 'a', hourly: false }])}>1. load a row</button>
	<button onclick={() => (rows[0] = { ...rows[0], hourly: !rows[0].hourly })}>2. edit the row</button>
	<button onclick={() => gate.resolve()}>3. resolve its read</button>
</p>

{#each rows as row}
	<!-- a NEW boundary with a pending snippet: its read does not block the batch that mounted it -->
	<svelte:boundary>
		{#snippet pending()}
			<p>loading {row.name}…</p>
		{/snippet}
		<p>{await slow(row.name)} ({row.hourly ? 'per hour' : 'per minute'})</p>
	</svelte:boundary>
{/each}

<p>
	rows: {rows.length}, saveable: {saveable}
	<button disabled={!saveable}>Save</button>
</p>

Logs

No errors or warnings; the stale state is rendered silently.

System Info

svelte: 5.57.3 (playground); also 5.57.2 with @sveltejs/kit 2.70.3 in our app
compilerOptions.experimental.async: true
Browsers: Brave (Chromium) on macOS; Chromium headless (Playwright)

Severity

annoyance

Activity

  1. added
    triage-botThe triage bot is working on this issue. That means it tries to reproduce it, then tries to fix it.
    on Nov 9, 2026
  2. svelte-triage-bot commented on Nov 9, 2026

    @svelte-triage-bot
    Contributor

    I reproduced the issue, but could not produce a safe, verified fix.

    What I tried

    Why I stopped
    Stopped because an open PR already fixes the reproduced bug: “https://github.com/sveltejs/svelte/pull/19030” (fix: batch views of reactions). The exact regression fails in both rendering modes on main and passes in both on that PR’s current head, c8513b9. Its solution publishes flushed writes independently of the linked batch’s outstanding async ownership, avoiding the reporter’s unsafe blanket exemption for ever-flushed batches. A competing product patch is unnecessary; the regression can be incorporated into #19030.

  3. removed
    triage-botThe triage bot is working on this issue. That means it tries to reproduce it, then tries to fix it.
    on Nov 9, 2026
  4. miguelhoyoabascal-code commented on Nov 13, 2026

    @miguelhoyoabascal-code

    Another trigger for the same thing, without {#each}: a {#key} block with an await, and a second unrelated write one microtask later (in our SvelteKit app this is replaceState updating page.url right after a debounced filter value changes).

    <script>
            let asked = $state("A");
            let url = $state("A");
    
            function pick(next) {
                    asked = next;
                    queueMicrotask(() => (url = next));
            }
    
            const load = (key) => new Promise((r) => setTimeout(() => r(`data for ${key}`), 50));
    </script>
    
    <button onclick={() => pick("A")}>A</button>
    <button onclick={() => pick("B")}>B</button>
    
    <p>asked: {asked} · url: {url}</p>
    
    {#key asked}
            <svelte:boundary>
                    {#snippet pending()}<p>loading…</p>{/snippet}
                    <p>{await load(asked)}</p>
            </svelte:boundary>
    {/key}

    Clicking B, then A, then B: the keyed block and url update, but asked: stays one step behind every time (asked: A · url: B · data for B, then asked: B · url: A · data for A, …). Writing url = next synchronously instead of in a microtask renders correctly. Svelte 5.57.3, Chrome 156.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions