GeezerVison is showing github.com — fix(spanner): avoid data race on DIRECTPATH_CHANNEL_CREATED by using volatile by rahul2393 · Pull Request #13727 · googleapis/google-cloud-java
Conversation
rahul2393
commented
Jul 10, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes b/509632639.
Problem
GapicSpannerRpc declared a process-wide mutable static flag:
public static boolean DIRECTPATH_CHANNEL_CREATED = false;
The flag is written from the GapicSpannerRpc constructor without synchronization and read by HeaderInterceptor when populating the directpath_enabled built-in metrics attribute. When multiple Spanner clients are constructed concurrently (for example Apache Beam's DirectRunner initializing DoFns on multiple worker threads), the unsynchronized write/read pair is a data race; ThreadSanitizer halts the JVM on it.
Fix
Declare the field volatile:
public static volatile boolean DIRECTPATH_CHANNEL_CREATED = false;
- The volatile write in the constructor happens-before every subsequent volatile read in
HeaderInterceptor, eliminating the data race. The flag has no read-modify-write usage, so volatile is sufficient; the existing last-writer-wins semantics are preserved unchanged.
volatile was chosen over AtomicBoolean deliberately: it keeps the field's source and binary signature (boolean) intact, so any external code reading GapicSpannerRpc.DIRECTPATH_CHANNEL_CREATED keeps compiling and linking. (The class is @InternalApi and the spi.v1 package is excluded from clirr checks, but there is no reason to break the field's ABI when volatile fixes the race equally well.)
- Javadoc on the field documents the concurrency semantics.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
gemini-code-assist[bot] reacted with eyes emoji
…volatile
GapicSpannerRpc declared a process-wide mutable static flag
'public static boolean DIRECTPATH_CHANNEL_CREATED' that is written from
the GapicSpannerRpc constructor without synchronization and read by
HeaderInterceptor when populating the directpath_enabled built-in
metrics attribute. Constructing multiple Spanner clients concurrently
(for example Apache Beam DirectRunner initializing DoFns on multiple
worker threads) makes that write/read pair a data race, which
ThreadSanitizer reports and halts the JVM on.
Declare the field volatile so the constructor write happens-before
every subsequent read. The flag has no read-modify-write usage, so
volatile is sufficient, and it preserves the field's boolean source and
binary signature as well as the existing last-writer-wins behavior.
Document the concurrency semantics on the field.
Add a regression test that constructs eight GapicSpannerRpc instances
concurrently against the in-process mock server, verifies construction
succeeds with the flag in a consistent state, and asserts via
reflection that the field stays volatile.
There was a problem hiding this comment.
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 addresses a potential data race during concurrent client construction by marking the static field DIRECTPATH_CHANNEL_CREATED as volatile in GapicSpannerRpc, and introduces a concurrent unit test to verify this fix. The review feedback recommends improving the test's cleanup block by adding a null check and awaiting the termination of the ExecutorService to prevent potential thread leaks and NullPointerExceptions.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
+1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.