Hey everyone, I’m running into a maddening issue and hoping someone has a workaround. I’m getting 50/50 500 internal server errors in local development whenever Better Auth is invoked.
I'm using a Turbo monorepo with Bun, TanStack Start (frontend), and Hono (backend), all targeting Cloudflare Workers (all recent versions). I'm using Prisma with Postgres for the DB, and Better Auth for authentication. For local dev, I'm using Alchemy, which runs both the web app and the server app locally in individual processes executed as a single Turbo task.
Exactly half of my requests fail with a 500 error (net::ERR_FAILED) and the other half succeed with a 200 OK.
I narrowed it down to Better Auth interacting with the database or some issue during client execution. My oRPC routes had a middleware using auth.api.getSession() to build the context. When I removed that line, all oRPC routes became 100% stable. However, any direct calls from the frontend authClient to the Better Auth endpoints on the Hono server still fail exactly 50% of the time.
This is similar to the behavior described in better-auth #969 and cloudflare/workerd #210. It seems like Better Auth (via Prisma) is leaving the database connection open or leaving a promise hanging after the response is sent. The local Cloudflare Worker isolate detects this leaked I/O, panics, and crashes the worker (causing the 500 on the next request), then reboots (causing a 200), and loops forever.
Has anyone successfully navigated this in local dev with Workers + Prisma + Better Auth? Is there a specific configuration or teardown hook I need to implement to force the DB connection closed so workerd stops killing the dev server? Any help would be hugely appreciated!