Repository navigation
fix(spanner): avoid mutating caller RequestOptions in place - #19187
Merged
Merged
Conversation
## Problem Statement - Reusing a `RequestOptions` protobuf object across Cloud Spanner requests mutated the caller's instance in place when merging `client_context` or updating `transaction_tag` / `request_tag` (including during `execute_partitioned_dml`). - When applications shared a `RequestOptions` instance across different security contexts (such as per-snapshot or per-transaction `client_context` for Parameterized Secure Views), `secure_context` entries from an earlier request persisted on the shared object and overrode subsequent requests, causing cross-context data exposure and thread-safety races. ## Solution - Clone caller-provided `RequestOptions` inputs before merging `client_context` or modifying `transaction_tag` / `request_tag` across read, query, DML, batch, transaction, and partitioned DML operations (both sync and async). - Preserve the existing `client_context` precedence (`Client < Snapshot/Transaction/Batch < RequestOptions.client_context`) and the zero-allocation fast path when neither `request_options` nor `client_context` is supplied.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Spanner client library to ensure that RequestOptions are cloned before use, preventing accidental mutation of caller-provided objects or shared instances. The changes include updates to execute_partitioned_dml and _merge_request_options to perform explicit cloning, along with comprehensive unit tests to verify isolation across tenants and threads. I have no feedback to provide as there were no review comments to evaluate.
Avoid Python copy.deepcopy overhead when cloning proto-plus and raw protobuf RequestOptions inputs by copying the underlying protobuf message via CopyFrom and wrapping it with RequestOptions.wrap.
Remove the redundant raw-protobuf isinstance branch from _clone_request_options; RequestOptions(...) already deep-copies raw protobuf inputs safely.
olavloite
approved these changes
Nov 23, 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 Statement
RequestOptionsprotobuf object across Cloud Spanner requests mutated the caller's instance in place when mergingclient_contextor updatingtransaction_tag/request_tag(including duringexecute_partitioned_dml).RequestOptionsinstance across different security contexts (such as per-snapshot or per-transactionclient_contextfor Parameterized Secure Views),secure_contextentries from an earlier request persisted on the shared object and overrode subsequent requests, causing cross-context data exposure and thread-safety races.Solution
RequestOptionsinputs before mergingclient_contextor modifyingtransaction_tag/request_tagacross read, query, DML, batch, transaction, and partitioned DML operations (both sync and async).client_contextprecedence (Client < Snapshot/Transaction/Batch < RequestOptions.client_context) and the zero-allocation fast path when neitherrequest_optionsnorclient_contextis supplied.