fix(firestore): observe rejection of lazily-started transaction ID promise - #9122
Open
rockwotj wants to merge 2 commits into
Open
fix(firestore): observe rejection of lazily-started transaction ID promise#9122rockwotj wants to merge 2 commits into
rockwotj wants to merge 2 commits into
Conversation
…omise When the first read of a transaction fails, the derived _transactionIdPromise can be left without a handler. Nothing awaits it for read-only transactions, because rollback() returns early when _writeBatch is unset, and a read-write transaction can leave it rejected across a macrotask boundary. Node reports an unhandled rejection, which terminates the process under the default --unhandled-rejections=throw, even though the caller handled the error from runTransaction(). Attach a no-op handler when the promise is created. Later awaiters in commit() and rollback() still reject, so error propagation is unchanged.
Contributor
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where an unhandled promise rejection is reported in Node.js when the first read of a transaction fails. This is resolved by attaching a catch handler to _transactionIdPromise to observe the rejection, and a corresponding test case has been added to verify the behavior. The reviewer suggested using the void operator on the floating promise to prevent potential ESLint warnings and explicitly signal that the promise is intentionally unawaited.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
dlarocque
approved these changes
Aug 12, 2026
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.
Problem
When the first read of a transaction fails, the client can emit an
unhandledRejection. Under Node's default--unhandled-rejections=throwthis terminates the process, even though the application handled the error thatrunTransaction()returned.withLazyStartedTransaction()derives a second promise from the first read:The caller handles the returned promise. Nothing attaches a handler to
_transactionIdPromiseat creation time. Its only handler comes later, from a subsequent read,commit(), orrollback(). Two cases leave it unobserved:rollback()returns early whenthis._writeBatchis unset, so it never awaits_transactionIdPromise. No handler is ever attached.rollback()does await the promise, but only after the callback returns. If the callback yields to the event loop in between, Node has already reported the rejection. Node also logsPromiseRejectionHandledWarninghere, which confirms the handler arrived too late.Case 1 is deterministic: for a read-only transaction, no handler is ever attached, so any failure of the first read terminates the process.
We hit case 1 in production on
@google-cloud/firestore@8.7.1. The first read of a read-only transaction, aBatchGetDocuments, stalled for 293 seconds and then failed with16 UNAUTHENTICATED. The application caught the error fromrunTransaction(), and the process still died on the orphaned rejection. Read-only transactions set_maxAttemptsto 1, so no retry masked it.Reproduction
Point the client at a port where nothing listens, then run a read-only transaction whose first read fails:
Before this change the listener fires with code 14. After it, it does not. The same script reproduces case 2 with a read-write transaction whose callback swallows the read error and then awaits a timer.
Fix
Attach a no-op handler to
_transactionIdPromisewhen it is created. This marks the rejection observed. Later awaiters incommit()androllback()still reject exactly as before, so error propagation is unchanged.Test
Adds a regression test to
handwritten/firestore/dev/test/transaction.ts. It recordsunhandledRejectionevents while a read-only transaction's first read fails, and asserts none are emitted.Verified both directions against the
firestorepackage:1 failing—expected [ Error: Test Error, …(1) ] to be empty.transactionsuite is green at37 passing.