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

core: Free unused MessageProducer in RetriableStream (1.45.x backport) #9853

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/java/io/grpc/internal/RetriableStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ public void messagesAvailable(final MessageProducer producer) {
checkState(
savedState.winningSubstream != null, "Headers should be received prior to messages.");
if (savedState.winningSubstream != substream) {
GrpcUtil.closeQuietly(producer);
return;
}
listenerSerializeExecutor.execute(
Expand Down
41 changes: 41 additions & 0 deletions core/src/test/java/io/grpc/internal/RetriableStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import io.grpc.internal.StreamListener.MessageProducer;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executor;
Expand Down Expand Up @@ -991,6 +993,27 @@ public void messageAvailable() {
verify(masterListener).messagesAvailable(messageProducer);
}

@Test
public void inboundMessagesClosedOnCancel() throws Exception {
ClientStream mockStream1 = mock(ClientStream.class);
doReturn(mockStream1).when(retriableStreamRecorder).newSubstream(0);

retriableStream.start(masterListener);
retriableStream.request(1);
retriableStream.cancel(Status.CANCELLED.withDescription("on purpose"));

ArgumentCaptor<ClientStreamListener> sublistenerCaptor1 =
ArgumentCaptor.forClass(ClientStreamListener.class);
verify(mockStream1).start(sublistenerCaptor1.capture());

ClientStreamListener listener = sublistenerCaptor1.getValue();
listener.headersRead(new Metadata());
InputStream is = mock(InputStream.class);
listener.messagesAvailable(new FakeMessageProducer(is));
verify(masterListener, never()).messagesAvailable(any(MessageProducer.class));
verify(is).close();
}

@Test
public void closedWhileDraining() {
ClientStream mockStream1 = mock(ClientStream.class);
Expand Down Expand Up @@ -2718,4 +2741,22 @@ private interface RetriableStreamRecorder {

Status prestart();
}

private static final class FakeMessageProducer implements MessageProducer {
private final Iterator<InputStream> iterator;

public FakeMessageProducer(InputStream... iss) {
this.iterator = Arrays.asList(iss).iterator();
}

@Override
@Nullable
public InputStream next() {
if (iterator.hasNext()) {
return iterator.next();
} else {
return null;
}
}
}
}