Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
shutdownInput/Output is not idempotent, so attempting to reclose a closed socket fails.
  • Loading branch information
kohsuke committed Jun 9, 2014
1 parent 546728f commit 4bb086e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/main/java/org/jenkinsci/remoting/nio/Closeables.java
Expand Up @@ -5,6 +5,8 @@
import java.net.Socket;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Creates {@link Closeable} that does socket half-close.
Expand All @@ -17,7 +19,14 @@ public static Closeable input(SelectableChannel ch) {
final SocketChannel s = (SocketChannel) ch;
return new Closeable() {
public void close() throws IOException {
s.socket().shutdownInput();
try {
s.socket().shutdownInput();
} catch (IOException e) {
// at least as of Java7u55, shutdownInput fails if the socket
// is already closed or half-closed, as opposed to be a no-op.
// so let's just ignore close error altogether
LOGGER.log(Level.FINE, "Failed to close "+s,e);
}
maybeClose(s);
}
};
Expand All @@ -30,7 +39,12 @@ public static Closeable output(SelectableChannel ch) {
final SocketChannel s = (SocketChannel) ch;
return new Closeable() {
public void close() throws IOException {
s.socket().shutdownOutput();
try {
s.socket().shutdownOutput();
} catch (IOException e) {
// see the discussion in try/catch block around shutdownInput above
LOGGER.log(Level.FINE, "Failed to close "+s,e);
}
maybeClose(s);
}
};
Expand All @@ -48,4 +62,6 @@ private static void maybeClose(SocketChannel sc) throws IOException {
sc.close();
}
}

private static final Logger LOGGER = Logger.getLogger(Closeables.class.getName());
}

0 comments on commit 4bb086e

Please sign in to comment.