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 explore the repository structure and locate the
runWithTransactionimplementation mentioned in the issue. - Assistant
Let me look at the transaction.ts file and related code.
- Assistant
I understand the issue—I'll inspect how
consumeVerificationValueuses transactions and review related tests. - Assistant
rg is not available, let me use grep.
- Assistant
Let me look at the consumeVerificationValue implementation and the magic-link plugin usage.
- Assistant
Looking at the issue more carefully - the deadlock is: The deadlock trace is clearer now: the inner
consumeOne(orconsumeOneWithHooks) is callingrunWithTransactionagain, which is the root cause. - Assistant
Let me check the adapter implementations for consumeOne and transaction - maybe the default createAdapterFactory wraps things in transactions.
- Assistant
Found it — the
consumeOnefallback opens another nestedadapter.transaction. Reading that section next. - Assistant
Checking how Prisma avoids nested transactions and what other related tests cover.
- Assistant
I need to understand how the transaction adapter works in the factory — specifically, when
consumeOneruns ontrx. - Assistant
Inspecting the Kysely adapter's transaction and consumeOne implementations next.
- Assistant
Reviewing the full magic-link verify flow for nested
runWithTransactioncalls. - Assistant
Checking for tests around transaction context and recent related changes.
- Assistant
Checking git history and the factory consumeOne/incrementOne tests for the expected pattern.
- Assistant
Checking factory imports and how to fix the nested-transaction path.
- Assistant
Checking for circular dependency risk when importing context from the factory.
- Assistant
I'll fix
runWithTransactionto reuse an active transaction and update theconsumeOne/incrementOnefallbacks so they don't open nested transactions. - Assistant
Checking when
runWithAdapteris used — it might leave an adapter in ALS without an open transaction. - Assistant
Every request already runs in
runWithAdapter, so a plain adapter presence check is wrong — I'll track whether we're actually inside a transaction. - Assistant
Fixing the comment typo and the factory fallbacks next.
- Assistant
Let me re-read the file to get the exact text.
- Assistant
I see there's no weird character, I just had one atyped accidentally.
- Assistant
The replace tool may be choking on special characters; I'll use line-number edits instead.
- Assistant
Updating the factory fallbacks so they reuse an active transaction from ALS.
- Assistant
Adding a helper and wiring it效劳 into the factory fallbacks.
- Assistant
Fixing the typo and switching the fallbacks over to the helper.
- Assistant
Now replace adapter.transaction in consumeOne and incrementOne with runInAdapterTransaction.
- Assistant
I'll replace both occurrences and update the FIXME comments.
- Assistant
Need to use comma not colon for sed ranges
- Assistant
I'll replace both
adapter.transaction(asyncwithrunInAdapterTransaction(adapter, asyncand update the FIXME comment for consumeOne. - Assistant
The edit dropped the
findManycall — restoring it now. - Assistant
consumeOne looks good. Updating incrementOne next.
- Assistant
Only one left.
- Assistant
Writing a reproduction script to verify nested propertysignaling transactions don't deadlock.
- Assistant
Heredoc with
createdidn't land — writing the file via the shell instead. - Assistant
Semantics check out. Running the real unit tests next.
- Assistant
Let me check vitest config for how it resolves modules, and run the factory tests and maybe a transaction-specific test.
- Assistant
Tests resolve from source via
dev-source, so no rebuild is needed. - Assistant
Timed out — trying a simpler run approach.
- Assistant
Vitest might be hung on a circular import — checking whether loading the factory module stalls.
- Assistant
Building the core package first so deps resolve correctly.
- Assistant
Build was killed—retrying with a longer timeout.
- Assistant
The pipe to
tailmay be buffering and hanging the tool — running without it. - Assistant
Corepack is prompting interactively — disabling that so the build can run non-interactively.
- Assistant
tsdown isn't at the repo root — checking whether it's available under packages/core.
- Assistant
Let me build with corepack prompt disabled and direct path to tsdown.
- Assistant
Build finished. Running the tests.
- Assistant
Factory tests passed. Writing a proper unit test against the actual APIs and running consumeVerificationValue tests.
- Assistant
I'll add a temporary test under packages/core and run it with vitest.
- Assistant
Removing the temporary test file per the instructions not to add tests to the repo.
- Assistant
Confirming
queueAfterTransactionHookusage is unchanged, and reviewing the final diff. - Assistant
All 322 core packages tests passed. Checking git status and whether dist is tracked.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.