Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/Selector/RegAfterPreClose.java
41153 views
1
/*
2
* Copyright (c) 2009, 2010, 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 6693490
26
* @summary Pre-close file descriptor may inadvertently get registered with
27
* epoll during close
28
*/
29
30
import java.net.*;
31
import java.nio.channels.*;
32
import java.util.concurrent.*;
33
import java.util.*;
34
import java.io.IOException;
35
36
public class RegAfterPreClose {
37
38
static final int TEST_ITERATIONS = 300;
39
static volatile boolean done;
40
41
/**
42
* A task that continuously connects to a given address and immediately
43
* closes the connection.
44
*/
45
static class Connector implements Runnable {
46
private final SocketAddress sa;
47
Connector(int port) throws IOException {
48
InetAddress lh = InetAddress.getLocalHost();
49
this.sa = new InetSocketAddress(lh, port);
50
}
51
public void run() {
52
while (!done) {
53
try {
54
SocketChannel.open(sa).close();
55
} catch (IOException x) {
56
// back-off as probably resource related
57
try {
58
Thread.sleep(10);
59
} catch (InterruptedException ignore) { }
60
}
61
}
62
}
63
}
64
65
/**
66
* A task that closes a channel.
67
*/
68
static class Closer implements Runnable {
69
private final Channel channel;
70
Closer(Channel sc) {
71
this.channel = sc;
72
}
73
public void run() {
74
try {
75
channel.close();
76
} catch (IOException ignore) { }
77
}
78
}
79
80
public static void main(String[] args) throws Exception {
81
// create listener
82
InetSocketAddress isa = new InetSocketAddress(0);
83
ServerSocketChannel ssc = ServerSocketChannel.open();
84
ssc.socket().bind(isa);
85
86
// register with Selector
87
final Selector sel = Selector.open();
88
ssc.configureBlocking(false);
89
SelectionKey key = ssc.register(sel, SelectionKey.OP_ACCEPT);
90
91
ThreadFactory factory = new ThreadFactory() {
92
@Override
93
public Thread newThread(Runnable r) {
94
Thread t = new Thread(r);
95
t.setDaemon(true);
96
return t;
97
}
98
};
99
100
// create Executor that handles tasks that closes channels
101
// "asynchronously" - this creates the conditions to provoke the bug.
102
ExecutorService executor = Executors.newFixedThreadPool(2, factory);
103
104
// submit task that connects to listener
105
executor.execute(new Connector(ssc.socket().getLocalPort()));
106
107
// loop accepting connections until done (or an IOException is thrown)
108
int remaining = TEST_ITERATIONS;
109
while (remaining > 0) {
110
sel.select();
111
if (key.isAcceptable()) {
112
SocketChannel sc = ssc.accept();
113
if (sc != null) {
114
remaining--;
115
sc.configureBlocking(false);
116
sc.register(sel, SelectionKey.OP_READ);
117
executor.execute(new Closer(sc));
118
}
119
}
120
sel.selectedKeys().clear();
121
}
122
done = true;
123
sel.close();
124
executor.shutdown();
125
}
126
}
127
128