Skip to content

fix(spanner): ensure GrpcCredentialsOption is preserved when set - #16731

Open
scotthart wants to merge 1 commit into
googleapis:mainfrom
scotthart:spanner_grpc_creds_option
Open

scotthart wants to merge 1 commit into
googleapis:mainfrom
scotthart:spanner_grpc_creds_option

Conversation

@scotthart

Copy link
Copy Markdown
Member

No description provided.

@scotthart
scotthart requested a review from a team as a code owner December 1, 2026 21:16
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Dec 1, 2026

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

Copy link
Copy Markdown

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 isolates session-pool options from unrelated defaults (such as credentials) when creating Spanner connections. It introduces DefaultSessionPoolOptionsOnly to apply only session-pool defaults and constraints, and updates MakeConnection and SessionPoolOptions to use this helper. Additionally, comprehensive unit tests are added to verify that unrelated options do not leak. The review feedback highlights two important issues in the test code: a thread-safety data race with a function-local static PRNG generator in ScopedTempFile, and a potential null pointer dereference in LoopbackSpannerServer if BuildAndStart() fails.

Comment on lines +1426 to +1434
explicit ScopedTempFile(std::string_view contents) {
static internal::DefaultPRNG generator = internal::MakeDefaultPRNG();
path_ = internal::PathAppend(
::testing::TempDir(),
internal::Sample(generator, 12,
"abcdefghijklmnopqrstuvwxyz0123456789"));
std::ofstream os(path_);
os << contents;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The function-local static internal::DefaultPRNG generator is not thread-safe. If multiple threads instantiate ScopedTempFile concurrently, it will result in a data race and undefined behavior. We should protect the generator with a std::mutex to ensure thread safety. To avoid static destruction order issues with non-trivial destructors (in accordance with the Google C++ Style Guide), we should allocate the static mutex and generator on the heap and access them via references.

  explicit ScopedTempFile(std::string_view contents) {
    static auto& mu = *new std::mutex;
    static auto& generator = *new internal::DefaultPRNG(internal::MakeDefaultPRNG());
    std::string filename;
    {
      std::lock_guard<std::mutex> lk(mu);
      filename = internal::Sample(generator, 12,
                                  "abcdefghijklmnopqrstuvwxyz0123456789");
    }
    path_ = internal::PathAppend(::testing::TempDir(), filename);
    std::ofstream os(path_);
    os << contents;
  }
References
  1. Avoid using function-local static variables with non-trivial destructors (such as std::vector or std::string) to prevent static destruction order issues, in accordance with the Google C++ Style Guide. (link)

builder.RegisterService(&service_);
server_ = builder.BuildAndStart();
}
~LoopbackSpannerServer() { server_->Shutdown(); }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

grpc::ServerBuilder::BuildAndStart() can return nullptr if it fails to bind to the port or encounters other initialization errors. If server_ is nullptr, calling server_->Shutdown() in the destructor will result in a null pointer dereference and crash. We should check if server_ is not null before calling Shutdown().

  ~LoopbackSpannerServer() { if (server_) server_->Shutdown(); }

@codecov

codecov Bot commented Dec 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.81%. Comparing base (434f132) to head (a4871d2).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16731      +/-   ##
==========================================
- Coverage   92.82%   92.81%   -0.01%     
==========================================
  Files        2279     2279              
  Lines      229093   229386     +293     
==========================================
+ Hits       212647   212902     +255     
- Misses      16446    16484      +38     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant