Repository navigation
Build the Cloudflare control-plane runtime once per isolate - #296
Merged
Merged
Conversation
Each request built the whole runtime again, including Better Auth with its plugins, endpoints and schema check, and every service. A CPU profile of the hosted control plane spent about half its time collecting the resulting garbage and 13 percent building the runtime. Workers forbid I/O on behalf of another request, so the database pool and the analytics and reporting clients stay per request. The shared runtime reaches them through AsyncLocalStorage. The request hook runs request handling in that scope and closes the three clients afterwards as before. A failed initialization is retried by the next request.
At low traffic isolates start often, and the check would run at each start. Node still runs it, so tests compare the schemas.
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
widefleet-docs | c503bbc | Nov 22 2026, 03:01 PM |
SvelteKit calls handleError after the hook has returned, outside the request scope, so the installation's crash report could not reach the request's reporting client. The services now stay open until handleError has reported the error, then close. Redirects and HTTP errors, which do not reach handleError, still close them right away.
tnkuehne
added a commit
that referenced
this pull request
Nov 22, 2026
After #296, every request to the hosted control plane failed with 500: "The control plane runtime was used outside a request". ## Cause Building the runtime starts Better Auth's initialization, and the hosted extension's authentication plugin reads the installation from the database in its `init`. #296 built the runtime in `createCloudflareRuntime`, before the request hook enters the `AsyncLocalStorage` scope that holds the request's database pool. That read therefore ran outside any request. The public Cloudflare tests did not catch it because their build has no extension with such a plugin. ## Changes - `get` creates and awaits the shared runtime inside the request's scope. Initialization reads the database with the pool of the request that first needs the runtime. This also holds when the runtime is first needed outside the hook's `run`, for example while reporting an error. - The Cloudflare CI job builds the control plane with `plugin-init-extension.fixture.ts`. That extension's authentication plugin reads the installation during Better Auth's initialization, as the hosted extension does. ## Validation Locally, with the control plane built with the fixture extension: - Without this change, the Cloudflare control-plane suite fails during setup, as hosted did. - With this change, all 16 tests pass. Typecheck, lint and format pass. <!-- greptile_comment --> <!-- greptile_summary --> <h2><a href="https://app.greptile.com/api/retrigger?id=115798572&sig=bab0f2614f144ec9561458327dfad23d77a3af949245b28f951244bcd0eed160"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/-/switch-tenant?tenant=mendra&next=%2Fwidefleet%2Fwidefleet%2Fpull%2F302&utm_source=greptile_review&utm_medium=github&utm_campaign=view_in_greptile"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 5/5</h2> <!-- greptile-impact-verdict --> The PR appears safe to merge; no actionable issues were found. <details open><summary><h3>Summary</h3></summary> Builds the shared Cloudflare runtime inside the request scope so authentication plugins can read the database during startup. - Cloudflare requests initialize the shared runtime inside their request scope. </details> <details open><summary><h3>Diagram</h3></summary> ```mermaid sequenceDiagram participant Request participant Scope as Request scope participant Shared as Shared runtime participant DB as Request database Request->>Scope: get() Scope->>Shared: Create if missing Shared->>DB: Initialize authentication plugins DB-->>Shared: Installation Shared-->>Scope: Ready runtime Scope-->>Request: Runtime ``` </details> <!-- greptile_confidence_score:5 --> <sub>Reviews (1) · Last reviewed commit: ["Build the shared Cloudflare runtime insi..."](11efbd1) · [Reviewed by Greptile](https://www.greptile.com/?utm_source=greptile_review&utm_medium=github&utm_campaign=review_public&utm_content=widefleet/widefleet)</sub> <!-- /greptile_comment -->
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.
The Cloudflare request hook built the whole control-plane runtime for every request: Better Auth with its plugins, endpoints and schema check, plus every service. A 40-second production CPU profile of the hosted control plane spent 51 percent in garbage collection, 13 percent building the runtime and 11 percent creating Better Auth. On hosted, a request such as
getAppsnow spends more of its server time on CPU than on the database.Changes
createCloudflareRuntimebuilds the runtime on an isolate's first request and reuses it afterwards. If initialization fails, the next request builds it again instead of keeping the failure.AsyncLocalStorage:Observationthat forwards to the request's clienthooks.cloudflare.tsruns request handling in that scope and closes the three clients when the response finishes, as before. Deferred rendering keeps the scope because it continues from that call.Validation
pnpm checkRUN_CLOUDFLARE_TESTS=1): 16 passed. This includes "keeps sessions and PostgreSQL connections scoped correctly across concurrent requests" and initialization error reporting./: median 14 ms on this branch, 15 to 17 ms onmain. A local machine shows little of the difference. A production CPU profile after deploy will show the real effect.

Confidence Score: 5/5
The PR appears safe to merge; the previous crash-report issue is fixed.Summary
Builds one shared Cloudflare runtime per isolate while keeping database, analytics, and reporting clients separate for each request.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Request] --> B[Create request clients] B --> C[Run with request scope] C --> D[Shared runtime] C --> E{Hook outcome} E -->|Response| F[Close clients when body ends] E -->|Unexpected error| G[Save request runtime] G --> H[Report inside saved scope] H --> I[Close request clients]Reviews (2) · Last reviewed commit: "Report unexpected hook errors with their..." · Reviewed by Greptile