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

Fix retry race condition that can lead to double decrementing inFlightSubStreams and so miss calling closed #11026

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Changes from 1 commit
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
26 changes: 20 additions & 6 deletions core/src/main/java/io/grpc/internal/RetriableStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@
@Nullable // null if already committed
@CheckReturnValue
private Runnable commit(final Substream winningSubstream) {

synchronized (lock) {
if (state.winningSubstream != null) {
return null;
Expand All @@ -165,10 +164,9 @@
// subtract the share of this RPC from channelBufferUsed.
channelBufferUsed.addAndGet(-perRpcBufferUsed);

final boolean wasCancelled = (scheduledRetry != null) ? scheduledRetry.isCancelled() : false;
final Future<?> retryFuture;
if (scheduledRetry != null) {
// TODO(b/145386688): This access should be guarded by 'this.scheduledRetry.lock'; instead
Copy link
Member

Choose a reason for hiding this comment

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

Why remove these? These look disabled by the @SuppressWarnings("GuardedBy")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The way that the comments were done was intrusive and ugly. Changed them all to be on the SuppressWarnings annotation.

// found: 'this.lock'
retryFuture = scheduledRetry.markCancelled();
scheduledRetry = null;
} else {
Expand All @@ -177,8 +175,6 @@
// cancel the scheduled hedging if it is scheduled prior to the commitment
final Future<?> hedgingFuture;
if (scheduledHedging != null) {
// TODO(b/145386688): This access should be guarded by 'this.scheduledHedging.lock'; instead
// found: 'this.lock'
hedgingFuture = scheduledHedging.markCancelled();
scheduledHedging = null;
} else {
Expand All @@ -196,6 +192,9 @@
}
if (retryFuture != null) {
retryFuture.cancel(false);
if (!wasCancelled) {
inFlightSubStreams.decrementAndGet();
}
}
if (hedgingFuture != null) {
hedgingFuture.cancel(false);
Expand Down Expand Up @@ -999,9 +998,19 @@
synchronized (lock) {
scheduledRetry = scheduledRetryCopy = new FutureCanceller(lock);
}

class RetryBackoffRunnable implements Runnable {
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void run() {
synchronized (scheduledRetryCopy.lock) {
if (scheduledRetryCopy.isCancelled()) {
return;

Check warning on line 1008 in core/src/main/java/io/grpc/internal/RetriableStream.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/RetriableStream.java#L1008

Added line #L1008 was not covered by tests
} else {
scheduledRetryCopy.markCancelled();
}
}

callExecutor.execute(
new Runnable() {
@Override
Expand Down Expand Up @@ -1563,11 +1572,16 @@
}

void setFuture(Future<?> future) {
boolean wasCancelled;
synchronized (lock) {
if (!cancelled) {
wasCancelled = cancelled;
if (!wasCancelled) {
this.future = future;
}
}
if (wasCancelled) {
future.cancel(false);

Check warning on line 1583 in core/src/main/java/io/grpc/internal/RetriableStream.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/RetriableStream.java#L1583

Added line #L1583 was not covered by tests
}
}

@GuardedBy("lock")
Expand Down
Loading