Skip to content

fix: Await transaction snapshot reads to preserve linked cancellation tokens - #15963

Merged
robertvoinescu-work merged 2 commits into
googleapis:mainfrom
robertvoinescu-work:fix/cancellation-disposal
Oct 7, 2026
Merged

robertvoinescu-work merged 2 commits into
googleapis:mainfrom
robertvoinescu-work:fix/cancellation-disposal

Conversation

@robertvoinescu-work

@robertvoinescu-work robertvoinescu-work commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes premature disposal of linked cancellation tokens during in-flight reads as described in #15958.

b/570764830

@robertvoinescu-work
robertvoinescu-work requested a review from a team as a code owner October 7, 2026 17:29

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors several snapshot retrieval methods in Transaction.cs to be asynchronous, ensuring that the linked CancellationTokenSource is not prematurely disposed before the underlying task completes. It also introduces comprehensive unit tests to validate in-flight cancellation propagation. The review feedback suggests optimizing these methods by avoiding CancellationTokenSource allocations when linking is unnecessary, and recommends disposing of the CancellationTokenRegistration in the test helper to prevent potential resource leaks.

Comment on lines +79 to +80
using var cts = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken, cancellationToken);
return await documentReference.GetSnapshotAsync(TransactionId, cts.Token).ConfigureAwait(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid unnecessary allocations of CancellationTokenSource and registration overhead when neither or only one of the cancellation tokens is active, we can conditionally create the linked token source only when both tokens are cancelable.

            bool link = CancellationToken.CanBeCanceled && cancellationToken.CanBeCanceled;
            using var cts = link ? CancellationTokenSource.CreateLinkedTokenSource(CancellationToken, cancellationToken) : null;
            var token = link ? cts.Token : (cancellationToken.CanBeCanceled ? cancellationToken : CancellationToken);
            return await documentReference.GetSnapshotAsync(TransactionId, token).ConfigureAwait(false);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point I've done this with a helper method in a seperate perf commit.

{
return documentReference.GetSnapshotAsync(TransactionId, cts.Token);
}
using var cts = CancellationTokenSource.CreateLinkedTokenSource(CancellationToken, cancellationToken);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency I removed the parentheses blocks for the using, but I can go the other way if you prefer.

@robertvoinescu-work
robertvoinescu-work marked this pull request as draft October 7, 2026 17:33
@robertvoinescu-work
robertvoinescu-work force-pushed the fix/cancellation-disposal branch from 2376cce to 1f04849 Compare October 7, 2026 17:48
@robertvoinescu-work
robertvoinescu-work marked this pull request as ready for review October 7, 2026 17:53

@amanda-tarafa amanda-tarafa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix, thanks!

@robertvoinescu-work
robertvoinescu-work merged commit 5775368 into googleapis:main Oct 7, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants