Skip to content

Commit

Permalink
[netty#4170] Shutdown socket before close fd when using epoll transport
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
normanmaurer committed Sep 25, 2015
1 parent 956a757 commit 7475334
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,29 @@ public ChannelMetadata metadata() {

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ protected AbstractEpollStreamChannel(FileDescriptor fd) {
flags |= Native.EPOLLRDHUP;
}

@Override
protected void shutdown(int fd) throws IOException {
Native.shutdown(fd, true, true);
}

@Override
protected AbstractEpollUnsafe newUnsafe() {
return new EpollStreamUnsafe();
Expand Down

0 comments on commit 7475334

Please sign in to comment.