Path: blob/master/test/jdk/java/nio/channels/Selector/RegAfterPreClose.java
41153 views
/*1* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 669349025* @summary Pre-close file descriptor may inadvertently get registered with26* epoll during close27*/2829import java.net.*;30import java.nio.channels.*;31import java.util.concurrent.*;32import java.util.*;33import java.io.IOException;3435public class RegAfterPreClose {3637static final int TEST_ITERATIONS = 300;38static volatile boolean done;3940/**41* A task that continuously connects to a given address and immediately42* closes the connection.43*/44static class Connector implements Runnable {45private final SocketAddress sa;46Connector(int port) throws IOException {47InetAddress lh = InetAddress.getLocalHost();48this.sa = new InetSocketAddress(lh, port);49}50public void run() {51while (!done) {52try {53SocketChannel.open(sa).close();54} catch (IOException x) {55// back-off as probably resource related56try {57Thread.sleep(10);58} catch (InterruptedException ignore) { }59}60}61}62}6364/**65* A task that closes a channel.66*/67static class Closer implements Runnable {68private final Channel channel;69Closer(Channel sc) {70this.channel = sc;71}72public void run() {73try {74channel.close();75} catch (IOException ignore) { }76}77}7879public static void main(String[] args) throws Exception {80// create listener81InetSocketAddress isa = new InetSocketAddress(0);82ServerSocketChannel ssc = ServerSocketChannel.open();83ssc.socket().bind(isa);8485// register with Selector86final Selector sel = Selector.open();87ssc.configureBlocking(false);88SelectionKey key = ssc.register(sel, SelectionKey.OP_ACCEPT);8990ThreadFactory factory = new ThreadFactory() {91@Override92public Thread newThread(Runnable r) {93Thread t = new Thread(r);94t.setDaemon(true);95return t;96}97};9899// create Executor that handles tasks that closes channels100// "asynchronously" - this creates the conditions to provoke the bug.101ExecutorService executor = Executors.newFixedThreadPool(2, factory);102103// submit task that connects to listener104executor.execute(new Connector(ssc.socket().getLocalPort()));105106// loop accepting connections until done (or an IOException is thrown)107int remaining = TEST_ITERATIONS;108while (remaining > 0) {109sel.select();110if (key.isAcceptable()) {111SocketChannel sc = ssc.accept();112if (sc != null) {113remaining--;114sc.configureBlocking(false);115sc.register(sel, SelectionKey.OP_READ);116executor.execute(new Closer(sc));117}118}119sel.selectedKeys().clear();120}121done = true;122sel.close();123executor.shutdown();124}125}126127128