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 inspect the repository for
runWithTransaction, the async transaction store, and related tests. I’ll then make the smallest reentrant change consistent with existing adapter semantics, run targeted tests (and broader relevant tests if practical), and review the diff before submitting. - Assistant
rgis unavailable in the container, so I’ll usegrep/findinstead. The repository is a pnpm workspace with dependencies already present, which should allow focused package tests without installation. - Assistant
The transaction context currently has no distinction between
runWithAdapterandrunWithTransaction; both populate the same store shape. I’ll add an internal transaction marker sorunWithTransactionreuses only an actually active transaction, while calls made under a non-transaction adapter context still open a transaction. Nested calls will execute directly and share the outer pending-hook queue, preserving commit/error behavior. - Assistant
The first edit attempt could not run because this environment has no
pythonbinary; no file was changed. I’ll apply the same targeted edit with Node, which is available through the repository toolchain. - Assistant
The implementation now marks only contexts created by
runWithTransactionas transactional, avoids opening a second adapter transaction in nested calls, and leavesrunWithAdapterbehavior unchanged. I’ll run the core tests and typecheck, then inspect formatting/status. - Assistant
The requested pnpm command did not reach the tests: Corepack attempted to download pnpm and blocked on its interactive confirmation, then timed out. I’ll invoke the locally installed binaries directly to avoid network/tooling prompts.
- Assistant
TypeScript compilation and Biome validation pass. The selected direct Vitest invocation is blocked by the workspace’s package self-import aliases (
@better-auth/core/async_hooks,.../instrumentation) when run from the root, rather than by test failures. I’ll retry from the core package context and also inspect the final diff. - Assistant
The package-context retry shows the same pre-existing workspace alias resolution issue, while typecheck and Biome pass. I’ve also checked the important edge case:
runWithAdapteris used around request handlers, so treating any ALS store as a transaction would incorrectly suppress legitimate transactions; the explicit marker avoids that regression. I’ll do a final build-level check and then submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.