Module-level state in SvelteKit leaks between requests (594 of 600)
Keep the signed-in user in module-level state on the server and every request shares it. One request at a time, nothing goes wrong. With ten in flight, almost every page carried another user's name. Here is the measurement, why it happens, and where per-request data belongs.
The pattern
A module is evaluated once per server process. A $state object in a .svelte.ts file, a store, or a plain let is therefore one object for every request that process handles.
Write the current user into it during a request, and the next request can render with that user. It looks
like idiomatic Svelte 5, and the SvelteKit docs do warn about it: "it's important not to store data in shared
variables". It still ships, because every test you run alone passes.
// src/lib/unsafe/session.svelte.ts
export const session = $state<{ name: string | null }>({ name: null });
// src/routes/leak/module-state/+layout.server.ts
// ANTI-PATTERN: "remember the signed-in user globally, so any component can read it".
export const load = async ({ locals }) => {
session.name = locals.user?.name ?? null;
return {};
};
// src/routes/safe/props/+layout.server.ts
// SAFE: per-request data leaves the load function as data, never through a module.
export const load = async ({ locals }) => ({ name: locals.user?.name ?? null });
How it was measured
A small SvelteKit app written for this topic, with 15 routes: the ways of keeping the user in shared state
that leak ($state, let, store, hook, layout component), and seven safe ways. Production build, served by node build. Each request comes from a different
simulated user and the data call waits 20–80 ms, like a database would. A script keeps N requests in flight
(3 rounds of 200 = 600 requests; 150 for one at a time) and counts every page whose HTML shows a name that
is not the requester's. JavaScript stays off: this is the server HTML, the one that caches, link previews and
slow phones get.
Pages that showed another user's name
| Where the user is kept | 1 at a time | 10 in flight | 50 in flight | 200 in flight |
|---|---|---|---|---|
| module $state | 0 / 150 | 594 / 600 | 597 / 600 | 597 / 600 |
| module let | 0 / 150 | 594 / 600 | 597 / 600 | 597 / 600 |
| module store | 0 / 150 | 592 / 600 | 595 / 600 | 597 / 600 |
| set in hook, read by helper | 0 / 150 | 595 / 600 | 596 / 600 | 597 / 600 |
| set in layout component, read after await (async SSR) | 0 / 150 | 594 / 600 | 597 / 600 | 597 / 600 |
| set in layout component (sync render) | 0 | 0 | 0 | 0 |
| all 7 safe routes | 0 | 0 | 0 | 0 |
"N in flight" is a stress test and is labelled as one. The plain let and the hook-set variable leak the same way as $state: this is how JavaScript modules work on a server, not a Svelte bug.
Requests arriving at a realistic rate
| /leak/module-state, open loop (Poisson arrivals) | Another user's name |
|---|---|
| 1 request/s for 180 s | 6 / 180 (3.3%) |
| 10 requests/s for 30 s | 123 / 300 (41%) |
| 100 requests/s for 10 s | 978 / 1,000 (97.8%) |
| safe route (props), 100 requests/s | 0 / 1,000 |
At one request per second, 6 of 180 pages (3.3 %) showed another user. The rate depends on your traffic and on how long your data calls take; it is not zero on a small site.
What a browser shows
In 10 of 10 browser trials with 20 other users loading the same dashboard, the server HTML showed another user's name for the test user who made the request (10 of 10). After hydration the page said "Not signed in" (10 of 10), so the wrong name is in the HTML, and the browser then overwrites it. A public page that only reads the global greeted 20 of 20 anonymous visitors with a previous user's name, one request at a time.
Where per-request data belongs
The two rules that are free in the demo README, and the only two you need to stop the leak:
- Never write per-user or per-request data into module-level state on the server. Not into
$state, not into a store, not into alet, not from a hook. - Pass per-request data as data. Return it from
load; read it throughpage.dataor props. On the server, keep it inevent.localsand reach the current request withgetRequestEvent().
Module-level state is fine for values that are the same for every user, and for state that is only written in
the browser. The seven safe routes in the demo (props, page.data, context created per request, locals + getRequestEvent(), and others) showed 0 wrong pages at 10, 50 and 200 in
flight and at 100 requests per second.
Serverless platforms were not measured here (adapter-node only). Where an instance serves more than one request,
the read-only case above applies: it needs no concurrency at all. With experimental async SSR, treat every read of shared state after an await as unsafe: the "set in layout component, read after await" row above is that case.
Rerun it
The demo (MIT) with the measure script, the open-loop script and the 15 tests (T1–T15) goes public before sales open soon; the waitlist gets the link first. Until then every number above is on the topic page with the versions and the date, and the result file is quoted there.
What I haven't measured: how often AI assistants write this pattern. What I measured: what it does once it ships, and which fix holds. The full write-up, with the anonymous-visitor case, async SSR, search patterns for your codebase and a drop-in concurrency test, is the guide for this topic (sales open soon).