Skip to content

Commit

Permalink
chore: make state field volatile in AsyncResultSetImpl (#3550)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
olavloite and rahul2393 authored Dec 13, 2024
1 parent c33c5e8 commit 432fc4e
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ private enum State {
* Listeners that will be called when the {@link AsyncResultSetImpl} has finished fetching all
* rows and any underlying transaction or session can be closed.
*/
private Collection<Runnable> listeners = new LinkedList<>();
private final Collection<Runnable> listeners = new LinkedList<>();

private State state = State.INITIALIZED;
private volatile State state = State.INITIALIZED;

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

private void initiateProduceRows() {
if (this.state == State.STREAMING_INITIALIZED) {
this.state = State.RUNNING;
synchronized (monitor) {
if (this.state == State.STREAMING_INITIALIZED) {
this.state = State.RUNNING;
}
produceRowsInitiated = true;
}
produceRowsInitiated = true;
this.service.execute(new ProduceRowsRunnable());
}

Expand Down

0 comments on commit 432fc4e

Please sign in to comment.