Skip to content

fix: share one Cloudflare platform proxy between Vite servers - #17368

Merged
teemingc merged 3 commits into
sveltejs:mainfrom
glw907:fix/cloudflare-platform-proxy-ownership
Oct 14, 2026
Merged

teemingc merged 3 commits into
sveltejs:mainfrom
glw907:fix/cloudflare-platform-proxy-ownership

Conversation

@glw907

@glw907 glw907 commented Oct 6, 2026

Copy link
Copy Markdown

closes #17344

virtual_workers_module() keeps one platform proxy on globalThis.__sveltekit_cloudflare_platform, but nothing records which servers use it. That breaks two ways when more than one Vite server runs in the same process:

This stores the pending getPlatformProxy() promise and a server count on a second global, __sveltekit_cloudflare_platform_setup. Each setup increments the count and awaits the shared promise; each close decrements it, and the close that reaches zero disposes the proxy. A restarting server is set up before the old one closes, so closeServer no longer checks reason. If getPlatformProxy() rejects, the shared state is cleared so the next server can retry.

As with the old early return, the first server's adapter options configure the shared proxy. One edge case remains: if a later plugin's configureServer throws while a server is being created, that server has already incremented the count but is never closed, so the count does not return to zero.

#16705 and #16811 touch the same function and will need a rebase.

Adds tests to index.spec.js for a second server closing while the first stays open, two servers created concurrently, and a failed start followed by a retry. The first two fail without this change. The existing restart test still passes.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

pnpm lint, pnpm check and the adapter's unit tests pass. The adapter's e2e suite could not run locally because Playwright's chrome channel is not installed.

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Track how many servers set up the proxy and dispose it only when the
last one closes, so a short-lived second server no longer disposes the
proxy the dev server is using, and concurrent servers start a single
proxy instead of one each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pkg-svelte-dev

pkg-svelte-dev Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from ad1c49b:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/ad1c49baba331c5a1fdbb77643975736cb05e45a

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/17368

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ad1c49b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/adapter-cloudflare Patch

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

@ottomated

Copy link
Copy Markdown
Contributor

Looks good once the tests pass and merge conflicts are resolved.

@teemingc teemingc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above comment

teemingc and others added 2 commits October 14, 2026 01:07
Preserve the Deno caches descriptor fix when sharing the Cloudflare platform proxy. Gate the concurrent rejection test on both servers awaiting the proxy and cover immediate rejection separately.

Co-authored-by: Copilot App <[email protected]>

@teemingc teemingc left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Note that changing the config and starting a separate dev server now reuses the old proxy, so new config settings won't be reflected. However, I've filed that as a separate issue #17429

@teemingc
teemingc merged commit a2ced7d into sveltejs:main Oct 14, 2026
36 of 37 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 14, 2026
teemingc added a commit that referenced this pull request Oct 14, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @sveltejs/[email protected]

### Minor Changes

