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 start by exploring the repository to understand the structure and locate the relevant code. Let's begin with a quick overview.
- Assistant
Let me look at the structure more carefully.
- Assistant
Let me look at the packages directory to understand the structure better.
- Assistant
Now let me find the transaction-related files mentioned in the issue:
- Assistant
Let me examine the transaction.ts file and related files:
- Assistant
Let me use the open command correctly.
- Assistant
Let me see the rest of the file.
- Assistant
Let me see the full file.
- Assistant
Let me use cat to see the full file.
- Assistant
Now let me look at the internal adapter to find
consumeVerificationValue: - Assistant
Let me look at the
consumeVerificationValuefunction in the internal adapter: - Assistant
Let me see the rest of the
consumeVerificationValuefunction and understand the flow. - Assistant
Let me look at
consumeOneWithHooksto understand the full call chain: - Assistant
Let me look at the import of consumeOneWithHooks to see where it comes from.
- Assistant
Let me look at the
consumeOneWithHooksimplementation ingetWithHooks. - Assistant
Let me look at the
consumeOneWithHooksfunction inwith-hooks.ts: - Assistant
Let me look at the
consumeOnemethod implementation: - Assistant
Let me look at the
consumeOnemethod in the adapters to understand the full flow: - Assistant
Now I understand the issue. Let me look at the
consumeOneimplementation in the factory to see if it callsrunWithTransactionagain: - Assistant
Let me look at the full consumeOne implementation to see the fallback that opens a nested transaction:
- Assistant
Now let me understand how the transaction adapter is created in the factory:
- Assistant
Let me look at the transaction callback implementation:
- Assistant
Let me look at the
createAsIsTransactionfunction to understand how transaction adapters are created. - Assistant
Let me understand
createAsIsTransactionand the full transaction flow: - Assistant
Now I have a clear understanding of the issue. Let me look at
runWithTransactionagain to understand the fix needed: - Assistant
Now let me check if there's an existing test for this scenario:
- Assistant
Let me look at the magic-link verify route to understand the full flow:
- Assistant
Let me look at the magic-link plugin to understand the verify flow.
- Assistant
Let me look at the Kysely adapter to understand how transactions work there:
- Assistant
Now I have a clear picture of the issue. Let me trace through the full call chain:
magicLinkVerifycallsconsumeVerificationValue(storedToken)consumeVerificationValuecallsrunWithTransaction(adapter, fn)→ opens transaction T1 (takes the only connection)- Inside
fn, it callstxAdapter.consumeOne()on the transaction adapter - If the adapter doesn't implement native
consumeOne, the factory fallback (line 1379 in factory.ts) callsadapter.transaction(...)again, trying to open transaction T2 - T2 blocks because T1 holds the only connection → deadlock
The fix:
runWithTransactionshould check if there's already an active transaction in theAsyncLocalStoragestore. If so, it should reuse it (just runfn()) instead of opening a nestedadapter.transaction. - Assistant
Let me now make the fix to
runWithTransaction: - Assistant
Now I'll implement the fix. The key change is to make
runWithTransactioncheck if there's already an active transaction in theAsyncLocalStoragebefore opening a new one: - Assistant
Let me try using
replacecommand instead: - Assistant
Now let me apply the fix using
edit: - Assistant
Let me use
sedto apply the change: - Assistant
Let me try with node instead.
- Assistant
Let me write a script file to avoid bash escaping issues:
- Assistant
Let me verify the change:
- Assistant
Good, the fix is applied. Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
Let me check the test setup and run the core tests first:
- Assistant
There's no test file for transaction.ts. Let me look at the relevant tests.
- Assistant
Let me look at the factory.test.ts to understand how transactions are tested:
- Assistant
Now let me run the relevant tests to ensure nothing is broken:
- Assistant
The test can't run because of missing package resolution. Let me check if there's a build step needed:
- Assistant
Let me try building first to resolve the dependencies.
- Assistant
Let me try running the test differently:
- Assistant
Let me check if there's a turbo configuration for building, and whether the
@better-auth/core/instrumentationimport might need a special resolution: - Assistant
Let me check the vitest config:
- Assistant
Let me check if the core package has a vitest config:
- Assistant
Let me try running the test from within the core package:
- Assistant
The test can't find
@better-auth/core/instrumentation. Let me check if this is a build issue. - Assistant
Let me check the exports in the core package.json more carefully:
- Assistant
Let me check if there's a build step needed:
- Assistant
The problem is that
@better-auth/core/instrumentationis not resolving properly because it's looking fordist/instrumentation/index.d.mtsbut the dist might not be built yet. Let me check if there are prebuilt dist files. - Assistant
Let me check if there's a way to build just the core package quickly, or if there's a dev mode that resolves from source.
- Assistant
Let me check the instrumentation export more carefully:
- Assistant
Let me try building just the core package:
- Assistant
Let me try a different approach - write a small test script to verify the fix works:
- Assistant
Let me look more carefully at how the factory handles transactions and the
consumeOnefallback path: - Assistant
Let me look at the kysely adapter to find where it configures the transaction.
- Assistant
Let me look at line 933 and the surrounding context in the Kysely adapter to understand how the transaction is set up.
- Assistant
Now I understand the full picture. Let me look at the Kysely adapter's transaction setup - it creates a factory adapter with
transaction: falsefor the transaction scope, meaning nestedadapter.transaction()calls become no-ops. But the issue is thatrunWithTransactionitself always callsadapter.transaction()unconditionally, even when already inside a transaction context.My fix is correct. Let me now also check the
consumeOnefallback in the factory to see if there's also a directadapter.transaction()call that needs to be addressed: - Assistant
Let me also check the sign-up.ts route that uses
runWithTransaction: - Assistant
Let me also check the organization adapter and routes that use
runWithTransaction: - Assistant
Good, the organization adapter uses
runWithTransactionat the top level. My fix wouldn't affect those. Now let me try to run some relevant tests. Let me check if there are any tests for the magic-link plugin: - Assistant
Let me try running the magic link test to confirm the fix doesn't break anything:
- Assistant
The tests can't run because the packages haven't been built. Let me try to build just the core package:
- Assistant
Let me try building the core package first, then run the test:
- Assistant
Good, the core package built. Now let me try running the magic link test:
- Assistant
The kysely adapter also needs to be built. Let me build all the packages.
- Assistant
The build is timing out. Let me try a more targeted approach - build just the kysely adapter.
- Assistant
Let me also build the better-auth package:
- Assistant
Good, both packages built. Now let me run the magic link test again:
- Assistant
Let me also build the memory adapter:
- Assistant
Let me try the magic link test again.
- Assistant
Let me build all the needed packages at once:
- Assistant
Let me try the magic link test again:
- Assistant
All 18 magic link tests pass. Let me also run the internal adapter tests and the factory tests:
- Assistant
All 51 internal adapter tests pass. Let me also run the factory tests:
- Assistant
Let me also run the sign-up tests and the organization tests since they use
runWithTransaction: - Assistant
All sign-up tests pass. Let me also run the organization tests:
- Assistant
All tests pass. Let me also run a few more relevant test suites:
- Assistant
Let me also run the organization hook test:
- Assistant
All tests pass. Let me review my change one final time to make sure it's correct:
- Assistant
The change is minimal and correct. Let me verify the final state of the file one more time:
- Assistant
The fix looks correct. Let me run a broader set of tests to make sure nothing is broken:
- Assistant
The only failure is due to the
@better-auth/mongo-adapterpackage not being built (a pre-existing environment issue, not related to my change). The 4 test files that did run all passed (70 tests passed).Let me also verify there are no other places that need fixing by checking if there are any other callers that might be affected:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.