Skip to content

Commit bcac47f

Browse files
Andrew LuGoeLin
Andrew Lu
authored andcommitted
8313657: com.sun.jndi.ldap.Connection.cleanup does not close connections on SocketTimeoutErrors
Backport-of: e56d3bc2dab3d32453b6eda66e8434953c436084
1 parent e7b2077 commit bcac47f

File tree

2 files changed

+212
-9
lines changed

2 files changed

+212
-9
lines changed

src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -638,14 +638,12 @@ void cleanup(Control[] reqCtls, boolean notifyParent) {
638638
ldapUnbind(reqCtls);
639639
}
640640
} finally {
641-
try {
642-
outStream.flush();
643-
sock.close();
644-
unpauseReader();
645-
} catch (IOException ie) {
646-
if (debug)
647-
System.err.println("Connection: problem closing socket: " + ie);
648-
}
641+
642+
flushAndCloseOutputStream();
643+
// 8313657 socket is not closed until GC is run
644+
closeOpenedSocket();
645+
tryUnpauseReader();
646+
649647
if (!notifyParent) {
650648
LdapRequest ldr = pendingRequests;
651649
while (ldr != null) {
@@ -679,6 +677,43 @@ void cleanup(Control[] reqCtls, boolean notifyParent) {
679677
}
680678
}
681679

680+
// flush and close output stream
681+
private void flushAndCloseOutputStream() {
682+
try {
683+
outStream.flush();
684+
} catch (IOException ioEx) {
685+
if (debug)
686+
System.err.println("Connection.flushOutputStream: OutputStream flush problem " + ioEx);
687+
}
688+
try {
689+
outStream.close();
690+
} catch (IOException ioEx) {
691+
if (debug)
692+
System.err.println("Connection.closeOutputStream: OutputStream close problem " + ioEx);
693+
}
694+
}
695+
696+
// close socket
697+
private void closeOpenedSocket() {
698+
try {
699+
sock.close();
700+
} catch (IOException ioEx) {
701+
if (debug) {
702+
System.err.println("Connection.closeConnectionSocket: Socket close problem: " + ioEx);
703+
System.err.println("Socket isClosed: " + sock.isClosed());
704+
}
705+
}
706+
}
707+
708+
// unpause reader
709+
private void tryUnpauseReader() {
710+
try {
711+
unpauseReader();
712+
} catch (IOException ioEx) {
713+
if (debug)
714+
System.err.println("Connection.tryUnpauseReader: unpauseReader problem " + ioEx);
715+
}
716+
}
682717

683718
// Assume everything is "quiet"
684719
// "synchronize" might lead to deadlock so don't synchronize method
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import javax.naming.Context;
25+
import javax.naming.directory.DirContext;
26+
import javax.naming.directory.InitialDirContext;
27+
import javax.net.SocketFactory;
28+
import java.net.InetAddress;
29+
import java.net.Socket;
30+
import java.net.SocketAddress;
31+
import java.io.ByteArrayInputStream;
32+
import java.io.IOException;
33+
import java.io.InputStream;
34+
import java.io.OutputStream;
35+
import java.util.Hashtable;
36+
37+
import jdk.test.lib.process.OutputAnalyzer;
38+
import jdk.test.lib.process.ProcessTools;
39+
40+
/*
41+
* @test
42+
* @bug 8313657
43+
* @summary make sure socket is closed when the error happens for OutputStream flushing
44+
* The value of provider url can be random, not necessary to be the one in the code
45+
* @library /test/lib
46+
* @run main/othervm SocketCloseTest
47+
*/
48+
49+
public class SocketCloseTest {
50+
public static String SOCKET_CLOSED_MSG = "The socket has been closed.";
51+
public static String SOCKET_NOT_CLOSED_MSG = "The socket was not closed.";
52+
public static String BAD_FLUSH = "Bad flush!";
53+
private static final byte[] BIND_RESPONSE = new byte[]{
54+
48, 12, 2, 1, 1, 97, 7, 10, 1, 0, 4, 0, 4, 0
55+
};
56+
57+
public static void main(String[] args) throws Exception {
58+
SocketCloseTest scTest = new SocketCloseTest();
59+
scTest.runCloseSocketScenario();
60+
}
61+
62+
public void runCloseSocketScenario() throws Exception {
63+
Hashtable<String, Object> props = new Hashtable<>();
64+
65+
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
66+
props.put(Context.PROVIDER_URL, "ldap://localhost:1389/o=example");
67+
props.put("java.naming.ldap.factory.socket", CustomSocketFactory.class.getName());
68+
try {
69+
final DirContext ctx = new InitialDirContext(props);
70+
} catch (Exception e) {
71+
if (CustomSocketFactory.customSocket.closeMethodCalledCount() > 0) {
72+
System.out.println(SOCKET_CLOSED_MSG);
73+
} else {
74+
System.out.println(SOCKET_NOT_CLOSED_MSG);
75+
throw e;
76+
}
77+
}
78+
}
79+
80+
public static class CustomSocketFactory extends SocketFactory {
81+
public static CustomSocket customSocket = new CustomSocket();
82+
83+
public static CustomSocketFactory getDefault() {
84+
return new CustomSocketFactory();
85+
}
86+
87+
@Override
88+
public Socket createSocket() {
89+
return customSocket;
90+
}
91+
92+
@Override
93+
public Socket createSocket(String s, int timeout) {
94+
return customSocket;
95+
}
96+
97+
@Override
98+
public Socket createSocket(String host, int port, InetAddress localHost,
99+
int localPort) {
100+
return customSocket;
101+
}
102+
103+
@Override
104+
public Socket createSocket(InetAddress host, int port) {
105+
return customSocket;
106+
}
107+
108+
@Override
109+
public Socket createSocket(InetAddress address, int port,
110+
InetAddress localAddress, int localPort) {
111+
return customSocket;
112+
}
113+
}
114+
115+
private static class LdapInputStream extends InputStream {
116+
private ByteArrayInputStream bos;
117+
118+
public LdapInputStream() {
119+
}
120+
121+
@Override
122+
public int read() throws IOException {
123+
bos = new ByteArrayInputStream(BIND_RESPONSE);
124+
return bos.read();
125+
}
126+
}
127+
128+
private static class LdapOutputStream extends OutputStream {
129+
130+
@Override
131+
public void write(int b) throws IOException {
132+
System.out.println("output stream writing");
133+
}
134+
135+
@Override
136+
public void flush() throws IOException {
137+
System.out.println(BAD_FLUSH);
138+
throw new IOException(BAD_FLUSH);
139+
}
140+
}
141+
142+
private static class CustomSocket extends Socket {
143+
private int closeMethodCalled = 0;
144+
private LdapOutputStream output = new LdapOutputStream();
145+
private LdapInputStream input = new LdapInputStream();
146+
147+
public void connect(SocketAddress address, int timeout) {
148+
}
149+
150+
public InputStream getInputStream() {
151+
return input;
152+
}
153+
154+
public OutputStream getOutputStream() {
155+
return output;
156+
}
157+
158+
public int closeMethodCalledCount() {
159+
return closeMethodCalled;
160+
}
161+
162+
@Override
163+
public void close() throws IOException {
164+
closeMethodCalled++;
165+
super.close();
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)