- feat: support `report-sha256`/`report-sha384`/`report-sha512` CSP
hash-reporting keywords
([#17398](#17398))

- feat: emit a `sveltekit.endpoint` span for `+server` handlers when
server tracing is enabled
([#17325](#17325))

### Patch Changes

- fix: don't treat every module as server-only when the project is
inside a `server` directory
([#17376](#17376))

- fix: continue deployment recovery when service worker updates fail
([#17440](#17440))

- fix: infer prerendered MIME types from generated files
([#17381](#17381))

- fix: leave generated files alone when their contents are unchanged
([#17310](#17310))

- fix: leave focus in the parent page when an app in an unfocused iframe
starts or navigates
([#17297](#17297))

- fix: don't warn about Storybook's Vite plugins that use
`transformIndexHtml`
([#17373](#17373))
## @sveltejs/[email protected]

### Patch Changes

- fix: start the dev server on runtimes such as Deno that define
`caches` as a getter-only global
([#17401](#17401))

- fix: start one platform proxy for concurrent Vite servers and keep it
alive until the last one closes
([#17368](#17368))

- fix: support `Span#setStatus` and `Span#updateName` on local emulation
of Cloudflare `tracing`
([#17400](#17400))
- Updated dependencies:
  - @sveltejs/[email protected]
## @sveltejs/[email protected]

### Patch Changes

- fix: keep the detected Bun minor in the resolved runtime
([#17331](#17331))
- Updated dependencies:
  - @sveltejs/[email protected]
## @sveltejs/[email protected]

### Patch Changes

- fix: emit component types for the installed Svelte version, rather
than the oldest one the `svelte` dependency range allows
([#17330](#17330))

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tee Ming <[email protected]>
longrunningprocess added a commit to CanIL-CA/tabitha that referenced this pull request Oct 14, 2026
…uard (#198)

8.0.1 starts one shared platform proxy for concurrent Vite servers (sveltejs/kit#17368), which fixes the SQLITE_BUSY_RECOVERY race (kit#17344) the guard worked around. Measured without the guard: all unit tests pass and run time is unchanged.

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
teemingc added a commit that referenced this pull request Oct 16, 2026
…7432)

## Summary

Closes #17429.

The Cloudflare adapter's Vite plugin currently reads the wrangler config
once at `configureServer` time and never re-reads it. Edit
`wrangler.toml`/`wrangler.jsonc` and the platform proxy keeps serving
the stale config until the user manually restarts the dev server.

This PR watches the resolved wrangler config path on the Vite dev
server's watcher. When the file is edited, we call `server.restart()`.
The existing teardown (`closeServer` → `dispose`) tears the shared
platform proxy down, and `configureServer` → `setup` creates a fresh one
— `getPlatformProxy(options)` re-reads the config file on each call, so
the restart alone is enough to pick up the new config.

## What changed

- **`packages/adapter-cloudflare/index.js`** — `setup` now accepts the
Vite dev/preview server and, when it's a dev server with a `watcher`,
resolves the wrangler config path via `unstable_readConfig({ config:
options.configPath })` and installs a `change` listener that calls
`server.restart()` when the resolved config file is touched. Preview
servers have no `watcher`, so they skip the hookup. Resolution via
`unstable_readConfig` means we honour the same resolution rules wrangler
itself uses (`wrangler.toml`, `wrangler.jsonc`, `--config`, etc.).
- **`packages/adapter-cloudflare/index.spec.js`** — two new tests:
- `restarts the dev server when the wrangler config file changes` —
mocks `unstable_readConfig` to return a resolved path, asserts
`server.restart()` is called on the matching change and not on any other
file.
- `does not install a config watcher when wrangler cannot resolve a
config path` — proves we're defensive about the `{ configPath: undefined
}` case.

## Local verification

```
$ ./node_modules/.bin/vitest run index.spec.js
 Test Files  1 passed (1)
      Tests  8 passed (8)
```

6 existing tests + 2 new, all green. Install was `pnpm install --filter
@sveltejs/adapter-cloudflare` on Node 24 / pnpm 12.

## Notes

- Builds directly on top of #17368 (@glw907, merged 2026-10-14) — the
shared-proxy teardown that this restart relies on landed one minute
before the author filed this issue. The two together give `wrangler
config change → one proxy restart across both dev servers`.
- The listener is attached per-server. If the SSR dev server triggers
the restart, Vite tears both down (SvelteKit SSR + client share the
proxy via `__sveltekit_cloudflare_platform_setup`), and the next `setup`
runs with the fresh config.
- The restart does not debounce — if an editor saves twice in a row,
Vite's own `server.restart()` already queues them.

## Changeset

Added patch changeset for `@sveltejs/adapter-cloudflare`.

cc @teemingc — thanks for the sharp report with the Wrangler file:line
references. If you'd rather drive this yourself I'm happy to close and
move my test coverage into your PR.

🤖 Used Claude Code for the research pass and to cross-check the mock
shapes against existing adapter-cloudflare tests; every assertion signed
off by me.

---------

Co-authored-by: Tee Ming <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-platform-tests This PR needs to run platform tests in order to merge. pkg:adapter-cloudflare

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adapter-cloudflare 8: concurrent Vite servers each start a platform proxy, so Vitest projects collide on the same SQLite (SQLITE_BUSY_RECOVERY)

3 participants