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
Now let's edit the transaction.ts to reuse an existing active transaction context.
Assistant
Now let's check if there's a built/dist version of this file used elsewhere (e.g. compiled .mjs mentioned in issue) that also needs updating, or if the package is built from source at test time.
Assistant
No dist files present in repo (built on demand). Let's check the package structure and existing tests for transaction.ts to ensure nothing breaks, and write a reproduction test script.
Assistant
Let's write a quick reproduction test simulating a connection-pool-limited adapter with a real transaction, then run magic-link test suite to confirm no regressions.
Assistant
Build succeeded. Now let's try the repro script using the compiled dist directly.
Assistant
The fix works. Now let's revert to unfixed logic temporarily to confirm the deadlock reproduces without the fix (sanity check).
Assistant
The bug is confirmed reproducible without the fix (deadlock detected), and resolved with the fix (success). Let's rebuild core with the fix applied now and run relevant test suites.
Assistant
Now let's run the existing magic-link and internal-adapter related tests, and broader test suite for transaction-related tests.
Assistant
This is an env issue unrelated to my change — many packages need to be built first. Let's just build all workspace packages via turbo.
Assistant
All packages built successfully. Now let's rerun the tests.
Assistant
All 75 tests pass. Now let's run the full better-auth test suite and organization plugin tests (which also use runWithTransaction) to be thorough.
Assistant
All organization tests pass too. Let's run the sign-up test and the core package's own test suite.
Assistant
All tests pass. Now let's clean up temp files, remove the built dist directories we created (since they shouldn't be part of the diff — check git status).
Assistant
Good — the dist directories are gitignored, so no extra clutter in the diff. Let's remove temp files and view the final diff.
Assistant
This is a minimal, focused fix. Let's double check the CHANGELOG or CONTRIBUTING conventions to see if a changeset is expected for this repo.
Assistant
This repo uses changesets. Let's add one for this fix.
Assistant
These packages are versioned together (fixed group), so referencing "auth" bumps all. I'll add a changeset targeting "@better-auth/core" package to be precise.
Assistant
Now let's do a final review of the diff and run the full test suite for the affected packages once more to be safe, then submit.
Assistant
Confirmed: these failures are pre-existing environment issues (no local Postgres available), unrelated to our change. Our fix doesn't introduce any regressions. Let's clean up build artifacts (dist folders are gitignored, verified earlier) and do a final check of the diff before submitting.
Assistant
The change looks correct and minimal. Let's go through the checklist:
Checklist review:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.