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: Made ServerImpl.internalClose thread-safe. #11864

Closed
wants to merge 14 commits into from
15 changes: 11 additions & 4 deletions core/src/main/java/io/grpc/internal/ServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,12 @@ static final class JumpToApplicationThreadServerStreamListener implements Server
// Only accessed from callExecutor.
private ServerStreamListener listener;

public JumpToApplicationThreadServerStreamListener(Executor executor,
Executor cancelExecutor, ServerStream stream, Context.CancellableContext context, Tag tag) {
public JumpToApplicationThreadServerStreamListener(
Executor executor,
Executor cancelExecutor,
ServerStream stream,
Context.CancellableContext context,
Tag tag) {
this.callExecutor = executor;
this.cancelExecutor = cancelExecutor;
this.stream = stream;
Expand Down Expand Up @@ -809,9 +813,12 @@ void setListener(ServerStreamListener listener) {
* Like {@link ServerCall#close(Status, Metadata)}, but thread-safe for internal use.
*/
private void internalClose(Throwable t) {
// TODO(ejona86): this is not thread-safe :)
String description = "Application error processing RPC";
stream.close(Status.UNKNOWN.withDescription(description).withCause(t), new Metadata());
Metadata metadata = Status.trailersFromThrowable(t);
if (metadata == null) {
metadata = new Metadata();
}
stream.close(Status.UNKNOWN.withDescription(description).withCause(t), metadata);
}

@Override
Expand Down
1 change: 0 additions & 1 deletion core/src/test/java/io/grpc/internal/ServerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,6 @@ private void ensureServerStateNotLeaked() {
assertEquals(Status.UNKNOWN.getCode(), statusCaptor.getValue().getCode());
// Used in InProcessTransport when set to include the cause with the status
assertNotNull(statusCaptor.getValue().getCause());
assertTrue(metadataCaptor.getValue().keys().isEmpty());
}

private static class SimpleServer implements io.grpc.internal.InternalServer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
Expand Down Expand Up @@ -1598,8 +1599,10 @@ public void interactionsAfterServerStreamCloseAreNoops() throws Exception {
assertNotNull(clientStreamListener.trailers.get(TIMEOUT_MS, TimeUnit.MILLISECONDS));

// Ensure that for a closed ServerStream, interactions are noops
server.stream.writeHeaders(new Metadata(), true);
server.stream.writeMessage(methodDescriptor.streamResponse("response"));
assertThrows(Exception.class, () ->
server.stream.writeHeaders(new Metadata(), true));
assertThrows(Exception.class, () ->
server.stream.writeMessage(methodDescriptor.streamResponse("response")));
server.stream.close(Status.INTERNAL, new Metadata());

// Make sure new streams still work properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.inprocess;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static io.grpc.internal.GrpcUtil.TIMEOUT_KEY;
import static java.lang.Math.max;

Expand Down Expand Up @@ -414,6 +415,7 @@ private class InProcessServerStream implements ServerStream {
private boolean closed;
@GuardedBy("this")
private int outboundSeqNo;
private boolean closeCalled;

InProcessServerStream(MethodDescriptor<?, ?> method, Metadata headers) {
statsTraceCtx = StatsTraceContext.newServerContext(
Expand All @@ -431,6 +433,7 @@ public void setListener(ServerStreamListener serverStreamListener) {

@Override
public void request(int numMessages) {
checkState(!closeCalled, "call already closed");
boolean onReady = clientStream.serverRequested(numMessages);
if (onReady) {
synchronized (this) {
Expand Down Expand Up @@ -487,6 +490,7 @@ private void clientCancelled(Status status) {

@Override
public void writeMessage(InputStream message) {
checkState(!closeCalled, "call already closed");
long messageLength = 0;
if (isEnabledSupportTracingMessageSizes) {
try {
Expand Down Expand Up @@ -546,6 +550,7 @@ public synchronized boolean isReady() {

@Override
public void writeHeaders(Metadata headers, boolean flush) {
checkState(!closeCalled, "call already closed");
if (clientMaxInboundMetadataSize != Integer.MAX_VALUE) {
int metadataSize = metadataSize(headers);
if (metadataSize > clientMaxInboundMetadataSize) {
Expand Down Expand Up @@ -581,6 +586,7 @@ public void close(Status status, Metadata trailers) {
// clientStreamListener.closed can trigger clientStream.cancel (see code in
// ClientCalls.blockingUnaryCall), which may race with clientStream.serverClosed as both are
// calling internalCancel().
closeCalled = true;
clientStream.serverClosed(Status.OK, status);

if (clientMaxInboundMetadataSize != Integer.MAX_VALUE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public void onCompleted() {

assertTrue(finishLatch.await(900, TimeUnit.MILLISECONDS));
assertEquals(fakeResponse, responseRef.get());
assertNull(throwableRef.get());
}

@Test
Expand Down
Loading