From 747533408dbf3fa04fe0753e0f20cf80b8ac66ed Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 22 Sep 2015 17:00:16 +0200 Subject: [PATCH] [#4170] Shutdown socket before close fd when using epoll transport Motivation: We should call shutdown(...) on the socket before closing the filedescriptor to ensure it is closed gracefully. Modifications: Call shutdown(...) before close. Result: Sockets are gracefully shutdown when using native transport. --- .../channel/epoll/AbstractEpollChannel.java | 18 +++++++++++++++--- .../epoll/AbstractEpollStreamChannel.java | 5 +++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java index 491e79dcaf..93e56e990a 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollChannel.java @@ -102,17 +102,29 @@ abstract class AbstractEpollChannel extends AbstractChannel implements UnixChann @Override protected void doClose() throws Exception { - active = false; + boolean active = this.active; + this.active = false; + FileDescriptor fd = fileDescriptor; try { - // deregister from epoll now + // deregister from epoll now and shutdown the socket. doDeregister(); + if (active) { + shutdown(fd.intValue()); + } } finally { // Ensure the file descriptor is closed in all cases. - FileDescriptor fd = fileDescriptor; fd.close(); } } + /** + * Called on {@link #doClose()} before the actual {@link FileDescriptor} is closed. + * This implementation does nothing. + */ + protected void shutdown(int fd) throws IOException { + // NOOP + } + @Override protected void doDisconnect() throws Exception { doClose(); diff --git a/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java b/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java index b66f90f30e..9dc95d01ec 100644 --- a/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java +++ b/transport-native-epoll/src/main/java/io/netty/channel/epoll/AbstractEpollStreamChannel.java @@ -93,6 +93,11 @@ public abstract class AbstractEpollStreamChannel extends AbstractEpollChannel { flags |= Native.EPOLLRDHUP; } + @Override + protected void shutdown(int fd) throws IOException { + Native.shutdown(fd, true, true); + } + @Override protected AbstractEpollUnsafe newUnsafe() { return new EpollStreamUnsafe();