Repository navigation
Build the shared Cloudflare runtime inside the request scope - #302
Merged
Merged
Conversation
Creating the runtime starts Better Auth's initialization, and a plugin can read the database during it. The hosted extension's plugin does, so on hosted every request failed: the runtime was created before the request hook entered the scope that holds the request's database pool. The runtime is now created and awaited in that scope. The Cloudflare test build includes an extension whose authentication plugin reads the database while Better Auth initializes, which reproduces the failure.
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.
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 increateCloudflareRuntime, before the request hook enters theAsyncLocalStoragescope 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
getcreates 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'srun, for example while reporting an error.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:
Typecheck, lint and format pass.


Confidence Score: 5/5
The PR appears safe to merge; no actionable issues were found.Summary
Builds the shared Cloudflare runtime inside the request scope so authentication plugins can read the database during startup.
Diagram
Reviews (1) · Last reviewed commit: "Build the shared Cloudflare runtime insi..." · Reviewed by Greptile