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
Now let me look at the existing core context test for conventions:
- Assistant
Now write the unit tests. First, let me check if I can find the GitHub issue number for the
@seeannotation: - Assistant
I guessed an issue number in that
@see— let me verify whether I can actually resolve it: - Assistant
The real issue is #9869. Let me correct the
@see: - Assistant
Now run the tests:
- Assistant
Confirmed — the three reentrant tests deadlock without the fix. Restoring:
- Assistant
Now let me trace the actual magic-link verify path on current main to see where the nesting occurs, and consider an integration-level regression test:
- Assistant
Let me check how the 1.6.11 release actually wired
consumeOne— to know whether the factory fallback path also needs the reuse: - Assistant
Now add the reuse to the factory fallbacks:
- Assistant
Now add the regression test to
factory.test.ts: - Assistant
Verify the factory test fails without the factory-side fix:
- Assistant
Now an end-to-end regression test at the magic-link level, matching the issue's repro. Let me check the existing magic-link tests:
- Assistant
The Postgres one needs Docker (pre-existing). The timeout needs investigation — let me check whether it's related to my change:
- Assistant
Now run typecheck:
- Assistant
Typecheck passes. Check formatting with Biome:
- Assistant
Final review of the complete change set:
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.