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 thetrxadapter from the ALS store.runWithTransactionis inconsistent with this -- it doesn't reuse.Expected:
runWithTransactionshould be reentrant -- when an adapter/transaction is already active in the async-context store, runfnwithin it instead of opening a nestedadapter.transaction(mirroringgetCurrentAdapter). Then verify needs only one connection and works on any pool size. (Alternatively/additionally, document that adapters MUST support reentrant nested transactions -- currently adapters like@zenstackhq/better-authopen a brand-new transaction on the base client for eachadapter.transactioncall, which is a reasonable contract but incompatible with the current nesting.)This is security-relevant: the 1.6.11 change that introduced this nesting was the magic-link "mint multiple sessions from one token" race fix, so the fix effectively breaks magic-link verification on connection-limited adapters/pools.
What version of Better Auth are you using?
1.6.11
System info
> The `auth info` CLI can't introspect this project headlessly because the auth instance is built dynamically via a factory (no static `auth.ts` export), so the values below are assembled manually { "better-auth": { "version": "1.6.11", "@better-auth/core": "1.6.11" }, "adapter": { "name": "@zenstackhq/better-auth", "version": "3.6.4", "orm": "@zenstackhq/orm 3.6.4 (Kysely 0.28.17)" }, "database": { "driver": "pg 8.20.0", "pool": { "max": 1 }, "fronted_by": "Cloudflare Hyperdrive" }, "runtime": { "platform": "Cloudflare Workers (workerd)", "wrangler": "4.95.0", "node_for_tooling": "v22.22.1" }, "plugins": ["magicLink"], "os": "macOS (Darwin 25.5.0)" }Which area(s) are affected? (Select all that apply)
Backend
Auth config (if applicable)
import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, });Additional context
- Bisected away unrelated suspects: the hang reproduces on older framework code and is independent of DI scope changes -- it's purely
1.6.10 -> 1.6.11. - Reproduced deterministically in a real
workerdintegration test (request magic link -> capture token -> verify) that times out before the change. - Relevant code:
@better-auth/corecontext/transaction.mjsrunWithTransaction(opensadapter.transactionunconditionally) vsgetCurrentAdapter(reuses the activetrx);internalAdapter.consumeVerificationValue->consumeOneWithHooks-> adapterconsumeOne. - Worked around downstream by making the adapter's transaction reentrant (reuse the in-flight transaction via
AsyncLocalStorage), which is also the correct semantics (nested transaction = one atomic unit) -- but it seems likerunWithTransactionitself should reuse the active transaction.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /better-auth, current file: none) bash-$
Assistant
I'll start by exploring the repository structure and finding the relevant files mentioned in the issue.
Assistant
Now let me look at how
runWithTransactionis used and findconsumeVerificationValue:Assistant
Let me look at the
consumeVerificationValueimplementation in the internal-adapter:Assistant
Now let me look at
consumeOneWithHooksto understand the nesting:Assistant
Now let me look at the
consumeOneWithHooksin with-hooks:Assistant
Let me look at the magic-link verify path:
Assistant
Let me look at the code around line 124 to understand another
runWithTransactionusage:Assistant
Let me check the adapter's
consumeOneimplementation to understand where the second transaction comes from:Assistant
Now I see. Let me look at the
consumeOneimplementation in the factory, especially the fallback path that callsadapter.transaction.Assistant
Let me look at the factory's
transactionimplementation to understand how the transaction adapter (trx) is built and whetherconsumeOneis on it:Assistant
Let me look at
createAsIsTransactionand how the transaction adapter is constructed:Assistant
Let me check the kysely adapter's
consumeOneandtransaction:Assistant
Let me look at the kysely transaction implementation to understand what methods the trx adapter exposes:
Assistant
Let me look at the transaction test suite to understand the expected contract:
Assistant
Let me look at the consumeVerificationValue test to understand the expectations:
Assistant
Let me check for existing tests related to
runWithTransactionin the core package:Assistant
Now let me write a reproduction script to confirm the deadlock. First, let me check the build/test setup:
Assistant
Now let me create a reproduction script that demonstrates the deadlock with a single-connection-pool adapter:
Assistant
Let me fix the imports to be relative within the core package:
Assistant
The factory.test.ts is in
packages/core/src/db/adapter/and it imports from./factory. Let me check how factory.test.ts resolves@better-auth/core/instrumentation. Let me look at the package.json exports and the instrumentation path.Actually, the issue is that the
factory.tsimports from@better-auth/core/instrumentation(a self-package import). This requires the package to be resolvable. Let me check if there's an alias or if the package resolves to itself.Let me check the existing factory.test.ts - it imports
./factorydirectly. The instrumentation import inside factory.ts is@better-auth/core/instrumentation. Let me check how this resolves.Let me check the tsconfig and the dev-source condition. The vitest config uses
conditions: ["dev-source"]. Let me check the package.json exports for./instrumentation.Assistant
The
@better-auth/core/instrumentationexport exists. The issue is the self-referencing package import. In Node.js, a package can reference itself by name only if it's properly set up. Let me check the factory.test.ts to see how it's run. Let me check if there's a nodeexportsself-reference resolution issue. Let me run the factory.test.ts to confirm it works.Assistant
Even the existing factory.test.ts fails. Let me check the node_modules linking for
@better-auth/core:Assistant
So the
@better-auth/corepackage isn't linked to itself in node_modules. The self-reference@better-auth/core/instrumentationcannot be resolved. This means the tests for the core package can't resolve self-references.Let me check the pnpm-workspace.yaml and how the workspace packages are linked. Maybe I need to build the core package first, or there's a different resolution.
Actually wait - the factory.test.ts is an existing test that presumably passes in CI. So there must be a way to resolve
@better-auth/core/instrumentation. Let me check the vitest config more thoroughly - maybe there's an alias in the core package's own vitest config.Assistant
The
runWithTransactionitself is intransaction.ts. Let me check its imports to avoid the instrumentation issue:Assistant
Let me check how the existing context test (
request-state.test.ts) resolves@better-auth/core/async_hooks:Assistant
Even the existing
request-state.test.tsfails with the same self-reference resolution issue. The workspace packages aren't linked. Let me check the install state and try to fix it:Assistant
The workspace packages aren't linked. Let me run
pnpm installto set up the workspace links:Assistant
@better-auth/coreis linked in the pnpm virtual store but the self-reference from within the package isn't resolving through vitest. Let me check the vitest resolution and try creating the root-level symlink that pnpm normally creates:Assistant
Let me remove the factory dependency and test only
transaction.tswith a minimal mock adapter:Assistant
The self-reference
@better-auth/core/async_hooksisn't resolving even with the root symlink. Let me verify the export path and check the resolution:Assistant
Let me create a self-link within the core package's node_modules so the self-reference resolves locally:
Assistant
The
dev-sourcecondition resolution isn't working through vitest. Let me try building the core package so thedistexists:Assistant
The build succeeded. Now let me re-run the test:
Assistant
I've reproduced the deadlock. The nested
runWithTransactionopens tx2 which tries to acquire a second connection held by tx1 → deadlock. Now let me implement the fix inrunWithTransaction:Assistant
Now let me apply the fix to make
runWithTransactionreentrant:Assistant
Let me use the
editcommand directly. First, let me open the file:Assistant
Now let me run the reproduction test to verify the deadlock is fixed:
Assistant
The deadlock is fixed for the nested
runWithTransactioncase. Now let me investigate theconsumeOnefactory fallback, since the issue trace mentionsconsumeOnetriggering the second transaction. Let me check the git history of the 1.6.11 change:Assistant
Let me look at how the magic-link verify was changed to use
consumeVerificationValue:Assistant
Let me check whether the factory tests can run now that dist is built, and test the
consumeOnefallback with a single-connection pool:Assistant
Now let me write a more comprehensive repro that tests the
consumeOnefactory fallback (for adapters without nativeconsumeOne) with a single-connection pool, to determine if it also deadlocks:Assistant
The consumeOne fallback is safe for standard adapters (trx built with
transaction: false). Let me check if@zenstackhq/better-authis available anywhere to understand its actual structure:Assistant
Let me test the scenario where the trx's
transactionis also real (non-standard adapter) to see if the consumeOne fallback deadlocks:Assistant
This confirms that for non-standard adapters (like
@zenstackhqwhich "opens a brand-new transaction on the base client for eachadapter.transactioncall"), theconsumeOnefactory fallback ALSO deadlocks, and therunWithTransactionfix alone doesn't help. Let me check the factory imports and the fallback code to also make it reentrant:Assistant
The factory doesn't import the ALS store. Let me check the
db/adapter/index.tsto understand potential circular dependency:Assistant
Let me check what
db/adapter/index.tsre-exports (does it re-export the factory?).Assistant
Let me check the factory's
deleteManyanddeletewrappers to see if they calladapter.transactionorrunWithTransaction:Assistant
Let me view the full
consumeOneandincrementOnefallbacks to implement reentrancy:Assistant
The
@zenstackhqadapter (non-standard, opens a new transaction peradapter.transactioncall) would deadlock via theconsumeOnefallback, which myrunWithTransactionfix alone doesn't cover. Let me checkasync_hooksto ensure no circular runtime dependency before adding a helper:Assistant
The reference-comparison approach won't work (the ALS store's adapter IS the same trx object
consumeOneis called on). I need to check the ALS store's existence directly. Let me add agetActiveTransactionhelper totransaction.ts:Assistant
The edit broke the closing of
getCurrentAdapter. Let me view and fix it:Assistant
I need to restore the
getCurrentAdapterclosing braces. Let me fix it:Assistant
There's a duplicate
getActiveTransaction. Let me remove the duplicate (lines 69-94):Assistant
The edits got messy. Let me rewrite the entire file cleanly:
Assistant
Now let me export
getActiveTransactionfrom the context index:Assistant
The
editcommand modified the wrong file (the currently opentransaction.ts)! Let me rewritetransaction.tscleanly and then editindex.tswith--file:Assistant
Now let me edit
index.tsusing the--fileoption:Assistant
Now let me update the factory's
consumeOneandincrementOnefallbacks to reuse the active transaction. First, add the import:Assistant
Now let me update the
consumeOnefallback. Let me view the current state:Assistant
Now let me rewrite the consumeOne fallback to reuse the active transaction. I'll extract the inner logic into a function
consumeOneInTransaction(trx)and usegetActiveTransactionto decide whether to reuse or open a new transaction.Let me replace the block.
Assistant
Let me open the file at the relevant location first, then run the replace:
Assistant
Now let me replace the consumeOne fallback block (lines 1362-1410):
Assistant
There's a duplicate closing brace on line 1423. Let me remove it:
Assistant
Now let me update the
incrementOnefallback similarly. Let me view it:Assistant
I made an error - the edit ate the
}),and);closing theif (adapterInstance.incrementOne)block. Let me fix it:Assistant
There's a duplicate closing brace on line 1581. Let me remove it:
Assistant
Now let me verify the consumeOne fallback structure is correct:
Assistant
Both fallbacks look correct. Now let me run the factory tests and my repro tests to verify:
Assistant
All tests pass, including repro3 (the consumeOne fallback deadlock). Now let me run the typecheck to verify no type errors were introduced:
Assistant
The typecheck passes. Now let me run the existing
consumeVerificationValuetests in the better-auth package to ensure I didn't break anything:Assistant
The better-auth tests can't resolve
@better-auth/kysely-adapter. Let me check the linking and build state:Assistant
The kysely-adapter has no
distand dev-source resolution is failing in this environment. Let me build it so the better-auth tests can resolve it:Assistant
Let me build the memory-adapter too:
Assistant
This is a chain of unbuilt packages. Let me build all the remaining workspace packages better-auth depends on:
Assistant
The consumeVerificationValue tests pass. Let me run the full internal-adapter test suite and the magic-link tests:
Assistant
All magic-link tests pass. Now let me write an end-to-end test that mirrors the exact
consumeVerificationValueflow with a single-connection-pool adapter (no native consumeOne) to fully verify the issue scenario. Let me check how to inject a custom adapter:Assistant
Let me look at the internal-adapter.test.ts imports and setup to write a faithful end-to-end repro:
Assistant
Let me write an end-to-end test in the better-auth package that uses
createInternalAdapterwith a custom single-connection-pool adapter. Let me check howcreateInternalAdapteris invoked and whatinitreturns.Actually, let me look at how the internal adapter is constructed in init.ts to replicate it minimally. Or I can use
init(opts)withdatabase: (options) => customAdapter.Let me write the test using
initwith a custom adapter function. Let me check whatinitneeds (migrations etc.). The internal-adapter.test.ts does(await getMigrations(opts)).runMigrations()theninit(opts). With a custom adapter function, migrations might not work the same way. Let me check.Actually, this is getting complex. Let me take a simpler, more direct approach: write the end-to-end repro using
createInternalAdapterdirectly, which is the function under test. Let me check its signature.createInternalAdapter(adapter, ctx)where ctx = { options, logger, hooks, generateId }. Let me look at how it's called in init.ts.Assistant
Let me look at how
createInternalAdapteris called in create-context.ts to mirror the setup:Assistant
Let me check the
InternalLoggertype to construct a minimal logger for the end-to-end test:Assistant
The end-to-end test deadlocks because the better-auth package is using the built dist of
@better-auth/core(which I built before my changes), not my source changes! Let me rebuild the core dist:Assistant
The end-to-end test now passes (14ms, no deadlock). Let me run the broader core test suite to ensure nothing is broken:
Assistant
All 325 core tests pass. Now let me run the better-auth tests that use
consumeVerificationValueand transactions (magic-link, two-factor, oidc, one-time-token):Assistant
All pass, including the two-factor concurrency tests (which use
consumeVerificationValueandincrementOne). Let me run the organization tests (heavyrunWithTransactionusers) and oidc/one-time-token:Assistant
All organization tests pass. Let me run a few more
consumeVerificationValue/runWithTransactionconsumers: sign-up, one-time-token, and oidc:Assistant
All tests pass. Now let me clean up the temporary test files I created (the instructions say not to add new tests):
Assistant
Only my 3 source file changes are tracked. Let me review the full diff to verify correctness:
Assistant
The diff looks correct. I also noticed and fixed a latent bug:
runWithTransactionhadlet called = true(should befalselikerunWithAdapter), so theensureAsyncStoragefailure fallback never worked. Let me verify the built dist reflects my changes:Assistant
Let me check the built
transaction.mjsto see if the reentrancy change is present (the variable may have been renamed by the bundler):Assistant
The dist is correct (line 77:
if (als.getStore()) return fn();- the variable was inlined). Let me run a few moreconsumeVerificationValueconsumers to be thorough:Assistant
All pass. Let me do a final review of the complete
transaction.tsfile to ensure it's clean:Assistant
The file is clean. Let me check
runWithAdapterusages to ensure no similar nesting issue exists there:Assistant
Wait - this is critical.
runWithAdapterwraps the request handler and sets the ALS store with the base adapter (not a transaction). MyrunWithTransactionreentrancy check (als.getStore()) would incorrectly skip opening a transaction in the real HTTP flow, losing atomicity. Let me investigate howapicalls and the handler relate torunWithAdapter:Assistant
runWithAdapteris only in the HTTPhandler, not theapiobject. Let me check howapimethods are constructed to see if they wrap withrunWithAdapter:Assistant
I found a critical issue:
runWithAdapter(which wraps the HTTP handler) sets the ALS store with the base adapter, so myrunWithTransactionreentrancy check would incorrectly skip opening a transaction in the HTTP path, losing atomicity. I need to distinguish a transaction context from a base-adapter context. Let me check all ALS.run()usages first:Assistant
Only
runWithAdapterandrunWithTransactionset the adapter ALS store. I'll add anisTransactionflag toHookContextto distinguish a transaction context (trx) from a base-adapter context (set byrunWithAdapterin the HTTP handler). Let me updatetransaction.ts:Assistant
Now I need to verify the critical HTTP-handler path:
runWithAdapter(base adapter scope) wrappingrunWithTransactionmust STILL open a transaction. Let me write a test simulating the HTTP handler scope:Assistant
Both pass — the HTTP-handler scope opens exactly 1 transaction (atomicity preserved) and doesn't deadlock. Now let me rebuild the core dist so better-auth tests pick up the
isTransactionchange, then re-run the end-to-end repro:Assistant
The dist is updated. Now let me write a full end-to-end test that wraps
consumeVerificationValue(viacreateInternalAdapter) inrunWithAdapter(simulating the HTTP handler scope), with a transaction counter to assert exactly 1 transaction:Assistant
Excellent! The HTTP-handler-scoped end-to-end test passes: exactly 1 transaction, no deadlock, single-use semantics intact. Now let me re-run the full core and key better-auth test suites to confirm the
isTransactionchange didn't break anything:Assistant
All pass. Let me run the organization tests (heavy
runWithTransactionusers) once more to confirm theisTransactionchange is safe:Assistant
All pass. Let me run the typecheck once more to confirm the
isTransactionchange typechecks cleanly:Assistant
Typecheck passes. Now let me clean up the temporary test files and do a final review:
Assistant
Let me do a final review of the complete diff:
Assistant
The diff is clean and correct. Let me verify no temporary test files remain and check the final state:
Assistant
No temporary files remain; only the 3 source files are modified (dist/node_modules are gitignored). Let me run the factory and increment-one tests one final time to confirm the fallback changes are correct:
Assistant
All pass. Let me do a final structural review of the factory fallback sections to confirm no syntax issues:
Assistant
All sections are syntactically correct. Let me run a final broad sanity check on the better-auth db test folder to catch any regressions in the db layer:
Assistant
There are 2 failures. Let me investigate whether they're caused by my changes or pre-existing:
Assistant
The first failure is
ECONNREFUSED 5432(no Postgres running — environment issue). Let me check the second (timeout) test to determine if it's environment-related or a real regression:Assistant
Both failing tests require external databases (
testWith: "postgres"→ ECONNREFUSED 5432, andtestWith: "mongodb"→ connection timeout). These are environment issues (no Postgres/MongoDB running), not caused by my changes. Let me confirm no DB services are running and that these failures are pre-existing:Assistant
No Postgres/MongoDB is reachable and no docker — these are pre-existing environment failures. To definitively confirm my changes don't cause a deadlock in these DB-backed tests, let me stash my changes, rebuild the original dist, run the 2 tests, then restore:
Assistant
Confirmed: the mongodb test fails (10s timeout) even with the original code (no MongoDB running). It's a pre-existing environment failure, not caused by my changes. Let me restore my changes and rebuild the dist:
Assistant
My changes are restored and the dist rebuilt. The 2 db failures are confirmed pre-existing (missing Postgres/MongoDB). Let me do a final confirmation run of the key tests with the restored changes:
Assistant
All key tests pass. Let me do a final full-diff review of the factory.ts changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →