Skip to content

Commit d37d019

Browse files
weibxiaoRob McKenna
authored and
Rob McKenna
committed
8313657: com.sun.jndi.ldap.Connection.cleanup does not close connections on SocketTimeoutErrors
Backport-of: e56d3bc2dab3d32453b6eda66e8434953c436084
1 parent 9c24db1 commit d37d019

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
@@ -643,14 +643,12 @@ void cleanup(Control[] reqCtls, boolean notifyParent) {
643643
ldapUnbind(reqCtls);
644644
}
645645
} finally {
646-
try {
647-
outStream.flush();
648-
sock.close();
649-
unpauseReader();
650-
} catch (IOException ie) {
651-
if (debug)
652-
System.err.println("Connection: problem closing socket: " + ie);
653-
}
646+
647+
flushAndCloseOutputStream();
648+
// 8313657 socket is not closed until GC is run
649+
closeOpenedSocket();
650+
tryUnpauseReader();
651+
654652
if (!notifyParent) {
655653
LdapRequest ldr = pendingRequests;
656654
while (ldr != null) {
@@ -684,6 +682,43 @@ void cleanup(Control[] reqCtls, boolean notifyParent) {
684682
}
685683
}
686684

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

688723
// Assume everything is "quiet"
689724
// "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)