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
core: fix bug RetriableStream cancel() racing with start() #8386
Merged
dapengzhang0
merged 2 commits into
grpc:master
from
dapengzhang0:fix-retriable-stream-bug
Aug 6, 2021
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ abstract class RetriableStream<ReqT> implements ClientStream { | |
@GuardedBy("lock") | ||
private FutureCanceller scheduledHedging; | ||
private long nextBackoffIntervalNanos; | ||
private Status cancellationStatus; | ||
|
||
RetriableStream( | ||
MethodDescriptor<ReqT, ?> method, Metadata headers, | ||
|
@@ -244,14 +245,16 @@ private void drain(Substream substream) { | |
int index = 0; | ||
int chunk = 0x80; | ||
List<BufferEntry> list = null; | ||
boolean streamStarted = false; | ||
|
||
while (true) { | ||
State savedState; | ||
|
||
synchronized (lock) { | ||
savedState = state; | ||
if (savedState.winningSubstream != null && savedState.winningSubstream != substream) { | ||
// committed but not me | ||
if (savedState.winningSubstream != null && savedState.winningSubstream != substream | ||
&& streamStarted) { | ||
// committed but not me, to be cancelled | ||
break; | ||
} | ||
if (index == savedState.buffer.size()) { // I'm drained | ||
|
@@ -275,17 +278,22 @@ private void drain(Substream substream) { | |
|
||
for (BufferEntry bufferEntry : list) { | ||
savedState = state; | ||
if (savedState.winningSubstream != null && savedState.winningSubstream != substream) { | ||
// committed but not me | ||
if (savedState.winningSubstream != null && savedState.winningSubstream != substream | ||
&& streamStarted) { | ||
// committed but not me, to be cancelled | ||
break; | ||
} | ||
if (savedState.cancelled) { | ||
if (savedState.cancelled && streamStarted) { | ||
checkState( | ||
savedState.winningSubstream == substream, | ||
"substream should be CANCELLED_BECAUSE_COMMITTED already"); | ||
substream.stream.cancel(cancellationStatus); | ||
return; | ||
} | ||
bufferEntry.runWith(substream); | ||
if (bufferEntry instanceof RetriableStream.StartEntry) { | ||
streamStarted = true; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -299,6 +307,13 @@ private void drain(Substream substream) { | |
@Nullable | ||
abstract Status prestart(); | ||
|
||
class StartEntry implements BufferEntry { | ||
@Override | ||
public void runWith(Substream substream) { | ||
substream.stream.start(new Sublistener(substream)); | ||
} | ||
} | ||
|
||
/** Starts the first PRC attempt. */ | ||
@Override | ||
public final void start(ClientStreamListener listener) { | ||
|
@@ -311,13 +326,6 @@ public final void start(ClientStreamListener listener) { | |
return; | ||
} | ||
|
||
class StartEntry implements BufferEntry { | ||
@Override | ||
public void runWith(Substream substream) { | ||
substream.stream.start(new Sublistener(substream)); | ||
} | ||
} | ||
|
||
synchronized (lock) { | ||
state.buffer.add(new StartEntry()); | ||
} | ||
|
@@ -440,6 +448,7 @@ public void run() { | |
|
||
@Override | ||
public final void cancel(Status reason) { | ||
cancellationStatus = reason; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to just before the synchronized block (or as an else within the synchronized block)? Right now if commit() fails the status is still set, which may be confusing. |
||
Substream noopSubstream = new Substream(0 /* previousAttempts doesn't matter here */); | ||
noopSubstream.stream = new NoopClientStream(); | ||
Runnable runnable = commit(noopSubstream); | ||
|
@@ -450,11 +459,16 @@ public final void cancel(Status reason) { | |
return; | ||
} | ||
|
||
state.winningSubstream.stream.cancel(reason); | ||
Substream winningSubstreamToCancel = null; | ||
synchronized (lock) { | ||
// This is not required, but causes a short-circuit in the draining process. | ||
if (state.drainedSubstreams.contains(state.winningSubstream)) { | ||
winningSubstreamToCancel = state.winningSubstream; | ||
} // otherwise the winningSubstream will be cancelled while draining | ||
state = state.cancelled(); | ||
} | ||
if (winningSubstreamToCancel != null) { | ||
winningSubstreamToCancel.stream.cancel(reason); | ||
} | ||
} | ||
|
||
private void delayOrExecute(BufferEntry bufferEntry) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
streamStarted
is false here, we can also short-circuit by returning directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite. At the moment, every created stream has to be used. Things like DelayedClientTransport.newStream() add the stream to their tracking list, so you can't just drop the stream on the floor.