Skip to content

Build the shared Cloudflare runtime inside the request scope - #302

Merged
tnkuehne merged 1 commit into
mainfrom
runtime-init-in-request
Nov 22, 2026
Merged

tnkuehne merged 1 commit into
mainfrom
runtime-init-in-request

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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.

RetriggerView in GreptileConfidence 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.

  • Cloudflare requests initialize the shared runtime inside their request scope.

Diagram

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
Loading

Reviews (1) · Last reviewed commit: "Build the shared Cloudflare runtime insi..." · Reviewed by Greptile

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.
@tnkuehne
tnkuehne merged commit 7b46649 into main Nov 22, 2026
16 checks passed
@tnkuehne
tnkuehne deleted the runtime-init-in-request branch November 22, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant