8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set"
Reviewed-by: alanb
--- a/test/jdk/java/nio/channels/Selector/RegisterDuringSelect.java Thu Aug 30 17:59:40 2018 -0700
+++ b/test/jdk/java/nio/channels/Selector/RegisterDuringSelect.java Fri Aug 31 10:00:22 2018 +0800
@@ -23,94 +23,92 @@
/* @test
* @bug 8201315
+ * @build SelectorUtils
+ * @run main RegisterDuringSelect
* @summary Test that channels can be registered, interest ops can changed,
* and keys cancelled while a selection operation is in progress.
*/
import java.io.IOException;
-import java.nio.channels.ClosedSelectorException;
import java.nio.channels.Pipe;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.Phaser;
public class RegisterDuringSelect {
+ interface TestOperation {
+ void accept(Thread t, Selector sel, Pipe.SourceChannel sc) throws Exception;
+ }
+ static class Test {
+ final Selector sel;
+ final Pipe p;
+ final Pipe.SourceChannel sc;
- static Callable<Void> selectLoop(Selector sel, Phaser barrier) {
- return new Callable<Void>() {
- @Override
- public Void call() throws IOException {
- for (;;) {
+ Test() throws Exception {
+ sel = Selector.open();
+ p = Pipe.open();
+ sc = p.source();
+ sc.configureBlocking(false);
+ }
+ void test(TestOperation op) throws Exception {
+ try {
+ Thread t = new Thread(() -> {
try {
sel.select();
- } catch (ClosedSelectorException ignore) {
- return null;
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
}
- if (sel.isOpen()) {
- barrier.arriveAndAwaitAdvance();
- System.out.println("selectLoop advanced ...");
- } else {
- // closed
- return null;
- }
- }
+ });
+ t.start();
+ op.accept(t, sel, sc);
+ } finally {
+ sel.close();
+ p.source().close();
+ p.sink().close();
}
- };
+ }
}
/**
* Invoke register, interestOps, and cancel concurrently with a thread
* doing a selection operation
*/
public static void main(String args[]) throws Exception {
- Future<Void> result;
-
- ExecutorService pool = Executors.newFixedThreadPool(1);
- try (Selector sel = Selector.open()) {
- Phaser barrier = new Phaser(2);
-
- // submit task to do the select loop
- result = pool.submit(selectLoop(sel, barrier));
-
- Pipe p = Pipe.open();
+ new Test().test((t, sel, sc) -> {
+ System.out.println("register ...");
+ // spin until make sure select is invoked
+ SelectorUtils.spinUntilLocked(t, sel);
+ SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
try {
- Pipe.SourceChannel sc = p.source();
- sc.configureBlocking(false);
-
- System.out.println("register ...");
- SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
if (!sel.keys().contains(key))
throw new RuntimeException("key not in key set");
- sel.wakeup();
- barrier.arriveAndAwaitAdvance();
-
- System.out.println("interestOps ...");
- key.interestOps(0);
- sel.wakeup();
- barrier.arriveAndAwaitAdvance();
-
- System.out.println("cancel ...");
- key.cancel();
+ } finally {
sel.wakeup();
- barrier.arriveAndAwaitAdvance();
- if (sel.keys().contains(key))
- throw new RuntimeException("key not removed from key set");
-
- } finally {
- p.source().close();
- p.sink().close();
+ t.join();
}
-
- } finally {
- pool.shutdown();
- }
-
- // ensure selectLoop completes without exception
- result.get();
-
+ });
+ new Test().test((t, sel, sc) -> {
+ System.out.println("interestOps ...");
+ SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
+ // spin until make sure select is invoked
+ SelectorUtils.spinUntilLocked(t, sel);
+ key.interestOps(0);
+ try {
+ if (key.interestOps() != 0)
+ throw new RuntimeException("interested ops not cleared");
+ } finally {
+ sel.wakeup();
+ t.join();
+ }
+ });
+ new Test().test((t, sel, sc) -> {
+ System.out.println("cancel ...");
+ SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
+ // spin until make sure select is invoked
+ SelectorUtils.spinUntilLocked(t, sel);
+ key.cancel();
+ sel.wakeup();
+ t.join();
+ if (sel.keys().contains(key))
+ throw new RuntimeException("key not removed from key set");
+ });
}
}
-
--- a/test/jdk/java/nio/channels/Selector/SelectAndClose.java Thu Aug 30 17:59:40 2018 -0700
+++ b/test/jdk/java/nio/channels/Selector/SelectAndClose.java Fri Aug 31 10:00:22 2018 +0800
@@ -23,33 +23,18 @@
/* @test
* @bug 5004077 8203765
+ * @build SelectorUtils
+ * @run main SelectAndClose
* @summary Check blocking of select and close
*/
import java.io.IOException;
-import java.lang.management.ManagementFactory;
-import java.lang.management.MonitorInfo;
-import java.lang.management.ThreadInfo;
import java.nio.channels.Selector;
public class SelectAndClose {
static Selector selector;
static volatile boolean awakened = false;
- private static boolean mightHoldLock(Thread t, Object lock) {
- long tid = t.getId();
- int hash = System.identityHashCode(lock);
- ThreadInfo ti = ManagementFactory.getThreadMXBean().
- getThreadInfo(new long[]{ tid} , true, false, 100)[0];
- if (ti != null) {
- for (MonitorInfo mi : ti.getLockedMonitors()) {
- if (mi.getIdentityHashCode() == hash)
- return true;
- }
- }
- return false;
- }
-
public static void main(String[] args) throws Exception {
selector = Selector.open();
@@ -66,12 +51,8 @@
});
selectThread.start();
- // Spin until the monitor of the selected-key set is likely held
- // as selected operations are specified to synchronize on the
- // selected-key set.
- while (!mightHoldLock(selectThread, selector.selectedKeys())) {
- Thread.sleep(50);
- }
+ // spin until make sure select is invoked
+ SelectorUtils.spinUntilLocked(selectThread, selector);
// Close the selector.
selector.close();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/nio/channels/Selector/SelectorUtils.java Fri Aug 31 10:00:22 2018 +0800
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2018, 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.
+ */
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.MonitorInfo;
+import java.lang.management.ThreadInfo;
+import java.nio.channels.Selector;
+
+public class SelectorUtils {
+
+ /**
+ * tell if the monitor of an Object is held by a Thread.
+ * @param t the Thread to hold the monitor of the selected-key set
+ * @param lock the Object
+ * @return
+ */
+ public static boolean mightHoldLock(Thread t, Object lock) {
+ long tid = t.getId();
+ int hash = System.identityHashCode(lock);
+ ThreadInfo ti = ManagementFactory.getThreadMXBean().
+ getThreadInfo(new long[]{ tid} , true, false, 100)[0];
+ if (ti != null) {
+ for (MonitorInfo mi : ti.getLockedMonitors()) {
+ if (mi.getIdentityHashCode() == hash)
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Spin until the monitor of the selected-key set is likely held
+ * as selected operations are specified to synchronize on the
+ * selected-key set.
+ * @param t the Thread to hold the monitor of the selected-key set
+ * @param sel the Selector
+ * @throws Exception
+ */
+ public static void spinUntilLocked(Thread t, Selector sel) throws Exception {
+ while (!mightHoldLock(t, sel.selectedKeys())) {
+ Thread.sleep(50);
+ }
+ }
+}