magic-link verify deadlocks: runWithTransaction nests adapter.transaction (1.6.11…
- User
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 thetrxadapter from the ALS store. is inconsistent with this -- it doesn't reuse. - Assistant
I’ll trace the transaction context and existing tests, add a regression that exposes the nested transaction call, then make the smallest reentrancy fix and run focused tests plus typechecking.
- Assistant
rgisn’t installed in this checkout, so I’m switching to targetedfind/grepsearches and keeping the scope limited to transaction and adapter tests. - Assistant
The request handler itself also installs an adapter context, so checking merely “is there a store?” would accidentally disable all request transactions. I’m distinguishing an actual transaction context and testing both sides: nested transactions reuse it, while a normal request adapter context still opens one.
- Assistant
The new core test hit the package’s self-import before executing because this checkout has no built
@better-auth/coreartifacts. I’m building that package locally (generated output is ignored), then I’ll run the regression to confirm the behavioral failure before editing the implementation. - Assistant
The focused regression now passes: nested calls stay on the same transaction adapter, while request-scoped calls still open a transaction. I’m validating the full verification-consume suite next, since that is the path magic-link verification exercises.
- Assistant
The consume suite is blocked at import time by another unbuilt workspace dependency (
@better-auth/kysely-adapter), not by the change. I’ll build that local package and rerun; no source files are changed by this step. - Assistant
The transaction-context regression and all 14 focused
consumeVerificationValuetests pass. I’m running the repository typecheck and reviewing the final diff for unintended generated or unrelated changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.