Repository navigation
Conversation
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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
- 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(); } |
There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
No description provided.