test/jdk/java/nio/channels/FileChannel/InterruptDeadlock.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     1
/*
44113
2b76e807b769 8176394: Incorrect relational operator in java/nio/channels/FileChannel/InterruptDeadlock.java
bpb
parents: 16921
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     4
 *
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     8
 *
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    13
 * accompanied this code).
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    14
 *
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    18
 *
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    21
 * questions.
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    22
 */
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    23
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    24
/* @test
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    25
 * @bug 8012019
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    26
 * @summary Tests interruption of threads doing position-based read methods in
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    27
 *   an attempt to provoke a deadlock between position sensitive and position
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    28
 *   insensitive methods
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    29
 */
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    30
import java.nio.ByteBuffer;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    31
import java.nio.channels.*;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    32
import java.nio.file.*;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    33
import static java.nio.file.StandardOpenOption.*;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    34
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    35
public class InterruptDeadlock {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    36
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    37
    /**
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    38
     * A thread that continuously reads from a FileChannel with
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    39
     * read(ByteBuffer,long). The thread terminates when interrupted and/or
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    40
     * the FileChannel is closed.
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    41
     */
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    42
    static class Reader extends Thread {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    43
        final FileChannel fc;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    44
        volatile Exception exception;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    45
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    46
        Reader(FileChannel fc) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    47
            this.fc = fc;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    48
        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    49
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    50
        @Override
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    51
        public void run() {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    52
            ByteBuffer bb = ByteBuffer.allocate(1024);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    53
            try {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    54
                long pos = 0L;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    55
                for (;;) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    56
                    bb.clear();
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    57
                    int n = fc.read(bb, pos);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    58
                    if (n > 0)
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    59
                        pos += n;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    60
                    // fc.size is important here as it is position sensitive
44113
2b76e807b769 8176394: Incorrect relational operator in java/nio/channels/FileChannel/InterruptDeadlock.java
bpb
parents: 16921
diff changeset
    61
                    if (pos >= fc.size())
16921
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    62
                        pos = 0L;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    63
                }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    64
            } catch (ClosedChannelException x) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    65
                System.out.println(x.getClass() + " (expected)");
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    66
            } catch (Exception unexpected) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    67
                this.exception = unexpected;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    68
            }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    69
        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    70
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    71
        Exception exception() {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    72
            return exception;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    73
        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    74
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    75
        static Reader startReader(FileChannel fc) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    76
            Reader r = new Reader(fc);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    77
            r.start();
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    78
            return r;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    79
        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    80
    }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    81
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    82
    // the number of reader threads to start
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    83
    private static final int READER_COUNT = 4;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    84
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    85
    public static void main(String[] args) throws Exception {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    86
        Path file = Paths.get("data.txt");
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    87
        try (FileChannel fc = FileChannel.open(file, CREATE, TRUNCATE_EXISTING, WRITE)) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    88
            fc.position(1024L * 1024L);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    89
            fc.write(ByteBuffer.wrap(new byte[1]));
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    90
        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    91
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    92
        Reader[] readers = new Reader[READER_COUNT];
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    93
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    94
        for (int i=1; i<=20; i++) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    95
            System.out.format("Iteration: %s%n", i);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    96
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    97
            try (FileChannel fc = FileChannel.open(file)) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    98
                boolean failed = false;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
    99
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   100
                // start reader threads
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   101
                for (int j=0; j<READER_COUNT; j++) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   102
                    readers[j] = Reader.startReader(fc);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   103
                }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   104
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   105
                // give readers a bit of time to get started (not strictly required)
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   106
                Thread.sleep(100);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   107
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   108
                // interrupt and wait for the readers to terminate
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   109
                for (Reader r: readers) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   110
                    r.interrupt();
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   111
                }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   112
                for (Reader r: readers) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   113
                    try {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   114
                        r.join(10000);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   115
                        Exception e = r.exception();
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   116
                        if (e != null) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   117
                            System.err.println("Reader thread failed with: " + e);
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   118
                            failed = true;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   119
                        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   120
                    } catch (InterruptedException x) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   121
                        System.err.println("Reader thread did not terminte");
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   122
                        failed = true;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   123
                    }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   124
                }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   125
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   126
                // the channel should not be open at this point
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   127
                if (fc.isOpen()) {
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   128
                    System.err.println("FileChannel was not closed");
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   129
                    failed = true;
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   130
                }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   131
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   132
                if (failed)
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   133
                    throw new RuntimeException("Test failed - see log for details");
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   134
            }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   135
        }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   136
    }
e70261f11307 8012019: (fc) Thread.interrupt triggers hang in FileChannelImpl.pread (win)
alanb
parents:
diff changeset
   137
}