Skip to content

Commit 42e8276

Browse files
olavloiterahul2393
authored andcommitted
chore: make state field volatile in AsyncResultSetImpl (googleapis#3550)
* chore: make state field volatile in AsyncResultSetImpl Mark the `state` field in `AsyncResultSetImpl` volatile, as it is inspected by different threads. * fix: protect writes with monitor --------- Co-authored-by: rahul2393 <[email protected]>
1 parent 62a7151 commit 42e8276

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/AsyncResultSetImpl.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ private enum State {
112112
* Listeners that will be called when the {@link AsyncResultSetImpl} has finished fetching all
113113
* rows and any underlying transaction or session can be closed.
114114
*/
115-
private Collection<Runnable> listeners = new LinkedList<>();
115+
private final Collection<Runnable> listeners = new LinkedList<>();
116116

117-
private State state = State.INITIALIZED;
117+
private volatile State state = State.INITIALIZED;
118118

119119
/** This variable indicates that produce rows thread is initiated */
120120
private volatile boolean produceRowsInitiated;
@@ -498,10 +498,12 @@ public ApiFuture<Void> setCallback(Executor exec, ReadyCallback cb) {
498498
}
499499

500500
private void initiateProduceRows() {
501-
if (this.state == State.STREAMING_INITIALIZED) {
502-
this.state = State.RUNNING;
501+
synchronized (monitor) {
502+
if (this.state == State.STREAMING_INITIALIZED) {
503+
this.state = State.RUNNING;
504+
}
505+
produceRowsInitiated = true;
503506
}
504-
produceRowsInitiated = true;
505507
this.service.execute(new ProduceRowsRunnable());
506508
}
507509

0 commit comments

Comments
 (0)