Repository navigation
Description
Describe the bug
With experimental.async, an await inside {#if value} can run again with a value the {#if} rules out.
The setup: value starts as null. Inside {#if value} we await query(value), where query() returns a cached, lazy thenable that reads a $state field in its then getter and writes it when it settles. SvelteKit's remote query has exactly this shape, which is how we hit it: a remote query inside an {#if} got called with null, the server's validation answered 400, and the rejection was unhandled.
Steps: set value twice quickly ('a', then 'b'), so both updates are pending at once. Then let a settle, then b. When b settles, its loading = false write starts a new batch. That batch does not include either pending value update, so it reads value as null. It still walks into the {#if} branch, which so far only exists offscreen for the two pending batches, and re-runs the await expression there. query(null) gets called.
Two visible effects:
query(null)is called (in our app: a network request with an argument the server rejects).bis not shown until that stalenullthenable settles. If it never settles,bnever shows.
It needs all of: two overlapping updates to the {#if} value, and a thenable whose then getter reads state that is later written. With one update, or a plain promise, or no state read/write in the thenable, it does not happen.
The branch's {#if} condition is never falsy while it is on screen, so the await should never see null.
Possibly related, but not the same: #19109 (there the outgoing, on-screen branch is walked by a second batch that writes the same source; here the incoming, offscreen branch is walked by an unrelated batch that writes a different source).
I checked the batch-views branch from #19030, and the repro below no longer calls query(null) there, and shows b right away. Happy to contribute the regression test if that helps (it is written in the runtime-runes sample style and fails on main).
Reproduction
Click "toggle" twice, then "resolve" twice. The console logs query(a), query(b), query(null), and b does not appear. Click "resolve" a third time (that settles the stale null one) and b appears.
App.svelte:
<script>
import { Query } from './query.svelte.js';
const queue = [];
const cache = new Map();
function query(value) {
console.log(`query(${value})`);
let q = cache.get(value);
if (!q) cache.set(value, (q = new Query(value, queue)));
return q;
}
let value = $state(null);
</script>
<p>Click "toggle" twice, then "resolve" twice.</p>
<button onclick={() => (value = value === 'a' ? 'b' : 'a')}>toggle</button>
<button onclick={() => queue.shift()?.()}>resolve</button>
{#if value}
<p>{await query(value)}</p>
{/if}query.svelte.js:
// A lazy thenable that reads state in its `then` getter and writes it
// when it settles. SvelteKit's remote `query` has this shape.
export class Query {
loading = $state(true);
#promise = null;
constructor(value, queue) {
this.value = value;
this.queue = queue;
}
get then() {
this.loading;
const promise = (this.#promise ??= new Promise((fulfil) => {
this.queue.push(() => {
this.loading = false;
fulfil(this.value);
});
}));
return promise.then.bind(promise);
}
}Expected: query(a), query(b), and b on screen after the second "resolve".
Actual: query(a), query(b), query(null), and nothing on screen until the null thenable settles.
Logs
query(a)
query(b)
query(null)The stack of the query(null) call (from the same component in a test run on main):
at query (App.svelte)
at deriveds.js (async_derived)
at update_reaction (runtime.js)
at update_effect (runtime.js)
at Batch.#traverse (batch.js)
at Batch.#process (batch.js)
at Batch.flush (batch.js)
at run_micro_tasks (task.js)System Info
System:
OS: macOS 27.0.1
CPU: (16) arm64 Apple M4 Max
Binaries:
Node: 26.4.0
pnpm: 12.16.1
npmPackages:
svelte: 5.57.3 (also reproduced on main at 0156c5f)Severity
annoyance
Activity
svelte-triage-bot commentedon Nov 12, 2026
I looked into this but could not produce a safe, verified fix.
An existing open PR already addresses this regression: "#19030". The issue author verified that its batch-views branch prevents query(null) and displays b after the second resolve. Per the prior-art rule, no competing patch was attempted; the regression should be covered in that PR.
Details
awaitinside{#if value}re-runs withvalue === nullwhen a later state write lands while two updates are pending #19253".: The issue is open with no comments. Its reproduction describes the unrelated loading-state write re-entering an offscreen incoming branch. The reporter explicitly verified that the batch-views branch avoids query(null) and renders b immediately.