author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
13582
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
1 |
/* |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
4 |
* |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
8 |
* |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
14 |
* |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
18 |
* |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
21 |
* questions. |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
22 |
*/ |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
23 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
24 |
/* @test |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
25 |
* @bug 7132889 |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
26 |
* @summary Test that register does not return a valid SelectionKey when |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
27 |
* invoked at around the time that the channel is closed |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
28 |
*/ |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
29 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
30 |
import java.nio.channels.*; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
31 |
import java.util.concurrent.*; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
32 |
import java.util.Random; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
33 |
import java.io.IOException; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
34 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
35 |
public class RacyRegister { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
36 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
37 |
public static void main(String[] args) throws Exception { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
38 |
ExecutorService pool = Executors.newFixedThreadPool(1); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
39 |
try (Selector sel = Selector.open()) { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
40 |
int count = 100; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
41 |
while (count-- > 0) { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
42 |
final SocketChannel sc = SocketChannel.open(); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
43 |
sc.configureBlocking(false); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
44 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
45 |
// close channel asynchronously |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
46 |
Future<Void> result = pool.submit(new Callable<Void>() { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
47 |
public Void call() throws IOException { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
48 |
sc.close(); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
49 |
return null; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
50 |
} |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
51 |
}); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
52 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
53 |
// attempt to register channel with Selector |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
54 |
SelectionKey key = null; |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
55 |
try { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
56 |
key = sc.register(sel, SelectionKey.OP_READ); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
57 |
} catch (ClosedChannelException ignore) { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
58 |
} |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
59 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
60 |
// ensure close is done |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
61 |
result.get(); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
62 |
|
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
63 |
// if we have a key then it should be invalid |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
64 |
if (key != null && key.isValid()) |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
65 |
throw new RuntimeException("Key is valid"); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
66 |
} |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
67 |
} finally { |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
68 |
pool.shutdown(); |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
69 |
} |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
70 |
} |
16f0af616ea0
7132889: (se) AbstractSelectableChannel.register and configureBlocking not safe from asynchronous close
alanb
parents:
diff
changeset
|
71 |
} |