1 /*
2  * Copyright (c) 2018, 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 /* @test
25  * @bug 8198928
26  * @library /test/lib
27  * @build jdk.test.lib.Utils
28  * @run main CloseDuringConnect
29  * @summary Attempt to cause a deadlock by closing a SocketChannel in one thread
30  *     where another thread is closing the channel after a connect fail
31  */
32 
33 import java.io.IOException;
34 import java.io.UncheckedIOException;
35 import java.net.InetAddress;
36 import java.net.InetSocketAddress;
37 import java.net.SocketAddress;
38 import java.nio.channels.SocketChannel;
39 import java.util.concurrent.Executors;
40 import java.util.concurrent.Future;
41 import java.util.concurrent.ScheduledExecutorService;
42 import java.util.stream.IntStream;
43 import static java.util.concurrent.TimeUnit.MILLISECONDS;
44 
45 import jdk.test.lib.Utils;
46 
47 public class CloseDuringConnect {
48 
49     // number of test iterations, needs to be 5-10 at least
50     static final int ITERATIONS = 50;
51 
52     // maximum delay before closing SocketChannel, in milliseconds
53     static final int MAX_DELAY_BEFORE_CLOSE = 20;
54 
55     /**
56      * Invoked by a task in the thread pool to connect to a remote address.
57      * The connection should never be established.
58      */
connect(SocketChannel sc, SocketAddress remote)59     static Void connect(SocketChannel sc, SocketAddress remote) {
60         try {
61             if (!sc.connect(remote)) {
62                 while (!sc.finishConnect()) {
63                     Thread.yield();
64                 }
65             }
66             throw new RuntimeException("Connected, should not happen");
67         } catch (IOException expected) { }
68         if (sc.isConnected())
69             throw new RuntimeException("isConnected return true, should not happen");
70         return null;
71     }
72 
73     /**
74      * Invoked by a task in the thread pool to close a socket channel.
75      */
close(SocketChannel sc)76     static Void close(SocketChannel sc) {
77         try {
78             sc.close();
79         } catch (IOException e) {
80             throw new UncheckedIOException("close failed", e);
81         }
82         return null;
83     }
84 
85     /**
86      * Test for deadlock by submitting a task to connect to the given address
87      * while another task closes the socket channel.
88      * @param pool the thread pool to submit or schedule tasks
89      * @param remote the remote address, does not accept connections
90      * @param blocking socket channel blocking mode
91      * @param delay the delay, in millis, before closing the channel
92      */
test(ScheduledExecutorService pool, SocketAddress remote, boolean blocking, long delay)93     static void test(ScheduledExecutorService pool,
94                      SocketAddress remote,
95                      boolean blocking,
96                      long delay) {
97         try {
98             SocketChannel sc = SocketChannel.open();
99             sc.configureBlocking(blocking);
100             Future<Void> r1 = pool.submit(() -> connect(sc, remote));
101             Future<Void> r2 = pool.schedule(() -> close(sc), delay, MILLISECONDS);
102             r1.get();
103             r2.get();
104         } catch (Throwable t) {
105             throw new RuntimeException("Test failed", t);
106         }
107     }
108 
main(String[] args)109     public static void main(String[] args) throws Exception {
110         SocketAddress refusing = Utils.refusingEndpoint();
111         ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
112         try {
113             IntStream.range(0, ITERATIONS).forEach(i -> {
114                 System.out.format("Iteration %d ...%n", (i + 1));
115 
116                 // Execute the test for varying delays up to MAX_DELAY_BEFORE_CLOSE,
117                 // for socket channels configured both blocking and non-blocking
118                 IntStream.range(0, MAX_DELAY_BEFORE_CLOSE).forEach(delay -> {
119                     test(pool, refusing, /*blocking mode*/true, delay);
120                     test(pool, refusing, /*blocking mode*/false, delay);
121                 });
122             });
123         } finally {
124             pool.shutdown();
125         }
126     }
127 }
128