fix(adapter-cloudflare): await initialization on every request to prevent race condition#15161
Merged
teemingc merged 3 commits intosveltejs:mainfrom Jan 14, 2026
Conversation
…vent race condition
🦋 Changeset detectedLatest commit: cb6e515 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
teemingc
requested changes
Jan 14, 2026
Changeset funny-pets-melt.md + version bump (patch)
Contributor
Author
|
Done! Thanks for looking at it. Let me know if you need anything else. |
teemingc
reviewed
Jan 14, 2026
teemingc
approved these changes
Jan 14, 2026
Merged
KiraPC
pushed a commit
to KiraPC/sveltekit
that referenced
this pull request
Feb 5, 2026
…veltejs#15161) * fix(adapter-cloudflare): await initialization on every request to prevent race condition * Create changeset Changeset funny-pets-melt.md + version bump (patch) * Apply suggestion from @teemingc --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com>
teemingc
added a commit
that referenced
this pull request
Feb 5, 2026
* fix(adapter-node): validate ORIGIN env var at startup (#14978) - Add parse_origin() utility to validate and normalize origin URLs - Server now fails fast with clear error for invalid ORIGIN - Automatically normalize default ports and strip path/query/hash - Add tests for origin validation * add changeset * chore(deps): update pnpm to v10.25.0 (#15043) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore: Test remote functions with full-async setup (#15033) * checkpoint * donesies * fix: lockfile * update svelte-check * add .env for test app * fix flaky test * polyfill withResolvers * chore(deps): update actions/upload-artifact action to v6 (#15057) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update pnpm to v10.26.0 (#15065) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix: make typescript an optional peer dependency (#15074) Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * docs: update Wrangler command for Cloudflare Workers testing (#15077) * chore(deps): update pnpm to v10.26.2 (#15072) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * docs: clarify remote function import (#15082) Tiny detail to make it super clear the import is for remote functions. * docs: update description for `static` folder (#15087) Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * chore: upgrade Playwright (#15089) * chore: Upgrade Playwright * checkpoint, not sure what else is going wrong * i have never been so happy to see a test failure * fix lockfile maybe * fix: remove playwright * more flaky tests * fix clicknav * fix another flaky test * improve further * another clicknav usage * chore: upgrade jws (#15095) * chore: upgrade js-yaml (#15096) * feat: expose `waitUntil` also for serverless runtime & add docs (#14725) * improvement: expose `waitUntil` also for serverless runtime & add documentation * changeset * revert previous changes and add docs * revert previous changes * change changeset --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> * chore(deps): update vitest monorepo to v4 (major) (#14789) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Tee Ming <chewteeming01@gmail.com> * fix: avoid overridden Vite config warning with Vitest 4 (#15121) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: teemingc <54401897+teemingc@users.noreply.github.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * docs: remove `$lib` path customization (#14812) * docs: remove `$lib` path customization Updated documentation for $lib import alias in SvelteKit. * Update documentation/docs/98-reference/26-$lib.md Co-authored-by: Tee Ming <chewteeming01@gmail.com> * chore: remove deprecated comment --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> * fix: use hasOwn check when deep-setting object properties (#15127) * Version Packages (#15091) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * chore: upgrade to pnpm 10.27.0 (#15128) * fix: add `has(name, value)` overload for URLSearchParams (#15076) * Add support for SearchParams.has(name, value) overload * generate changeset * Update .changeset/sharp-tires-work.md --------- Co-authored-by: Elliott Johnson <hello@ell.iott.dev> Co-authored-by: Elliott Johnson <elliott.johnson@vercel.com> * Apply suggestions from code review Co-authored-by: Tee Ming <chewteeming01@gmail.com> * fix: support instrumentation for `vite preview` (#15105) The `instrumentation.server.js` file is now imported when running `vite preview`. Unfortunately, since it's not the first thing that's imported (all of Vite's depedencies are imported first), some instrumentation that rely on monkey-patching imports might not work properly, but at least we're importing it before the server code. However, this matches the existing behaviour of `vite dev`. See: https://github.com/sveltejs/kit/blob/51214794b40b2fc533b06cb8344e84e661f90be0/packages/kit/src/exports/vite/dev/index.js#L507-L513 * chore: run pnpm dedupe (#15134) * fix: add `experimental.forkPreloads` flag (#15135) * feat: add `experimental.enhancedPreloading` flag Adds a new experimental flag to gate the use of Svelte's `fork` API for preloading. The flag defaults to `false`, disabling the fork-based preloading behavior until explicitly enabled. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * changeste * chore: rename `enhancedPreloading` to `forkPreloads` Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> * chore: fix prettier ignoring source code in with build in the name (#15133) * avoid prettier ignoring files that contain build in their name * fix global ignore ignoring directories with build name * prettier * Update .prettierignore * Update .prettierignore * Version Packages (#15129) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * breaking: remove `buttonProps` from experimental remote form functions (#14622) * breaking: remove `buttonProps` from experimental remote form functions use e.g. `<button {...myForm.fields.action.as('submit', 'register')}>Register</button>` button instead * fix * error in dev on buttonProps access * fix * fix tests * tweak * regenerate --------- Co-authored-by: Rich Harris <richard.a.harris@gmail.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> * Revert "breaking: remove `buttonProps` from experimental remote form function…" (#15143) This reverts commit 4f9870d. * feat: add env vars for keepAliveTimeout and headersTimeout (#15125) * Check for and apply timeouts for keepAlive and headers * Update docs with new timeout env vars * Add changeset for new timeouts * Run the formatter * Tweak the docs wording a bit * Check for empty vars before parsing and validating * Update documentation/docs/25-build-and-deploy/40-adapter-node.md Co-authored-by: Tee Ming <chewteeming01@gmail.com> * Add a timeout_env helper with corresponding tests * fix: convert adapter-node tests to TypeScript for project service compatibility The tsconfig include pattern with brace expansion {js,ts} was not being recognized by TypeScript's project service used by ESLint. Split into separate patterns and converted test files to TypeScript. * format --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> Co-authored-by: Elliott Johnson <hello@ell.iott.dev> * chore: Add AGENTS.md, symlink copilot instructions (#15145) * chore: Add AGENTS.md, symlink copilot instructions * clean up * Version Packages (#15146) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * chore: remove unused is_http_method helper and method set to (#15152) * fix: avoid overriding Vite default `base` when running Vitest 4 (#14866) * using same default base * add changeset * kit_base_path kit_paths_base >< * Apply suggestion from @teemingc --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> * docs: remove old references about `invalid.fieldName(...)` for issues (#15118) * Initial plan * docs: fix invalid() documentation to reflect actual API Co-authored-by: teemingc <54401897+teemingc@users.noreply.github.com> * docs: add changeset for invalid() documentation fix Co-authored-by: teemingc <54401897+teemingc@users.noreply.github.com> * Delete .changeset/brown-impalas-judge.md * Clarify usage of invalid function in validation Updated the explanation of the `invalid` function to include its origin from `@sveltejs/kit` and clarified its usage. * Update documentation/docs/20-core-concepts/60-remote-functions.md --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: teemingc <54401897+teemingc@users.noreply.github.com> Co-authored-by: Tee Ming <chewteeming01@gmail.com> Co-authored-by: Rich Harris <richard.a.harris@gmail.com> * fix: await initialization on every request to prevent race condition (#15161) * fix(adapter-cloudflare): await initialization on every request to prevent race condition * Create changeset Changeset funny-pets-melt.md + version bump (patch) * Apply suggestion from @teemingc --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> * chore: Upgrade devalue (#15172) * Merge commit from fork * ensure decoded resolved path is compared with decoded url.pathname * remote resources shouldn't be rerouted * actually it doesn't matter because reroute doesn't run anyway * add validations for node adapter host, protocol, and port * edge case * there * changeset * Update handler.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update handler.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update handler.js * Update handler.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update packages/adapter-node/src/handler.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * wrap fetch call * Apply suggestion from @teemingc --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Merge commit from fork * fix: Add length checks to remote forms * don't pre-instantiate a huge buffer * Update .changeset/thin-dodos-reply.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * move errors to util function * test: avoid preallocating remote form buffers * test: make remote form allocation regression fail on old code --------- Co-authored-by: Ottomated <otto@ottomated.net> Co-authored-by: ottomated <31470743+ottomated@users.noreply.github.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Junha Heo <hashcoko@gmail.com> * chore: Upgrade svelte (#15173) * lockfile * Version Packages (#15162) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * chore: fix lint errors (#15174) * generate types * format * chore: replace `netlify-cli` with `@netlify/dev` (#14686) * chore: replace netlify-cli with netlify dev package * update lockfile * match modern netlify build output * add doc * remove srvx because it doesn't work with node 18 * format * switch to node * fix format and lint ignore * no more srvx * try installing deno for netlify edge in github action * oops indentation * pass port in through npm script * remove port from npm script * log when server started * prettier * generate types --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> * chore: update suggestion message (#15165) * chore: upgrade dependencies (#15176) * chore: prevent two concurrent running release workflows (#15160) * Revert "Revert "breaking: remove `buttonProps` from experimental remote form …" (#15144) This reverts commit 3305022. * fix: compatibility with vite-plugin-svelte@7 (#15179) * Version Packages (#15177) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * chore: docs update on `vitePreprocess` (#15185) * fix: don't send "Vary: Accept-Encoding" header when precompress=false (#15182) * Disable sirv compression checks when precompress=false * Add changeset * Update .changeset/sweet-spoons-study.md --------- Co-authored-by: Elliott Johnson <hello@ell.iott.dev> * fix: include `hooks.server` and `hooks.universal` as entrypoints to give them stable manifest identifiers (#15178) * fix: include `hooks.server` and `hooks.universal` as entrypoints to give them stable manifest identifiers * changeset * yep * lockfile --------- Co-authored-by: Tee Ming <chewteeming01@gmail.com> * fix: Skip failing tests on async (#15189) * fix: preload links if href changes (#15191) * test * fix * changeset * docs: update cloudflare adapter package description and misc (#15164) * simplify workers and pages distinction * support workers ci in adapter auto * add todo * remove todo * improve error message * spacing * Update packages/adapter-auto/index.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * validate main key and fix relative import paths * better comment * allow spa static assets configuration * add validation error reasons * docs formatting * make compatibility date generic * mention workers ci * grammar * integrate #14871 thanks to penalosa * add test for workers_ci * format * update test * split out * chore * Update packages/adapter-cloudflare/package.json Co-authored-by: Elliott Johnson <elliott.johnson@vercel.com> --------- Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Elliott Johnson <elliott.johnson@vercel.com> * fix: ensure injected paths are prefixed with `./` (#15163) * simplify workers and pages distinction * support workers ci in adapter auto * add todo * remove todo * improve error message * spacing * Update packages/adapter-auto/index.js Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * validate main key and fix relative import paths * better comment * allow spa static assets configuration * add validation error reasons * docs formatting * make compatibility date generic * mention workers ci * grammar * integrate #14871 thanks to penalosa * add test for workers_ci * format * update test * split out adapter-auto changes * remove unrelated changes * this too --------- Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * docs: convert `picklist` options to a `const` array (#14616) * fix: prevent isr routes from handling remote function calls (#15085) (#15098) * fix: prevent isr routes from handling remote function calls (#15085) * add changeset for observability changes * fix: improve `fields` type for generic components (#14974) * fix: improves fields type for generic components * changeset * fix generated types * cleaner type for intellisense --------- Co-authored-by: Elliott Johnson <elliott.johnson@vercel.com> * Version Packages (#15186) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * chore: upgrade dependencies (#15201) * chore(deps): update dependency @netlify/edge-functions to v3 (#14772) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore: add closing angle bracket (#15205) * fix: reexport browser/dev from esm-env (#15206) * chore: upgrade netlify/dev library (#15210) * fix: ensure CSS URLs are correct when inlining client stylesheets (#15153) * add fix and test format * add unit test * changeset * add test for inlining conditionally rendered component css * format * Apply suggestion from @teemingc * Apply suggestion from @teemingc * Update css.js * handle whitespace, add some additional test cases * add failing test for assets in static dir * bump svelte * this should just work * fix lockfile * ok its working now * last fix * format * push wip * tests are passing * split tests * rename parser to parse * hoist regexes * use test.each * add test for content and comments * rename assets to paths_assets * add tests for escaped characters * add test for encoded characters * safeguard against trailing slashes * decode vite asset filenames * a bit of clean up * oops * tippex comments * tippex strings * tippex wip * harden comment and escaped character tests * account for nested app dir * bump svelte * chore: fix tippex and add test cases --------- Co-authored-by: Elliott Johnson <hello@ell.iott.dev> Co-authored-by: Rich Harris <rich.harris@vercel.com> * fix: use validated args in batch resolver (#15215) Co-authored-by: Elliott Johnson <hello@ell.iott.dev> Co-authored-by: Rishab49 <25582966+Rishab49@users.noreply.github.com> * chore(deps): update dependency set-cookie-parser to v3 (#15155) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * fix: only match rest params with matchers when the matcher matches (#15216) * chore: restore previous explicit version for magic-string dependency in package.json of public packages (#15217) * fix: emit script CSP nonces when is present if is also present (#15221) * Update pnpm-workspace.yaml (#15218) * fix(kit): properly handle percent-encoded anchors during prerendering (#15231) * fix(kit): properly handle percent-encoded anchors during prerendering * changelog * Apply suggestion from @GauBen * improve tests --------- Co-authored-by: Elliott Johnson <hello@ell.iott.dev> * feat: export type Picture in enhanced-img (#15225) * feat: export type Picture in enhanced-img This type is part of public api, so it should be reexported. Otherwise users have to explicitly add "vite-imagetools", or use hacks like `typeof import('fake.jpg?enhanced').default` This type is needed when using `import.meta.glob`, because by default it is getting typed as `unknown`. * Apply suggestion from @teemingc Co-authored-by: Tee Ming <chewteeming01@gmail.com> * tweak --------- Co-authored-by: Elliott Johnson <hello@ell.iott.dev> Co-authored-by: Tee Ming <chewteeming01@gmail.com> * handle pr review comments * fix: address PR review comments * chore: remove obsolete changesets consumed by releases * Apply suggestion from @elliott-with-the-longest-name-on-github Co-authored-by: Elliott Johnson <hello@ell.iott.dev> * cleanup * silly github --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Elliott Johnson <elliott.johnson@vercel.com> Co-authored-by: Ondrej Čierny <cierny@users.noreply.github.com> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Tee Ming <chewteeming01@gmail.com> Co-authored-by: Samuel Plumppu <6125097+Greenheart@users.noreply.github.com> Co-authored-by: Scott Wu <sw@scottwu.ca> Co-authored-by: Leon Scherer <55341374+Fruup@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: teemingc <54401897+teemingc@users.noreply.github.com> Co-authored-by: Hyunbin Seo <47051820+hyunbinseo@users.noreply.github.com> Co-authored-by: Rich Harris <richard.a.harris@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Rory Duncan <RoryDuncan@users.noreply.github.com> Co-authored-by: Elliott Johnson <hello@ell.iott.dev> Co-authored-by: Alois Klink <alois@aloisklink.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Rich Harris <rich.harris@vercel.com> Co-authored-by: Andrew Dailey <andrew@shallowbrooksoftware.com> Co-authored-by: jyc.dev <jycouet@gmail.com> Co-authored-by: Thomas LE JEUNE <thomastheyoung@gmail.com> Co-authored-by: Ottomated <otto@ottomated.net> Co-authored-by: ottomated <31470743+ottomated@users.noreply.github.com> Co-authored-by: Junha Heo <hashcoko@gmail.com> Co-authored-by: Conduitry <git@chor.date> Co-authored-by: Dominik G. <dominik.goepel@gmx.de> Co-authored-by: Patrick <Patrick@ShowYou.us> Co-authored-by: Chew Tee Ming <chew.tee.ming@nindatech.com> Co-authored-by: Richard Smith <richardmcsmith@gmail.com> Co-authored-by: Matt DeKok <matt@dekok.dev> Co-authored-by: Rishab49 <25582966+Rishab49@users.noreply.github.com> Co-authored-by: Gautier Ben Aïm <48261497+GauBen@users.noreply.github.com> Co-authored-by: blt-r <63462729+blt-r@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a race condition in the Cloudflare adapter's worker where concurrent requests during cold start can bypass server initialization.
The problem
Currently,
await initializedis inside theif (!origin)conditional:This creates a race condition:
originisundefinedifblock, setsorigin, begins awaitinginitializedawaitresolvesoriginis now set (truthy), skips theifblock entirelyThe fix
Move
await initializedoutside the conditional:Why this is safe
The
Server.init()method inpackages/kit/src/runtime/server/index.jsuses a memoization pattern:The
??=operator ensures the initialization logic only executes once:init_promiseisundefined, so the async IIFE runs and its promise is storedinit_promiseis already set, returns the same (resolved) promise immediatelyThis means awaiting
initializedon every request:The initialization logic is never re-executed. The only "cost" is microtask scheduling from awaiting a resolved promise, which is negligible.
Reproduction
While difficult to reproduce reliably, this can manifest during:
Symptoms may include undefined behavior, errors from uninitialized server state, or inconsistent responses.