mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8336339: (se) SelectionKey.interestOps(int) should not throw ClosedSelectorException
Reviewed-by: jpai, bpb
This commit is contained in:
parent
22914e0774
commit
9f03f68755
8 changed files with 136 additions and 34 deletions
|
@ -26,7 +26,6 @@
|
||||||
package sun.nio.ch;
|
package sun.nio.ch;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.channels.ClosedSelectorException;
|
|
||||||
import java.nio.channels.SelectionKey;
|
import java.nio.channels.SelectionKey;
|
||||||
import java.nio.channels.Selector;
|
import java.nio.channels.Selector;
|
||||||
import java.nio.channels.spi.SelectorProvider;
|
import java.nio.channels.spi.SelectorProvider;
|
||||||
|
@ -92,11 +91,6 @@ class EPollSelectorImpl extends SelectorImpl {
|
||||||
EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
|
EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureOpen() {
|
|
||||||
if (!isOpen())
|
|
||||||
throw new ClosedSelectorException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int doSelect(Consumer<SelectionKey> action, long timeout)
|
protected int doSelect(Consumer<SelectionKey> action, long timeout)
|
||||||
throws IOException
|
throws IOException
|
||||||
|
@ -243,7 +237,6 @@ class EPollSelectorImpl extends SelectorImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEventOps(SelectionKeyImpl ski) {
|
public void setEventOps(SelectionKeyImpl ski) {
|
||||||
ensureOpen();
|
|
||||||
synchronized (updateLock) {
|
synchronized (updateLock) {
|
||||||
updateKeys.addLast(ski);
|
updateKeys.addLast(ski);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
package sun.nio.ch;
|
package sun.nio.ch;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.channels.ClosedSelectorException;
|
|
||||||
import java.nio.channels.SelectionKey;
|
import java.nio.channels.SelectionKey;
|
||||||
import java.nio.channels.Selector;
|
import java.nio.channels.Selector;
|
||||||
import java.nio.channels.spi.SelectorProvider;
|
import java.nio.channels.spi.SelectorProvider;
|
||||||
|
@ -97,11 +96,6 @@ class KQueueSelectorImpl extends SelectorImpl {
|
||||||
KQueue.register(kqfd, fd0, EVFILT_READ, EV_ADD);
|
KQueue.register(kqfd, fd0, EVFILT_READ, EV_ADD);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureOpen() {
|
|
||||||
if (!isOpen())
|
|
||||||
throw new ClosedSelectorException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int doSelect(Consumer<SelectionKey> action, long timeout)
|
protected int doSelect(Consumer<SelectionKey> action, long timeout)
|
||||||
throws IOException
|
throws IOException
|
||||||
|
@ -285,7 +279,6 @@ class KQueueSelectorImpl extends SelectorImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEventOps(SelectionKeyImpl ski) {
|
public void setEventOps(SelectionKeyImpl ski) {
|
||||||
ensureOpen();
|
|
||||||
synchronized (updateLock) {
|
synchronized (updateLock) {
|
||||||
updateKeys.addLast(ski);
|
updateKeys.addLast(ski);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -137,6 +137,8 @@ public abstract class AbstractSelectableChannel
|
||||||
|
|
||||||
void removeKey(SelectionKey k) { // package-private
|
void removeKey(SelectionKey k) { // package-private
|
||||||
synchronized (keyLock) {
|
synchronized (keyLock) {
|
||||||
|
if (keys == null)
|
||||||
|
return;
|
||||||
for (int i = 0; i < keys.length; i++)
|
for (int i = 0; i < keys.length; i++)
|
||||||
if (keys[i] == k) {
|
if (keys[i] == k) {
|
||||||
keys[i] = null;
|
keys[i] = null;
|
||||||
|
@ -233,7 +235,7 @@ public abstract class AbstractSelectableChannel
|
||||||
k.interestOps(ops);
|
k.interestOps(ops);
|
||||||
} else {
|
} else {
|
||||||
// New registration
|
// New registration
|
||||||
k = ((AbstractSelector)sel).register(this, ops, att);
|
k = ((AbstractSelector) sel).register(this, ops, att);
|
||||||
addKey(k);
|
addKey(k);
|
||||||
}
|
}
|
||||||
return k;
|
return k;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -188,11 +188,11 @@ public abstract class SelectorImpl
|
||||||
// Deregister channels
|
// Deregister channels
|
||||||
Iterator<SelectionKey> i = keys.iterator();
|
Iterator<SelectionKey> i = keys.iterator();
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
SelectionKeyImpl ski = (SelectionKeyImpl)i.next();
|
SelectionKeyImpl ski = (SelectionKeyImpl) i.next();
|
||||||
deregister(ski);
|
deregister(ski);
|
||||||
SelectableChannel selch = ski.channel();
|
SelectableChannel selch = ski.channel();
|
||||||
if (!selch.isOpen() && !selch.isRegistered())
|
if (!selch.isOpen() && !selch.isRegistered())
|
||||||
((SelChImpl)selch).kill();
|
((SelChImpl) selch).kill();
|
||||||
selectedKeys.remove(ski);
|
selectedKeys.remove(ski);
|
||||||
i.remove();
|
i.remove();
|
||||||
}
|
}
|
||||||
|
@ -221,13 +221,14 @@ public abstract class SelectorImpl
|
||||||
keys.add(k);
|
keys.add(k);
|
||||||
try {
|
try {
|
||||||
k.interestOps(ops);
|
k.interestOps(ops);
|
||||||
} catch (ClosedSelectorException e) {
|
} catch (CancelledKeyException e) {
|
||||||
|
// key observed and cancelled. Okay to return a cancelled key.
|
||||||
|
}
|
||||||
|
if (!isOpen()) {
|
||||||
assert ch.keyFor(this) == null;
|
assert ch.keyFor(this) == null;
|
||||||
keys.remove(k);
|
keys.remove(k);
|
||||||
k.cancel();
|
k.cancel();
|
||||||
throw e;
|
throw new ClosedSelectorException();
|
||||||
} catch (CancelledKeyException e) {
|
|
||||||
// key observed and cancelled. Okay to return a cancelled key.
|
|
||||||
}
|
}
|
||||||
return k;
|
return k;
|
||||||
}
|
}
|
||||||
|
@ -277,7 +278,7 @@ public abstract class SelectorImpl
|
||||||
|
|
||||||
SelectableChannel ch = ski.channel();
|
SelectableChannel ch = ski.channel();
|
||||||
if (!ch.isOpen() && !ch.isRegistered())
|
if (!ch.isOpen() && !ch.isRegistered())
|
||||||
((SelChImpl)ch).kill();
|
((SelChImpl) ch).kill();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,7 +231,6 @@ class PollSelectorImpl extends SelectorImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEventOps(SelectionKeyImpl ski) {
|
public void setEventOps(SelectionKeyImpl ski) {
|
||||||
ensureOpen();
|
|
||||||
synchronized (updateLock) {
|
synchronized (updateLock) {
|
||||||
updateKeys.addLast(ski);
|
updateKeys.addLast(ski);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,6 @@ package sun.nio.ch;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.ClosedSelectorException;
|
|
||||||
import java.nio.channels.Pipe;
|
import java.nio.channels.Pipe;
|
||||||
import java.nio.channels.SelectionKey;
|
import java.nio.channels.SelectionKey;
|
||||||
import java.nio.channels.Selector;
|
import java.nio.channels.Selector;
|
||||||
|
@ -89,11 +88,6 @@ class WEPollSelectorImpl extends SelectorImpl {
|
||||||
WEPoll.ctl(eph, EPOLL_CTL_ADD, fd0Val, WEPoll.EPOLLIN);
|
WEPoll.ctl(eph, EPOLL_CTL_ADD, fd0Val, WEPoll.EPOLLIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureOpen() {
|
|
||||||
if (!isOpen())
|
|
||||||
throw new ClosedSelectorException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int doSelect(Consumer<SelectionKey> action, long timeout)
|
protected int doSelect(Consumer<SelectionKey> action, long timeout)
|
||||||
throws IOException
|
throws IOException
|
||||||
|
@ -228,7 +222,6 @@ class WEPollSelectorImpl extends SelectorImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEventOps(SelectionKeyImpl ski) {
|
public void setEventOps(SelectionKeyImpl ski) {
|
||||||
ensureOpen();
|
|
||||||
synchronized (updateLock) {
|
synchronized (updateLock) {
|
||||||
updateKeys.addLast(ski);
|
updateKeys.addLast(ski);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved.
|
* Copyright (c) 2002, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -606,7 +606,6 @@ class WindowsSelectorImpl extends SelectorImpl {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setEventOps(SelectionKeyImpl ski) {
|
public void setEventOps(SelectionKeyImpl ski) {
|
||||||
ensureOpen();
|
|
||||||
synchronized (updateLock) {
|
synchronized (updateLock) {
|
||||||
updateKeys.addLast(ski);
|
updateKeys.addLast(ski);
|
||||||
}
|
}
|
||||||
|
|
122
test/jdk/java/nio/channels/Selector/RaceUpdatesAndClose.java
Normal file
122
test/jdk/java/nio/channels/Selector/RaceUpdatesAndClose.java
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @test
|
||||||
|
* @bug 8336339
|
||||||
|
* @summary Race registration and selection key updates with Selector.close
|
||||||
|
* @run junit RaceUpdatesAndClose
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.nio.channels.CancelledKeyException;
|
||||||
|
import java.nio.channels.ClosedSelectorException;
|
||||||
|
import java.nio.channels.DatagramChannel;
|
||||||
|
import java.nio.channels.SelectionKey;
|
||||||
|
import java.nio.channels.Selector;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Phaser;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.RepeatedTest;
|
||||||
|
|
||||||
|
class RaceUpdatesAndClose {
|
||||||
|
private static ExecutorService executor;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void setup() throws Exception {
|
||||||
|
executor = Executors.newFixedThreadPool(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
static void finish() {
|
||||||
|
executor.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Race SelectableChannel.register and Selector.close.
|
||||||
|
*/
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void raceRegisterAndClose() throws Exception {
|
||||||
|
try (Selector sel = Selector.open();
|
||||||
|
DatagramChannel dc = DatagramChannel.open()) {
|
||||||
|
|
||||||
|
dc.configureBlocking(false);
|
||||||
|
|
||||||
|
Phaser phaser = new Phaser(2);
|
||||||
|
|
||||||
|
// register
|
||||||
|
var task1 = executor.submit(() -> {
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
try {
|
||||||
|
dc.register(sel, SelectionKey.OP_READ);
|
||||||
|
} catch (ClosedSelectorException e) { }
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
// close selector
|
||||||
|
var task2 = executor.submit(() -> {
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
sel.close();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
task1.get();
|
||||||
|
task2.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Race SelectionKey.interestOps and Selector.close.
|
||||||
|
*/
|
||||||
|
@RepeatedTest(100)
|
||||||
|
void raceInterestOpsAndClose() throws Exception {
|
||||||
|
try (Selector sel = Selector.open();
|
||||||
|
DatagramChannel dc = DatagramChannel.open()) {
|
||||||
|
|
||||||
|
dc.configureBlocking(false);
|
||||||
|
SelectionKey key = dc.register(sel, SelectionKey.OP_READ);
|
||||||
|
|
||||||
|
Phaser phaser = new Phaser(2);
|
||||||
|
|
||||||
|
// interestOps
|
||||||
|
var task1 = executor.submit(() -> {
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
try {
|
||||||
|
key.interestOps(0);
|
||||||
|
} catch (CancelledKeyException e) { }
|
||||||
|
});
|
||||||
|
|
||||||
|
// close selector
|
||||||
|
var task2 = executor.submit(() -> {
|
||||||
|
phaser.arriveAndAwaitAdvance();
|
||||||
|
sel.close();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
task1.get();
|
||||||
|
task2.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue