test(transactions): demonstrate ambient tx loss across await in mutate()#1522
Open
marius-se wants to merge 1 commit into
Open
test(transactions): demonstrate ambient tx loss across await in mutate()#1522marius-se wants to merge 1 commit into
marius-se wants to merge 1 commit into
Conversation
The docstring on `Transaction#mutate` (packages/db/src/transactions.ts:248-251) claims: "If the callback returns a Promise, the transaction context will remain active until the promise settles, allowing optimistic writes after `await` boundaries." The implementation does not honor that contract: `mutate()` invokes the callback synchronously and pops the transaction off the ambient stack in a `finally` block (packages/db/src/transactions.ts:294-298), so any collection operation executed after an `await` inside an async callback runs without an ambient transaction. This test asserts the documented behavior and fails today with `expected length 2, got 1` — only the pre-`await` insert lands on the transaction; the post-`await` insert is orphaned into a new direct transaction via the collection's `onInsert` handler instead. This commit only adds the failing test. CI will go red intentionally until either the implementation is fixed to match the docstring or the docstring is updated to match the implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎯 Changes
Adds a test that demonstrates a mismatch between
Transaction#mutate's documented contract and its implementation for async callbacks.The contract
The docstring on
Transaction#mutate(packages/db/src/transactions.ts:248-251) states:What actually happens
mutate()invokes the callback synchronously and pops the transaction off the ambient stack in afinallyblock (packages/db/src/transactions.ts:287-298):When the callback is
async,callback()returns its Promise at the firstawait, thefinallyfires immediately, andunregisterTransaction(this)removes the transaction fromtransactionStackbefore any post-awaitcode runs. Collection operations after the await see no ambient transaction and either throwMissingInsertHandlerError(noonInsert) or create a brand-new direct transaction via the collection's handler.Additional signals that the async path is unsupported today:
() => void, not() => void | Promise<void>.packages/db/tests/exercises an asyncmutate()callback. The async tests intransactions.test.tscovermutationFnandcommit(), not themutate()callback.✅ Checklist
pnpm test.🚀 Release Impact