Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate benign race condition in DirectStreamObserver and make first and subsequent iterations consistent #33419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
scwhittle marked this conversation as resolved.
Show resolved Hide resolved
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that with the -1 -> 0 and the >= we still have the same behavior for the first triggered check. But subsequent triggered checks would take 1 more than previous incorrect behavior.

For example if maxMessagesPerCheck was 1, before we would check on
2nd message
and then every message afterwards. But now we would correctly check every other message. So if the test fails it seems like the test was verifying the bad behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

friendly ping

numMessages = 0;
int waitSeconds = 1;
int totalSecondsWaited = 0;
Expand Down
Loading