From e45c0c53d04b3fa7ca3ed1c576f37af59db6af11 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Mon, 3 Aug 2015 19:11:47 -0700 Subject: [PATCH] Fix shutting down a never-started ServerImpl --- core/src/main/java/io/grpc/ServerImpl.java | 10 +++++++++- core/src/test/java/io/grpc/ServerImplTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/io/grpc/ServerImpl.java b/core/src/main/java/io/grpc/ServerImpl.java index 9b2125bc55..00631875a7 100644 --- a/core/src/main/java/io/grpc/ServerImpl.java +++ b/core/src/main/java/io/grpc/ServerImpl.java @@ -132,13 +132,21 @@ public final class ServerImpl extends Server { * Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected. */ public ServerImpl shutdown() { + boolean shutdownTransportServer; synchronized (lock) { if (shutdown) { return this; } shutdown = true; + shutdownTransportServer = started; + if (!shutdownTransportServer) { + transportServerTerminated = true; + checkForTermination(); + } + } + if (shutdownTransportServer) { + transportServer.shutdown(); } - transportServer.shutdown(); SharedResourceHolder.release(TIMER_SERVICE, timeoutService); return this; } diff --git a/core/src/test/java/io/grpc/ServerImplTest.java b/core/src/test/java/io/grpc/ServerImplTest.java index a0f381e50b..7b86d7aec4 100644 --- a/core/src/test/java/io/grpc/ServerImplTest.java +++ b/core/src/test/java/io/grpc/ServerImplTest.java @@ -115,6 +115,20 @@ public class ServerImplTest { assertTrue(server.isTerminated()); } + @Test + public void stopImmediate() { + transportServer = new SimpleServer() { + @Override + public void shutdown() { + throw new AssertionError("Should not be called, because wasn't started"); + } + }; + ServerImpl server = new ServerImpl(executor, registry, transportServer); + server.shutdown(); + assertTrue(server.isShutdown()); + assertTrue(server.isTerminated()); + } + @Test public void startStopImmediateWithChildTransport() throws IOException { ServerImpl server = new ServerImpl(executor, registry, transportServer);