Skip to content

fix(spanner): avoid mutating caller RequestOptions in place - #19187

Merged
sakthivelmanii merged 3 commits into
mainfrom
fix/spanner-clone-request-options
Nov 23, 2026
Merged

sakthivelmanii merged 3 commits into
mainfrom
fix/spanner-clone-request-options

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

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.

## 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.
@sakthivelmanii
sakthivelmanii requested a review from a team as a code owner November 23, 2026 07:46

@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 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.
@sakthivelmanii
sakthivelmanii merged commit e281fa2 into main Nov 23, 2026
85 checks passed
@sakthivelmanii
sakthivelmanii deleted the fix/spanner-clone-request-options branch November 23, 2026 14:55
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