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 understand the issue:
runWithTransactionin@better-auth/core(context/transaction.mjs) always opens a freshadapter.transactionwithout checking if there's already an active transaction in the async context.getCurrentAdaptercorrectly reuses the active transaction from the ALS store.- When
consumeVerificationValueruns insiderunWithTransaction, it triggers anotherrunWithTransactionviaconsumeOne, causing a nested transaction attempt. - This causes a deadlock when the database pool has limited connections (e.g., max: 1).
The fix should make
runWithTransactionreentrant - when an adapter/transaction is already active in the async-context store, it should run the function within it instead of opening a nested transaction.Let me explore the repository structure first.
Assistant
Let me look at the specific file mentioned in the issue:
context/transaction.mjs.Assistant
Let me look at this file and related files.
Assistant
The open command requires a LINE_NUMBER argument too. Let me just use the open command properly.
Assistant
Let me try with the exact syntax from the instructions.
Assistant
Now I can see the issue clearly. Let me also look at the
consumeVerificationValueand related code to understand the full flow.Assistant
Let me look at the
consumeVerificationValueimplementation in `internal-adapter.tsAssistant
Now let me look at
consumeOneWithHookswhich is called within the transaction:Assistant
Let me find
consumeOneWithHooksdefinitionAssistant
Let me look at
consumeOneWithHooks:Assistant
Let me look at how the adapter
consumeOnemight trigger another `runWithTransactionAssistant
Let me look at the factory's
consumeOnefallback path that usesrunWithTransactionAssistant
Let me look at how the factory wraps the
transactionAssistant
Let me look at the factory's
transactionimplementation (lines 843-...):Assistant
Let me look at what the Kysely adapter's
transactionreturns:Assistant
Let me see the Kysely adapter's transaction implementation
Assistant
Let me look at the factory's
consumeOnefallback more carefully - it callsadapter.transactionwhich is the outer factory adapter:Assistant
Let me look at the magic-link verify route to understand the full call chain:
Assistant
Let me check how
createCustomAdaptercreates the transaction adapter for KyselyAssistant
Let me see the
createCustomAdapterfunctionAssistant
Let me find where
consumeOneis defined in the Kysely adapter'screateCustomAdapterAssistant
Let me search for all callers of `runWithTransaction
Assistant
Let me look at the internal-adapter.ts line 124
Assistant
Let me also check the sign-up route call at line
Assistant
Let me look at the full magic link verify handler to see if there's another
runWithTransactioncallAssistant
Let me look at the organization adapter's
runWithTransactioncalls to understand potential nesting patternsAssistant
Now I have a clear understanding of the issue. Let me also look at the test file for the transaction context to understand expected
Assistant
Let me look at the project structure
Assistant
Now I understand the issue clearly. The fix is in
runWithTransactionin./packages/core/src/context/transaction.ts. It should check if there's already an active transaction in the ALS store (likegetCurrentAdapterdoes) and if so, reuse it instead of opening a nested transaction.Let me open the file:
Assistant
Let me see the rest:
Assistant
Now I'll edit the
runWithTransactionfunction to make it reentrant by checking for an existing transaction in the ALS store before opening a new one:Assistant
Let me try the edit command with the proper syntax:
Assistant
The file has been updated. Let me verify the changes look correct:
Assistant
Good. Now let me check if there are built/dist files that also need to be updated, and run any existing tests:
Assistant
No compiled files exist - the source
.tsfile is what gets published. Now let me run any relevant tests:Assistant
Let me run the tests for the packages that use
runWithTransactionto make sure my changes don't break anything:Assistant
The test failures appear to be pre-existing package resolution issues, not related to my change. Let me verify by checking git status:
Assistant
Let me verify the test failures are pre-existing by stashing my change and running the same tests:
Assistant
Let me restore my changes:
Assistant
Let me verify the TypeScript compiles correctly:
Assistant
The TypeScript compiles successfully. My change is clean and minimal. Let me also verify the final state of the file:
Assistant
The fix is complete. Let me verify the git diff one final time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.