From efbebc6e843850b7ed9a1d015413c99f114a7d92 Mon Sep 17 00:00:00 2001 From: Rajini Sivaram Date: Mon, 9 Nov 2015 07:23:47 -0800 Subject: [PATCH] KAFKA-2779; Close SSL socket channel on remote connection close Close socket channel in finally block to avoid file descriptor leak when remote end closes the connection Author: Rajini Sivaram Reviewers: Ismael Juma , Jun Rao Closes #460 from rajinisivaram/KAFKA-2779 --- .../kafka/common/network/SslTransportLayer.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/clients/src/main/java/org/apache/kafka/common/network/SslTransportLayer.java b/clients/src/main/java/org/apache/kafka/common/network/SslTransportLayer.java index e2d6b3bcb073e..ffd2a9ef0f212 100644 --- a/clients/src/main/java/org/apache/kafka/common/network/SslTransportLayer.java +++ b/clients/src/main/java/org/apache/kafka/common/network/SslTransportLayer.java @@ -137,11 +137,9 @@ public boolean isConnected() { /** * Sends a SSL close message and closes socketChannel. - * @throws IOException if an I/O error occurs - * @throws IOException if there is data on the outgoing network buffer and we are unable to flush it */ @Override - public void close() throws IOException { + public void close() { if (closing) return; closing = true; sslEngine.closeOutbound(); @@ -159,10 +157,15 @@ public void close() throws IOException { } netWriteBuffer.flip(); flush(netWriteBuffer); - socketChannel.socket().close(); - socketChannel.close(); } catch (IOException ie) { log.warn("Failed to send SSL Close message ", ie); + } finally { + try { + socketChannel.socket().close(); + socketChannel.close(); + } catch (IOException e) { + log.warn("Failed to close SSL socket channel: " + e); + } } key.attach(null); key.cancel();