Skip to content

Commit 69d8669

Browse files
author
Alan Bateman
committed
8278339: ServerSocket::isClosed may return false after accept throws
Reviewed-by: dfuchs
1 parent 56ca66e commit 69d8669

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

src/java.base/share/classes/java/net/ServerSocket.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,15 +711,12 @@ private void ensureCompatible(SocketImpl si) throws IOException {
711711
public void close() throws IOException {
712712
synchronized (stateLock) {
713713
if (!closed) {
714-
try {
715-
// close underlying socket if created
716-
if (created) {
717-
impl.close();
718-
}
719-
} finally {
720-
closed = true;
721-
}
714+
closed = true;
722715

716+
// close underlying socket if created
717+
if (created) {
718+
impl.close();
719+
}
723720
}
724721
}
725722
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2021, 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+
/**
25+
* @test
26+
* @bug 8278339
27+
* @summary Test that ServerSocket::isClosed returns true after async close
28+
*/
29+
30+
import java.io.IOException;
31+
import java.net.InetAddress;
32+
import java.net.InetSocketAddress;
33+
import java.net.ServerSocket;
34+
import java.net.Socket;
35+
36+
public class IsClosedAfterAsyncClose {
37+
38+
private static final int ITERATIONS = 100;
39+
40+
public static void main(String[] args) throws Exception {
41+
for (int i = 0; i < ITERATIONS; i++) {
42+
System.out.printf("Test %d...%n", i);
43+
44+
// create listener bound to the loopback address
45+
ServerSocket listener = new ServerSocket();
46+
InetAddress loopback = InetAddress.getLoopbackAddress();
47+
listener.bind(new InetSocketAddress(loopback, 0));
48+
49+
// task to close listener after a delay
50+
Runnable closeListener = () -> {
51+
try {
52+
Thread.sleep(100);
53+
listener.close();
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
57+
};
58+
59+
// main thread blocks in accept. When listener is closed then accept
60+
// should wakeup with an IOException and isClosed should be true.
61+
try (listener) {
62+
Thread closer = new Thread(closeListener);
63+
closer.start();
64+
try {
65+
while (true) {
66+
Socket s = listener.accept();
67+
// close spurious connection
68+
s.close();
69+
}
70+
} catch (IOException ioe) {
71+
if (!listener.isClosed()) {
72+
throw new RuntimeException("isClosed returned false!!");
73+
}
74+
} finally {
75+
closer.join();
76+
}
77+
}
78+
}
79+
}
80+
}
81+

0 commit comments

Comments
 (0)