Commit 1e05ea5
authored
fix(spanner): avoid data race on DIRECTPATH_CHANNEL_CREATED by using volatile (#13727)
Fixes b/509632639.
## Problem
`GapicSpannerRpc` declared a process-wide mutable static flag:
```java
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 `DoFn`s on multiple worker threads), the
unsynchronized write/read pair is a data race; ThreadSanitizer halts the
JVM on it.
## Fix
Declare the field `volatile`:
```java
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.1 parent 4fb3f4b commit 1e05ea5
2 files changed
Lines changed: 58 additions & 1 deletion
File tree
- java-spanner/google-cloud-spanner/src
- main/java/com/google/cloud/spanner/spi/v1
- test/java/com/google/cloud/spanner/spi/v1
Lines changed: 9 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
255 | 255 | | |
256 | 256 | | |
257 | 257 | | |
258 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
259 | 267 | | |
260 | 268 | | |
261 | 269 | | |
| |||
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
| 109 | + | |
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
| 114 | + | |
113 | 115 | | |
114 | 116 | | |
115 | 117 | | |
| 118 | + | |
116 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
117 | 123 | | |
118 | 124 | | |
119 | 125 | | |
| |||
934 | 940 | | |
935 | 941 | | |
936 | 942 | | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
937 | 986 | | |
938 | 987 | | |
939 | 988 | | |
| |||
0 commit comments