Skip to content

Commit 8300197

Browse files
Alan Batemanpull[bot]
authored andcommitted
8336339: (se) SelectionKey.interestOps(int) should not throw ClosedSelectorException
Reviewed-by: jpai, bpb
1 parent 4dc0cfb commit 8300197

File tree

8 files changed

+136
-34
lines changed

8 files changed

+136
-34
lines changed

src/java.base/linux/classes/sun/nio/ch/EPollSelectorImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package sun.nio.ch;
2727

2828
import java.io.IOException;
29-
import java.nio.channels.ClosedSelectorException;
3029
import java.nio.channels.SelectionKey;
3130
import java.nio.channels.Selector;
3231
import java.nio.channels.spi.SelectorProvider;
@@ -92,11 +91,6 @@ class EPollSelectorImpl extends SelectorImpl {
9291
EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
9392
}
9493

95-
private void ensureOpen() {
96-
if (!isOpen())
97-
throw new ClosedSelectorException();
98-
}
99-
10094
@Override
10195
protected int doSelect(Consumer<SelectionKey> action, long timeout)
10296
throws IOException
@@ -243,7 +237,6 @@ protected void implDereg(SelectionKeyImpl ski) throws IOException {
243237

244238
@Override
245239
public void setEventOps(SelectionKeyImpl ski) {
246-
ensureOpen();
247240
synchronized (updateLock) {
248241
updateKeys.addLast(ski);
249242
}

src/java.base/macosx/classes/sun/nio/ch/KQueueSelectorImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package sun.nio.ch;
2727

2828
import java.io.IOException;
29-
import java.nio.channels.ClosedSelectorException;
3029
import java.nio.channels.SelectionKey;
3130
import java.nio.channels.Selector;
3231
import java.nio.channels.spi.SelectorProvider;
@@ -97,11 +96,6 @@ class KQueueSelectorImpl extends SelectorImpl {
9796
KQueue.register(kqfd, fd0, EVFILT_READ, EV_ADD);
9897
}
9998

100-
private void ensureOpen() {
101-
if (!isOpen())
102-
throw new ClosedSelectorException();
103-
}
104-
10599
@Override
106100
protected int doSelect(Consumer<SelectionKey> action, long timeout)
107101
throws IOException
@@ -285,7 +279,6 @@ protected void implDereg(SelectionKeyImpl ski) throws IOException {
285279

286280
@Override
287281
public void setEventOps(SelectionKeyImpl ski) {
288-
ensureOpen();
289282
synchronized (updateLock) {
290283
updateKeys.addLast(ski);
291284
}

src/java.base/share/classes/java/nio/channels/spi/AbstractSelectableChannel.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, 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
@@ -137,6 +137,8 @@ private SelectionKey findKey(Selector sel) {
137137

138138
void removeKey(SelectionKey k) { // package-private
139139
synchronized (keyLock) {
140+
if (keys == null)
141+
return;
140142
for (int i = 0; i < keys.length; i++)
141143
if (keys[i] == k) {
142144
keys[i] = null;
@@ -233,7 +235,7 @@ public final SelectionKey register(Selector sel, int ops, Object att)
233235
k.interestOps(ops);
234236
} else {
235237
// New registration
236-
k = ((AbstractSelector)sel).register(this, ops, att);
238+
k = ((AbstractSelector) sel).register(this, ops, att);
237239
addKey(k);
238240
}
239241
return k;

src/java.base/share/classes/sun/nio/ch/SelectorImpl.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, 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
@@ -188,11 +188,11 @@ public final void implCloseSelector() throws IOException {
188188
// Deregister channels
189189
Iterator<SelectionKey> i = keys.iterator();
190190
while (i.hasNext()) {
191-
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
191+
SelectionKeyImpl ski = (SelectionKeyImpl) i.next();
192192
deregister(ski);
193193
SelectableChannel selch = ski.channel();
194194
if (!selch.isOpen() && !selch.isRegistered())
195-
((SelChImpl)selch).kill();
195+
((SelChImpl) selch).kill();
196196
selectedKeys.remove(ski);
197197
i.remove();
198198
}
@@ -221,13 +221,14 @@ protected final SelectionKey register(AbstractSelectableChannel ch,
221221
keys.add(k);
222222
try {
223223
k.interestOps(ops);
224-
} catch (ClosedSelectorException e) {
224+
} catch (CancelledKeyException e) {
225+
// key observed and cancelled. Okay to return a cancelled key.
226+
}
227+
if (!isOpen()) {
225228
assert ch.keyFor(this) == null;
226229
keys.remove(k);
227230
k.cancel();
228-
throw e;
229-
} catch (CancelledKeyException e) {
230-
// key observed and cancelled. Okay to return a cancelled key.
231+
throw new ClosedSelectorException();
231232
}
232233
return k;
233234
}
@@ -277,7 +278,7 @@ protected final void processDeregisterQueue() throws IOException {
277278

278279
SelectableChannel ch = ski.channel();
279280
if (!ch.isOpen() && !ch.isRegistered())
280-
((SelChImpl)ch).kill();
281+
((SelChImpl) ch).kill();
281282
}
282283
}
283284
}

src/java.base/unix/classes/sun/nio/ch/PollSelectorImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ protected void implDereg(SelectionKeyImpl ski) throws IOException {
231231

232232
@Override
233233
public void setEventOps(SelectionKeyImpl ski) {
234-
ensureOpen();
235234
synchronized (updateLock) {
236235
updateKeys.addLast(ski);
237236
}

src/java.base/windows/classes/sun/nio/ch/WEPollSelectorImpl.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.io.FileDescriptor;
2929
import java.io.IOException;
3030
import java.nio.ByteBuffer;
31-
import java.nio.channels.ClosedSelectorException;
3231
import java.nio.channels.Pipe;
3332
import java.nio.channels.SelectionKey;
3433
import java.nio.channels.Selector;
@@ -89,11 +88,6 @@ class WEPollSelectorImpl extends SelectorImpl {
8988
WEPoll.ctl(eph, EPOLL_CTL_ADD, fd0Val, WEPoll.EPOLLIN);
9089
}
9190

92-
private void ensureOpen() {
93-
if (!isOpen())
94-
throw new ClosedSelectorException();
95-
}
96-
9791
@Override
9892
protected int doSelect(Consumer<SelectionKey> action, long timeout)
9993
throws IOException
@@ -228,7 +222,6 @@ protected void implDereg(SelectionKeyImpl ski) throws IOException {
228222

229223
@Override
230224
public void setEventOps(SelectionKeyImpl ski) {
231-
ensureOpen();
232225
synchronized (updateLock) {
233226
updateKeys.addLast(ski);
234227
}

src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2024, 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
@@ -606,7 +606,6 @@ protected void implDereg(SelectionKeyImpl ski) {
606606

607607
@Override
608608
public void setEventOps(SelectionKeyImpl ski) {
609-
ensureOpen();
610609
synchronized (updateLock) {
611610
updateKeys.addLast(ski);
612611
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright (c) 2024, 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 8336339
27+
* @summary Race registration and selection key updates with Selector.close
28+
* @run junit RaceUpdatesAndClose
29+
*/
30+
31+
import java.nio.channels.CancelledKeyException;
32+
import java.nio.channels.ClosedSelectorException;
33+
import java.nio.channels.DatagramChannel;
34+
import java.nio.channels.SelectionKey;
35+
import java.nio.channels.Selector;
36+
import java.util.concurrent.ExecutorService;
37+
import java.util.concurrent.Executors;
38+
import java.util.concurrent.Phaser;
39+
40+
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.AfterAll;
42+
import org.junit.jupiter.api.RepeatedTest;
43+
44+
class RaceUpdatesAndClose {
45+
private static ExecutorService executor;
46+
47+
@BeforeAll
48+
static void setup() throws Exception {
49+
executor = Executors.newFixedThreadPool(2);
50+
}
51+
52+
@AfterAll
53+
static void finish() {
54+
executor.shutdown();
55+
}
56+
57+
/**
58+
* Race SelectableChannel.register and Selector.close.
59+
*/
60+
@RepeatedTest(100)
61+
void raceRegisterAndClose() throws Exception {
62+
try (Selector sel = Selector.open();
63+
DatagramChannel dc = DatagramChannel.open()) {
64+
65+
dc.configureBlocking(false);
66+
67+
Phaser phaser = new Phaser(2);
68+
69+
// register
70+
var task1 = executor.submit(() -> {
71+
phaser.arriveAndAwaitAdvance();
72+
try {
73+
dc.register(sel, SelectionKey.OP_READ);
74+
} catch (ClosedSelectorException e) { }
75+
return null;
76+
});
77+
78+
// close selector
79+
var task2 = executor.submit(() -> {
80+
phaser.arriveAndAwaitAdvance();
81+
sel.close();
82+
return null;
83+
});
84+
85+
task1.get();
86+
task2.get();
87+
}
88+
}
89+
90+
/**
91+
* Race SelectionKey.interestOps and Selector.close.
92+
*/
93+
@RepeatedTest(100)
94+
void raceInterestOpsAndClose() throws Exception {
95+
try (Selector sel = Selector.open();
96+
DatagramChannel dc = DatagramChannel.open()) {
97+
98+
dc.configureBlocking(false);
99+
SelectionKey key = dc.register(sel, SelectionKey.OP_READ);
100+
101+
Phaser phaser = new Phaser(2);
102+
103+
// interestOps
104+
var task1 = executor.submit(() -> {
105+
phaser.arriveAndAwaitAdvance();
106+
try {
107+
key.interestOps(0);
108+
} catch (CancelledKeyException e) { }
109+
});
110+
111+
// close selector
112+
var task2 = executor.submit(() -> {
113+
phaser.arriveAndAwaitAdvance();
114+
sel.close();
115+
return null;
116+
});
117+
118+
task1.get();
119+
task2.get();
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)