ISSUE DESCRIPTION magic-link verify deadlocks: runWithTransaction nests…
- User
ISSUE DESCRIPTION
magic-link verify deadlocks: runWithTransaction nests adapter.transaction (1.6.11 consumeVerificationValue) instead of reusing the active transaction
Is this suited for github?
- Yes, this is suited for github
Reproduction
- better-auth 1.6.11 with the
magicLinkplugin. - A database adapter whose
transactionis a real interactive DB transaction that holds a dedicated connection for its duration (e.g. the Kysely adapter, or@zenstackhq/better-auth, backed bypg). - Limit the client connection pool to one connection:
new Pool({ max: 1 })-- standard when using Cloudflare Hyperdrive, which pools server-side so the worker keeps a tiny client pool. await auth.api.signInMagicLink({ body: { email } })-> works (single insert).await auth.api.magicLinkVerify({ query: { token } })(orGET /api/auth/magic-link/verify?token=...).
Minimal trigger: any setup where
adapter.transactionacquires a connection and the pool cannot hand out two connections at once.Current vs. Expected behavior
Current: the verify call hangs forever and never returns. Postgres shows the backend stuck
idle in transaction(wait_event = ClientRead) right after the verificationSELECT; the connection is never released, so every attempt leaks one idle-in-transaction connection.Trace of the cause:
internalAdapter.consumeVerificationValue(added in 1.6.11) runs insiderunWithTransaction-> opens transaction tx1 (BEGIN, takes the only pooled connection).- Consuming the row (
consumeOne) triggers anotherrunWithTransaction-> callsadapter.transactionagain -> transaction tx2, which tries to acquire a second connection. runWithTransactionin@better-auth/core(context/transaction.mjs) always opens a freshadapter.transaction; it does NOT checkadapterAsyncStorage.getStore()to see whether a transaction is already active in the current async context. So tx2 is nested inside tx1 and blocks forever waiting for the connection tx1 holds -> deadlock.
Note the CRUD path already reuses the active transaction:
getCurrentAdapter(adapter)returns the adapter from the ALS store. is inconsistent with this -- it doesn't reuse. - Assistant
I’ll locate
runWithTransaction, the transaction async-context storage, and nearby tests. I’ll then update the transaction runner to reuse the active transaction without changing its top-level behavior, add no repository tests per instruction, and run the relevant existing test suite/type checks. - Assistant
The storage is also used by
runWithAdapterfor every request, so merely checking whether a store exists would incorrectly bypass the transaction at the request boundary. I’ll add an internal transaction-context marker: ordinary adapter context still opens a transaction, while nested transaction contexts reuse the existing adapter and hook queue. This preserves deferred hook execution by the outermost transaction. - Assistant
The source change is now in place and
git diff --checkis clean. The package-manager command could not run because Corepack attempted an interactive pnpm download in this environment; I’ll use the repository’s installed binaries where available and perform a focused behavioral check with a temporary script. - Assistant
Formatting and the core package TypeScript check both pass. The implementation ensures a request-level
runWithAdaptercontext does not suppress the first transaction, while nestedrunWithTransactioncalls return directly inside the active transaction and share its pending hooks. I’ll do a final diff/status review before submission. - Assistant
Implemented a reentrant
runWithTransactioninpackages/core/src/context/transaction.ts.Changes
- Added an internal
isTransactionmarker to async-local transaction context. - Ensured
runWithAdaptercontexts are not mistaken for active database transactions. - Made nested
runWithTransactioncalls reuse the active transaction and pending hook queue. - Preserved normal transaction creation for top-level calls.
- Avoided nested adapter transactions that can deadlock single-connection pools.
Verification
- Biome formatting/check passed.
- Core package TypeScript check passed:
node_modules/.bin/tsc --project packages/core/tsconfig.json --pretty false --noEmit git diff --checkpassed.
Checklist
- Reproduced/confirmed the reported failure mechanism by tracing the nested transaction behavior and single-connection pool deadlock path.
- Added an internal
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.