test/jdk/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 7668 jdk/test/java/nio/channels/FileChannel/ReleaseOnCloseDeadlock.java@d4a77089c587
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
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.
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     4
 *
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     8
 *
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    13
 * accompanied this code).
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    14
 *
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    21
 * questions.
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    22
 */
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    23
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    24
/* @test
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
    25
 * @bug 6543863 6842687
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    26
 * @summary Try to cause a deadlock between (Asynchronous)FileChannel.close
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    27
 *   and FileLock.release
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    28
 */
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    29
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    30
import java.io.*;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    32
import static java.nio.file.StandardOpenOption.*;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    33
import java.nio.channels.*;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    34
import java.util.concurrent.*;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    35
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    36
public class ReleaseOnCloseDeadlock {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    37
    private static final int LOCK_COUNT = 1024;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    38
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    39
    public static void main(String[] args) throws IOException {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    40
        File blah = File.createTempFile("blah", null);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    41
        blah.deleteOnExit();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    42
        try {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    43
            for (int i=0; i<100; i++) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    44
                test(blah.toPath());
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    45
            }
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    46
        } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    47
            blah.delete();
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    48
        }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    49
    }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    50
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    51
    static void test(Path file) throws IOException {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    52
        FileLock[] locks = new FileLock[LOCK_COUNT];
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    53
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    54
        FileChannel fc = FileChannel.open(file, READ, WRITE);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    55
        for (int i=0; i<LOCK_COUNT; i++) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    56
            locks[i] = fc.lock(i, 1, true);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    57
        }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    58
        tryToDeadlock(fc, locks);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    59
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    60
        AsynchronousFileChannel ch = AsynchronousFileChannel.open(file, READ, WRITE);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    61
        for (int i=0; i<LOCK_COUNT; i++) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    62
            try {
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 2594
diff changeset
    63
                locks[i] = ch.lock(i, 1, true).get();
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    64
            } catch (InterruptedException x) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    65
                throw new RuntimeException(x);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    66
            } catch (ExecutionException x) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    67
                throw new RuntimeException(x);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    68
            }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    69
        }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    70
        tryToDeadlock(ch, locks);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    71
    }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    72
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    73
    static void tryToDeadlock(final Channel channel, FileLock[] locks)
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    74
        throws IOException
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    75
    {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    76
        // start thread to close the file (and invalidate the locks)
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    77
        Thread closer = new Thread(
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    78
            new Runnable() {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    79
                public void run() {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    80
                    try {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    81
                        channel.close();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    82
                    } catch (IOException ignore) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    83
                        ignore.printStackTrace();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    84
                    }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    85
                }});
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    86
        closer.start();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    87
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    88
        // release the locks explicitly
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    89
        for (int i=0; i<locks.length; i++) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    90
            try {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    91
                locks[i].release();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    92
            } catch (ClosedChannelException ignore) { }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    93
        }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    94
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    95
        // we are done when closer has terminated
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    96
        while (closer.isAlive()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    97
            try {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    98
                closer.join();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
    99
            } catch (InterruptedException ignore) { }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
   100
        }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
   101
    }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents:
diff changeset
   102
}