From 74e945ceb48571520d1bef3cf80f0709772fdfe3 Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Wed, 19 Jun 2019 17:23:08 -0700 Subject: [PATCH] core,netty: block server shutdown until the socket is unbound --- api/src/main/java/io/grpc/Server.java | 9 +++++++-- core/src/main/java/io/grpc/internal/InternalServer.java | 4 +++- netty/src/main/java/io/grpc/netty/NettyServer.java | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/io/grpc/Server.java b/api/src/main/java/io/grpc/Server.java index b648a6e59f..828abb2d0f 100644 --- a/api/src/main/java/io/grpc/Server.java +++ b/api/src/main/java/io/grpc/Server.java @@ -29,8 +29,10 @@ import javax.annotation.concurrent.ThreadSafe; */ @ThreadSafe public abstract class Server { + /** - * Bind and start the server. + * Bind and start the server. After this call returns, clients may begin connecting to the + * listening socket(s). * * @return {@code this} object * @throws IllegalStateException if already started @@ -102,6 +104,8 @@ public abstract class Server { /** * Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected. + * After this call returns, this server has released the listening socket(s) and may be reused by + * another server. * * @return {@code this} object * @since 1.0.0 @@ -111,7 +115,8 @@ public abstract class Server { /** * Initiates a forceful shutdown in which preexisting and new calls are rejected. Although * forceful, the shutdown process is still not instantaneous; {@link #isTerminated()} will likely - * return {@code false} immediately after this method returns. + * return {@code false} immediately after this method returns. After this call returns, this + * server has released the listening socket(s) and may be reused by another server. * * @return {@code this} object * @since 1.0.0 diff --git a/core/src/main/java/io/grpc/internal/InternalServer.java b/core/src/main/java/io/grpc/internal/InternalServer.java index c3a1092342..389b24b45a 100644 --- a/core/src/main/java/io/grpc/internal/InternalServer.java +++ b/core/src/main/java/io/grpc/internal/InternalServer.java @@ -42,7 +42,9 @@ public interface InternalServer { /** * Initiates an orderly shutdown of the server. Existing transports continue, but new transports * will not be created (once {@link ServerListener#serverShutdown()} callback is called). This - * method may only be called once. + * method may only be called once. Blocks until the listening socket(s) have been closed. If + * interrupted, this method will not wait for the close to complete, but it will happen + * asynchronously. */ void shutdown(); diff --git a/netty/src/main/java/io/grpc/netty/NettyServer.java b/netty/src/main/java/io/grpc/netty/NettyServer.java index 5236594f56..11bc58bb4c 100644 --- a/netty/src/main/java/io/grpc/netty/NettyServer.java +++ b/netty/src/main/java/io/grpc/netty/NettyServer.java @@ -283,6 +283,12 @@ class NettyServer implements InternalServer, InternalWithLogId { eventLoopReferenceCounter.release(); } }); + try { + channel.closeFuture().sync(); + } catch (InterruptedException e) { + log.log(Level.FINE, "Interrupted while shutting down", e); + Thread.currentThread().interrupt(); + } } @Override