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
Let me start by understanding the repository structure and then find the relevant code mentioned in the issue.
The issue is about:
runWithTransactionin@better-auth/corecontext/transaction.mjsopensadapter.transactionunconditionallygetCurrentAdapterreuses the activetrxfrom the async local storagerunWithTransactionshould be reentrant - when an adapter/transaction is already active in the async-context store, it should runfnwithin it instead of opening a nestedadapter.transaction
Let me explore the repository structure first.
Assistant
Let me find the relevant transaction file and the consumeVerificationValue function.
Assistant
Let me look at the key files
Assistant
Now I can see the issue clearly. The
runWithTransactionfunction always callsadapter.transaction(...), even when there's already an active transaction in the async local storage. Let me find theconsumeVerificationValuefunction that triggers this nesting.Assistant
Let me look at the
consumeVerificationValueimplementation in the internal adapter:Assistant
Now let me check the magic-link verify path to see how it calls `consumeVerificationValue
Assistant
Let me look at the magic-link verify handler to see the full context, especially around line 373:
Assistant
Let me look at
consumeOneWithHooksto trace the nestedAssistant
Let me find the
consumeOneWithHooksimport and definition:Assistant
Let me find the
getWithHooksfunction:Assistant
Let me look at the
with-hooks.tsfile:Assistant
Let me check how the API routes are wrapped - is there a
runWithTransactionat the API handler level?Assistant
Let me look at line 124 in internal-adapter.ts:
Assistant
Now I understand the full picture. The
runWithTransactionfunction intransaction.tsalways callsadapter.transaction(...)even when there's already an active transaction in the async local storage. This causes a nested transaction that deadlocks on connection-limited pools.The fix should make
runWithTransactionreentrant: when there's already an active transaction in the ALS store, it should simply runfn()within that context instead of opening a newadapter.transaction.Let me implement
Assistant
I need to use the
opencommand via the bash terminal.Assistant
Let me see the rest of the file:
Assistant
Now I understand the issue completely. The
runWithTransactionfunction always opens a newadapter.transaction, even when there's already an active transaction in the ALS store. The fix is to check for an existing store and reuse it, mirroring howgetCurrentAdapterworks.Let me implement the fix
Assistant
Let me verify the change is correct:
Assistant
The change looks correct. Now let me run the relevant tests to make sure nothing is broken. First, let me find out how to run tests:
Assistant
Let me run the relevant tests. First, let me check for tests related to transactions and the internal adapter:
Assistant
Let me look at the core package.json to understand the test setup:
Assistant
Let me run the tests for the core package to make sure the change doesn't break anything
Assistant
The 3 failures are pre-existing import errors (package resolution issues unrelated to my change), and 306 tests passed including 23 test files. Let me also run the internal adapter test specifically in the better-auth package (which has the
consumeVerificationValuetests):Assistant
Let me check the better-auth package tests
Assistant
Let me check the transaction test suite:
Assistant
Let me check if there are any tests specifically for the transaction module:
Assistant
Let me check if there's a dist folder with the compiled output to compare with
Assistant
Let me also verify my understanding of the nesting by checking how
createSessionworks:Assistant
Let me fix the path:
Assistant
Let me look at the
createSessionfunctionAssistant
Let me check the sign-up route that uses `runWithTransaction
Assistant
Let me look at
createUserin the internal adapter:Assistant
Let me look at
createUser:Assistant
Let me check if there's a wrapper that puts API handlers into transactions:
Assistant
Let me look at the in-memory adapter's `consumeOne
Assistant
Let me look at the factory to see if
consumeOnetriggers another transactionAssistant
Let me look at the full
consumeOneimplementation in the factory:Assistant
Let me look at the full magic-link verify handler more carefully to see if there's any outer wrapping
Assistant
Let me look at the full verify handler:
Assistant
Let me check how the factory implements `transaction
Assistant
Let me check the factory's
transactionmethodAssistant
Let me find
createAsIsTransactionAssistant
Let me look at `createAsIs
Assistant
The fix looks correct. Let me now run the core package tests again to verify nothing is broken:
Assistant
The 3 failing test files have pre-existing import resolution issues (package resolution), not related to my change. Let me also quickly check that TypeScript compilation works:
Assistant
TypeScript compilation passes cleanly. Let me do a final review of the changes I made:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.