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
Let me start by exploring the repository structure to understand the codebase and locate the relevant files mentioned in the issue.
- Assistant
Let me focus on the transaction.ts file first, as that's where
runWithTransactionis defined according to the issue. - Assistant
Now let me look at the magic link plugin to understand the consumeVerificationValue flow:
- Assistant
Let me check the internal-adapter.ts to see how consumeVerificationValue is defined.
- Assistant
Let me look at the consumeVerificationValue function implementation.
- Assistant
Let me see more of this function and understand how consumeOneWithHooks works.
- Assistant
Let me also look at
consumeOneWithHooks: - Assistant
Let me check where
consumeOneWithHooksis defined: - Assistant
Let me look at
consumeOneWithHooksinwith-hooks.ts: - Assistant
Let me check how
consumeOneis defined on the adapter: - Assistant
Let me check how the magic link verify flow works to understand the exact call chain:
- Assistant
Let me check the magic link verify route to understand the flow:
- Assistant
Let me check the verify handler implementation:
- Assistant
Let me check how consumeOne is defined in the adapter:
- Assistant
Let me look at the DB adapter interface to find consumeOne
- Assistant
Let me look at how consumeOne is implemented in the adapter factory:
- Assistant
Let me look at how
adapter.transactionis implemented in the factory to understand whattrxis: - Assistant
Let me look at the transaction method in the factory:
- Assistant
Let me look at what
createAsIsTransactiondoes: - Assistant
Let me look at the createAsIsTransaction function.
- Assistant
Let me check the types of DBAdapter and DBTransactionAdapter to understand what methods are available:
- Assistant
Let me look at the DBTransactionAdapter type:
- Assistant
Now I understand the issue. Let me look more carefully at the flow in
consumeVerificationValue. The key question is: doesconsumeOneWithHooksitself callrunWithTransaction? - Assistant
Let me look more carefully at the issue - maybe the issue is that the factory's
consumeOnefallback callsadapter.transaction()which is nested: - Assistant
Let me look at line 124 to see the other use of
runWithTransaction: - Assistant
Now I have a clear understanding of the issue. Let me look at the
runWithTransactionfunction again and understand the fix needed:The issue is that
runWithTransactionalways callsadapter.transaction()even when there's already an active transaction in the AsyncLocalStorage. The fix should check if there's already an active transaction context and reuse it instead of opening a nested one. - Assistant
Now I understand the issue completely. The fix is in
runWithTransaction- it should check if there's already an active transaction in the AsyncLocalStorage store, and if so, reuse it instead of opening a nested transaction. Let me implement the fix: - Assistant
Now let me apply the fix to the actual file:
- Assistant
Let me see the rest of the file.
- Assistant
Now I'll apply the fix. The key change is in the
runWithTransactionfunction - when there's already an active transaction in the ALS store, we should reuse it instead of opening a nested one: - Assistant
Let me verify the complete file looks correct:
- Assistant
The change looks correct. Now let me run the existing tests to make sure nothing is broken:
- Assistant
Let me find tests related to transactions:
- Assistant
Let me check the test-utils transactions test:
- Assistant
Let me check if there are tests specifically for runWithTransaction or the consumeVerificationValue flow:
- Assistant
Let me look at the internal-adapter test to understand how the tests are run:
- Assistant
Let me check what test framework is being used and how to run tests:
- Assistant
Let me check if pnpm is available and run the internal-adapter tests:
- Assistant
Let me try using npx to run the tests:
- Assistant
Let me try running the internal-adapter test to see if my change breaks anything:
- Assistant
The test fails due to missing dependencies. Let me try to install the dependencies:
- Assistant
Let me check if there's a way to run the tests with the proper configuration:
- Assistant
Let me check the tsconfig to understand how the imports work:
- Assistant
Let me check if there's a workspace-level vitest config:
- Assistant
Let me try running the tests from the workspace root:
- Assistant
Let me check if the core package needs to be built first:
- Assistant
Let me check the exports of the core package:
- Assistant
Let me try building the core package first.
- Assistant
Let me try to find and run simpler tests that are more likely to work with the current setup:
- Assistant
Let me try running the factory test which tests consumeOne and transactions:
- Assistant
The tests require the core package to be built. Let me build it first:
- Assistant
Good, the core package built successfully. Let me try running the factory test again:
- Assistant
The factory tests pass. Let me also run the internal-adapter test:
- Assistant
The internal-adapter test requires other packages to be built. Let me try the magic-link test instead:
- Assistant
Let me build the kysely-adapter and better-auth packages as well:
- Assistant
Good, let me try the magic-link test again:
- Assistant
Let me build all packages:
- Assistant
All packages built successfully. Now let me try running the magic-link test again:
- Assistant
All 18 magic link tests pass. Let me also run the internal-adapter test now that all packages are built:
- Assistant
All 51 tests pass, including the consumeVerificationValue tests. Let me also run the core transaction tests:
- Assistant
Let me run a broader set of tests to make sure nothing is broken:
- Assistant
The only 2 failures are pre-existing and unrelated to my change:
- "should preserve a forced UUID on postgres when generateId is uuid" - Fails because no PostgreSQL server is running (
ECONNREFUSED) - "should coerce string where values to match field types" - Times out, also likely a PostgreSQL connection issue
All other 124 tests pass. Let me verify the final state of the file:
- "should preserve a forced UUID on postgres when generateId is uuid" - Fails because no PostgreSQL server is running (
- Assistant
Let me also run the core test suite to make sure nothing is broken there:
- Assistant
All 322 core tests pass. Let me also check if the dist directory needs to be updated with the build:
- Assistant
The dist has the old version. Let me rebuild the core package:
- Assistant
Let me verify the dist file now contains the fix:
- Assistant
The dist file now contains the fix. Let me run the full test suite one more time to confirm everything works:
- Assistant
Let me also see the summary.
- Assistant
All 391 tests pass across 28 test files. Let me verify the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.