Skip to content

Commit

Permalink
Eliminate benign race condition in DirectStreamObserver
Browse files Browse the repository at this point in the history
A dynamic race condition detector correct identifies an unsynchronized
read-after-write between the field initializer and the first call of onNext.

Additionally, this fixes a benign inconsistency in which it would wait one
extra message the first time around.
  • Loading branch information
kennknowles committed Dec 19, 2024
1 parent d17f77a commit 4f46507
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class DirectStreamObserver<T> implements StreamObserver<T> {
private final int maxMessagesBeforeCheck;

private final Object lock = new Object();
private int numMessages = -1;
private int numMessages;

public DirectStreamObserver(Phaser phaser, CallStreamObserver<T> outboundObserver) {
this(phaser, outboundObserver, DEFAULT_MAX_MESSAGES_BEFORE_CHECK);
Expand All @@ -69,7 +69,7 @@ public DirectStreamObserver(Phaser phaser, CallStreamObserver<T> outboundObserve
@Override
public void onNext(T value) {
synchronized (lock) {
if (++numMessages >= maxMessagesBeforeCheck) {
if (++numMessages > maxMessagesBeforeCheck) {
numMessages = 0;
int waitSeconds = 1;
int totalSecondsWaited = 0;
Expand Down

0 comments on commit 4f46507

Please sign in to comment.