Skip to content

Async: await inside {#if value} re-runs with value === null when a later state write lands while two updates are pending #19253

Open

Description

@PuruVJ

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:

  1. query(null) is called (in our app: a network request with an argument the server rejects).
  2. b is not shown until that stale null thenable settles. If it never settles, b never 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

Playground: https://svelte.dev/playground?version=5.57.3#H4sIAAAAAAAAE3VU72-bMBD9V260EkbK4Ht-qtrHadKqfSyV4hATvDmG2kfTDPG_72xDSNr1E3Dne-_O9x5dpPlRRPOI27Mu5tDdybKHneG6qMCIr6bVcJJYAVYCXrlqBUgE0yphoW4xmkXIpTpJvY_mJVdWzKJSUjKaP3URnhsH7QJ0cCB6aJrUvgqFLrbjVvwvXtQahUaCiZa2MLLBda5zlMemNggdPLbCnKGH0tRHiNPsxX0P5elvGy_oNJ0nGItASWp7BU_PiylY8KJyQS1O8IM3LBlKylYXKGsNHpL5kRPoXCqU1kqkqj6wbThw3_kjfbIdEXJUgkgJ23OkB4EDzCKkZQnsy0sypO2YngF7GRp6nLhnof0kGauNwNZQe_6zD5SOMCxnBfcWOQqmW6VcyTKb7k8vm_U3JYs_kEdYHw5K5BHgSRbEQvvVFDaCBny9xNNl1oTKXYtIt1LrwgGsOpbAag1sZB2eqxXEPIYNxLsY5u496deBapkFiPWnYH7Q1FayRJZsUkalQztXtbl2Eg18bnqkmTp-4hJvNtaHxruM5JxrJ1PxhtEcDVXNPlHmOxHdyvNj8kqjWQYPoPjfs79GvlOCXjj5RPC9Bb8QkJqcY2HrTmyBRIHCANd7OBmJ5CaJuSack9sDDUOyoLXaFH55yu8SY0t4x5qggvS2UHFLPJIYKt6INNfizfujUNzawSNeuarme6kPkzrcPQRB3TVkIWm9FUgy18YxbYG1uZXh6ARHm94sf3GVGB3nn9dCpbH9FbFboKG_i4GCRafOmD91aXWzCTb5Gb4ZK1tFq_QiGmCvG0mb1laM3aZvmYnD_70WUzpgsmnQ5JLsx9c-mUw_-HLoMXVTpjv6L7Ihkoz38EGPz_0_0mP-7IYFAAA

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

  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 12, 2026
  2. svelte-triage-bot commented on Nov 12, 2026

    @svelte-triage-bot
    Contributor

    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
    • Loaded the current issue and comments through the GitHub connection: "Async: await inside {#if value} re-runs with value === null when 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.
    • Inspected the prewarmed Svelte checkout, repository contribution instructions, and async batch traversal/value application code.: The checkout is clean at 0156c5f, the same main revision on which the reporter reproduced the bug. No tracked files were edited.
    • Searched prior art with gh pr list and inspected "fix: batch views of reactions #19030".: PR fix: batch views of reactions #19030, “fix: batch views of reactions,” is open and not a draft, with head branch batch-views. It introduces batch-local reaction records and dependency write-version comparisons—the existing approach that the reporter verified resolves this reproduction. Stopped rather than creating a competing patch. No reproduction or verification tests were run.
  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 12, 2026
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