test/jdk/java/nio/channels/Selector/RegAfterPreClose.java
author mli
Fri, 31 Aug 2018 10:00:22 +0800
changeset 51602 dbb0e798deeb
parent 47216 71c04702a3d5
permissions -rw-r--r--
8208280: java/nio/channels/Selector/RegisterDuringSelect.java fails with "key not removed from key set" Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     4
 *
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     8
 *
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    13
 * accompanied this code).
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    14
 *
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2433
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2433
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2433
diff changeset
    21
 * questions.
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    22
 */
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    23
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    24
/* @test
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    25
 * @bug 6693490
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    26
 * @summary Pre-close file descriptor may inadvertently get registered with
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    27
 *     epoll during close
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    28
 */
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    29
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    30
import java.net.*;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    31
import java.nio.channels.*;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    32
import java.util.concurrent.*;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    33
import java.util.*;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    34
import java.io.IOException;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    35
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    36
public class RegAfterPreClose {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    37
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    38
    static final int TEST_ITERATIONS = 300;
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    39
    static volatile boolean done;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    40
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    41
    /**
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    42
     * A task that continuously connects to a given address and immediately
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    43
     * closes the connection.
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    44
     */
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    45
    static class Connector implements Runnable {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    46
        private final SocketAddress sa;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    47
        Connector(int port) throws IOException {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    48
            InetAddress lh = InetAddress.getLocalHost();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    49
            this.sa = new InetSocketAddress(lh, port);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    50
        }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    51
        public void run() {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    52
            while (!done) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    53
                try {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    54
                    SocketChannel.open(sa).close();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    55
                } catch (IOException x) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    56
                    // back-off as probably resource related
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    57
                    try {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    58
                        Thread.sleep(10);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    59
                    } catch (InterruptedException  ignore) { }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    60
                }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    61
            }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    62
        }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    63
    }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    64
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    65
    /**
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    66
     * A task that closes a channel.
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    67
     */
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    68
    static class Closer implements Runnable {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    69
        private final Channel channel;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    70
        Closer(Channel sc) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    71
            this.channel = sc;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    72
        }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    73
        public void run() {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    74
            try {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    75
                channel.close();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    76
            } catch (IOException ignore) { }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    77
        }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    78
    }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    79
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    80
    public static void main(String[] args) throws Exception {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    81
        // create listener
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    82
        InetSocketAddress isa = new InetSocketAddress(0);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    83
        ServerSocketChannel ssc = ServerSocketChannel.open();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    84
        ssc.socket().bind(isa);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    85
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    86
        // register with Selector
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    87
        final Selector sel = Selector.open();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    88
        ssc.configureBlocking(false);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    89
        SelectionKey key = ssc.register(sel, SelectionKey.OP_ACCEPT);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    90
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    91
        ThreadFactory factory = new ThreadFactory() {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    92
            @Override
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    93
            public Thread newThread(Runnable r) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    94
                Thread t = new Thread(r);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    95
                t.setDaemon(true);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    96
                return t;
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    97
            }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    98
        };
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
    99
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   100
        // create Executor that handles tasks that closes channels
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   101
        // "asynchronously" - this creates the conditions to provoke the bug.
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   102
        ExecutorService executor = Executors.newFixedThreadPool(2, factory);
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   103
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   104
        // submit task that connects to listener
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   105
        executor.execute(new Connector(ssc.socket().getLocalPort()));
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   106
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   107
        // loop accepting connections until done (or an IOException is thrown)
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   108
        int remaining = TEST_ITERATIONS;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   109
        while (remaining > 0) {
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   110
            sel.select();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   111
            if (key.isAcceptable()) {
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   112
                SocketChannel sc = ssc.accept();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   113
                if (sc != null) {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   114
                    remaining--;
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   115
                    sc.configureBlocking(false);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   116
                    sc.register(sel, SelectionKey.OP_READ);
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   117
                    executor.execute(new Closer(sc));
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   118
                }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   119
            }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   120
            sel.selectedKeys().clear();
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   121
        }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   122
        done = true;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   123
        sel.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   124
        executor.shutdown();
2433
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   125
    }
9f0ab7f726b8 6693490: (se) select throws "File exists" IOException under load (lnx)
alanb
parents:
diff changeset
   126
}