diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 61b0823092..3a3b1160a6 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,5 +1,8 @@ Changes since 4.0.1 +* [HTTPCORE-207] SocketHttp*Connection classes can leak sockets if the connection is half-closed + Contributed by David Koski + * [HTTPCORE-201] OSGi Export-Package to specify release version Contributed by Oleg Kalnichevski diff --git a/httpcore/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java b/httpcore/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java index f2ddb06d40..3b968cf5c8 100644 --- a/httpcore/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java +++ b/httpcore/src/main/java/org/apache/http/impl/SocketHttpClientConnection.java @@ -245,20 +245,24 @@ public void close() throws IOException { return; } this.open = false; - doFlush(); + Socket sock = this.socket; try { + doFlush(); try { - this.socket.shutdownOutput(); - } catch (IOException ignore) { + try { + sock.shutdownOutput(); + } catch (IOException ignore) { + } + try { + sock.shutdownInput(); + } catch (IOException ignore) { + } + } catch (UnsupportedOperationException ignore) { + // if one isn't supported, the other one isn't either } - try { - this.socket.shutdownInput(); - } catch (IOException ignore) { - } - } catch (UnsupportedOperationException ignore) { - // if one isn't supported, the other one isn't either + } finally { + sock.close(); } - this.socket.close(); } } diff --git a/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java b/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java index 437bd00176..5d006c8d60 100644 --- a/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java +++ b/httpcore/src/main/java/org/apache/http/impl/SocketHttpServerConnection.java @@ -262,20 +262,25 @@ public void close() throws IOException { return; } this.open = false; - doFlush(); + this.open = false; + Socket sock = this.socket; try { + doFlush(); try { - this.socket.shutdownOutput(); - } catch (IOException ignore) { - } - try { - this.socket.shutdownInput(); - } catch (IOException ignore) { + try { + sock.shutdownOutput(); + } catch (IOException ignore) { + } + try { + sock.shutdownInput(); + } catch (IOException ignore) { + } + } catch (UnsupportedOperationException ignore) { + // if one isn't supported, the other one isn't either } - } catch (UnsupportedOperationException ignore) { - // if one isn't supported, the other one isn't either + } finally { + sock.close(); } - this.socket.close(); } }