Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,6 @@ void cleanup(Control[] reqCtls, boolean notifyParent) {
} finally {

flushAndCloseOutputStream();
// 8313657 socket is not closed until GC is run
closeOpenedSocket(sock);
tryUnpauseReader();

if (!notifyParent) {
Expand All @@ -705,7 +703,6 @@ void cleanup(Control[] reqCtls, boolean notifyParent) {
tlsHandshakeListener.tlsHandshakeCompleted.cancel(false);
}
}
sock = null;
}
nparent = notifyParent;
}
Expand All @@ -732,12 +729,6 @@ private void flushAndCloseOutputStream() {
if (debug)
System.err.println("Connection.flushOutputStream: OutputStream flush problem " + ioEx);
}
try {
outStream.close();
} catch (IOException ioEx) {
if (debug)
System.err.println("Connection.closeOutputStream: OutputStream close problem " + ioEx);
}
}

// close socket
Expand Down
20 changes: 20 additions & 0 deletions src/java.naming/share/classes/com/sun/jndi/ldap/LdapClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,13 @@ void close(Control[] reqCtls, boolean hardClose) {
if (!pooled) {
// Not being pooled; continue with closing
conn.cleanup(reqCtls, false);
closeOpenedResource();
} else {
// Pooled
// Is this a real close or a request to return conn to pool
if (hardClose) {
conn.cleanup(reqCtls, false);
closeOpenedResource();
pcb.removePooledConnection(this);
} else {
pcb.releasePooledConnection(this);
Expand All @@ -477,6 +479,23 @@ void close(Control[] reqCtls, boolean hardClose) {
}
}

// 8313657 socket is not closed until GC is run
private void closeOpenedResource() {
try {
if (conn != null) {
if (conn.outStream != null) {
conn.outStream.close();
}

if (conn.sock != null && !conn.sock.isClosed()) {
conn.sock.close();
}
}
} catch (IOException ioEx) {
//ignore the error;
}
}

// NOTE: Should NOT be synchronized otherwise won't be able to close
private void forceClose(boolean cleanPool) {
referenceCount = 0; // force closing of connection
Expand All @@ -489,6 +508,7 @@ private void forceClose(boolean cleanPool) {
"LdapClient: forced close of connection " + this);
}
conn.cleanup(null, false);
closeOpenedResource();
if (cleanPool) {
pcb.removePooledConnection(this);
}
Expand Down
14 changes: 1 addition & 13 deletions test/jdk/com/sun/jndi/ldap/SocketCloseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void runCloseSocketScenario() throws Exception {
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, "ldap://localhost:1389/o=example");
props.put("java.naming.ldap.factory.socket", CustomSocketFactory.class.getName());
props.put("com.sun.jndi.ldap.connect.timeout", 100+"");
try {
final DirContext ctx = new InitialDirContext(props);
} catch (Exception e) {
Expand Down Expand Up @@ -141,19 +142,6 @@ public void flush() throws IOException {

private static class CustomSocket extends Socket {
private int closeMethodCalled = 0;
private LdapOutputStream output = new LdapOutputStream();
private LdapInputStream input = new LdapInputStream();

public void connect(SocketAddress address, int timeout) {
}

public InputStream getInputStream() {
return input;
}

public OutputStream getOutputStream() {
return output;
}

public int closeMethodCalledCount() {
return closeMethodCalled;
Expand Down