Skip to content

Commit

Permalink
core: Catch uncaught exceptions in ServerImpl
Browse files Browse the repository at this point in the history
Fixes grpc#6162
  • Loading branch information
anarsultanov committed Sep 19, 2019
1 parent 19b0916 commit 6cd81c3
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions core/src/main/java/io/grpc/internal/ServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -131,7 +132,7 @@ public final class ServerImpl extends io.grpc.Server implements InternalInstrume
* Construct a server.
*
* @param builder builder with configuration for server
* @param transportServer transport server that will create new incoming transports
* @param transportServers transport server that will create new incoming transports
* @param rootContext context that callbacks for new RPCs should be derived from
*/
ServerImpl(
Expand Down Expand Up @@ -555,14 +556,10 @@ private void runInternal() {
}
listener =
startCall(stream, methodName, method, headers, context, statsTraceCtx, tag);
} catch (RuntimeException e) {
stream.close(Status.fromThrowable(e), new Metadata());
} catch (Throwable t) {
stream.close(Status.fromThrowable(t), new Metadata());
context.cancel(null);
throw e;
} catch (Error e) {
stream.close(Status.fromThrowable(e), new Metadata());
context.cancel(null);
throw e;
throw t;
} finally {
jumpListener.setListener(listener);
}
Expand Down Expand Up @@ -590,7 +587,7 @@ final class ServerStreamCancellationListener implements Context.CancellationList
@Override
public void cancelled(Context context) {
Status status = statusFromCancelled(context);
if (DEADLINE_EXCEEDED.getCode().equals(status.getCode())) {
if (DEADLINE_EXCEEDED.getCode().equals(Objects.requireNonNull(status).getCode())) {
// This should rarely get run, since the client will likely cancel the stream before
// the timeout is reached.
stream.cancel(status);
Expand Down Expand Up @@ -777,12 +774,9 @@ public void runInContext() {
PerfMark.linkIn(link);
try {
getListener().messagesAvailable(producer);
} catch (RuntimeException e) {
internalClose();
throw e;
} catch (Error e) {
} catch (Throwable t) {
internalClose();
throw e;
throw t;
} finally {
PerfMark.stopTask("ServerCallListener(app).messagesAvailable", tag);
}
Expand Down Expand Up @@ -812,12 +806,9 @@ public void runInContext() {
PerfMark.linkIn(link);
try {
getListener().halfClosed();
} catch (RuntimeException e) {
} catch (Throwable t) {
internalClose();
throw e;
} catch (Error e) {
internalClose();
throw e;
throw t;
} finally {
PerfMark.stopTask("ServerCallListener(app).halfClosed", tag);
}
Expand Down Expand Up @@ -886,12 +877,9 @@ public void runInContext() {
PerfMark.linkIn(link);
try {
getListener().onReady();
} catch (RuntimeException e) {
internalClose();
throw e;
} catch (Error e) {
} catch (Throwable t) {
internalClose();
throw e;
throw t;
} finally {
PerfMark.stopTask("ServerCallListener(app).onReady", tag);
}
Expand Down

0 comments on commit 6cd81c3

Please sign in to comment.