jdk/test/java/lang/ProcessBuilder/CloseRace.java
author igerasim
Thu, 30 Jan 2014 00:02:46 +0400
changeset 22626 758d413359c4
parent 21357 jdk/test/java/lang/Runtime/exec/CloseRace.java@eb15eae19cd9
child 23341 b9e3bcefc522
permissions -rw-r--r--
8027348: (process) Enhancement of handling async close of ProcessInputStream Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     1
/*
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
     2
 * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved.
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     4
 *
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     8
 *
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    13
 * accompanied this code).
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    14
 *
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    18
 *
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    21
 * questions.
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    22
 */
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    23
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    24
/**
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    25
 * @test
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    26
 * @bug 8024521
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    27
 * @summary Closing ProcessPipeInputStream at the time the process exits is racy
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    28
 *          and leads to data corruption. Run this test manually (as
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    29
 *          an ordinary java program) with  -Xmx8M  to repro bug 8024521.
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    30
 * @run main/othervm -Xmx8M -Dtest.duration=2 CloseRace
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    31
 */
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    32
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    33
import java.io.*;
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    34
import java.util.ArrayList;
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    35
import java.util.List;
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    36
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    37
public class CloseRace {
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    38
    private static final String BIG_FILE = "bigfile";
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    39
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    40
    private static final int[] procFDs = new int[6];
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    41
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    42
    /** default value sufficient to repro bug 8024521. */
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    43
    private static final int testDurationSeconds
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    44
        = Integer.getInteger("test.duration", 600);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    45
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    46
    static boolean fdInUse(int i) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    47
        return new File("/proc/self/fd/" + i).exists();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    48
    }
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    49
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    50
    static boolean[] procFDsInUse() {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    51
        boolean[] inUse = new boolean[procFDs.length];
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    52
        for (int i = 0; i < procFDs.length; i++)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    53
            inUse[i] = fdInUse(procFDs[i]);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    54
        return inUse;
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    55
    }
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    56
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    57
    static int count(boolean[] bits) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    58
        int count = 0;
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    59
        for (int i = 0; i < bits.length; i++)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    60
            count += bits[i] ? 1 : 0;
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    61
        return count;
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    62
    }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    63
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    64
    public static void main(String args[]) throws Exception {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    65
        if (!(new File("/proc/self/fd").isDirectory()))
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    66
            return;
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    67
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    68
        // Catch Errors from process reaper
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    69
        Thread.setDefaultUncaughtExceptionHandler
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    70
            ((t, e) -> { e.printStackTrace(); System.exit(1); });
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    71
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    72
        try (RandomAccessFile f = new RandomAccessFile(BIG_FILE, "rw")) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    73
            f.setLength(Runtime.getRuntime().maxMemory()); // provoke OOME
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    74
        }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
    75
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    76
        for (int i = 0, j = 0; j < procFDs.length; i++)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    77
            if (!fdInUse(i))
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    78
                procFDs[j++] = i;
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    79
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    80
        Thread[] threads = {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    81
            new Thread(new OpenLoop()),
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    82
            new Thread(new ExecLoop()),
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    83
        };
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    84
        for (Thread thread : threads)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    85
            thread.start();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    86
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    87
        Thread.sleep(testDurationSeconds * 1000);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    88
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    89
        for (Thread thread : threads)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    90
            thread.interrupt();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    91
        for (Thread thread : threads)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    92
            thread.join();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    93
    }
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    94
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    95
    static class OpenLoop implements Runnable {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    96
        public void run() {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    97
            while (!Thread.interrupted()) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    98
                try {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
    99
                    // wait for ExecLoop to finish creating process
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   100
                    do {} while (count(procFDsInUse()) != 3);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   101
                    List<InputStream> iss = new ArrayList<>(4);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   102
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   103
                    // eat up three "holes" (closed ends of pipe fd pairs)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   104
                    for (int i = 0; i < 3; i++)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   105
                        iss.add(new FileInputStream(BIG_FILE));
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   106
                    do {} while (count(procFDsInUse()) == procFDs.length);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   107
                    // hopefully this will racily occupy empty fd slot
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   108
                    iss.add(new FileInputStream(BIG_FILE));
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   109
                    Thread.sleep(1); // Widen race window
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   110
                    for (InputStream is : iss)
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   111
                        is.close();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   112
                } catch (InterruptedException e) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   113
                    break;
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   114
                } catch (Exception e) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   115
                    throw new Error(e);
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   116
                }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   117
            }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   118
        }
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   119
    }
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   120
22626
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   121
    static class ExecLoop implements Runnable {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   122
        public void run() {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   123
            ProcessBuilder builder = new ProcessBuilder("/bin/true");
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   124
            while (!Thread.interrupted()) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   125
                try {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   126
                    // wait for OpenLoop to finish
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   127
                    do {} while (count(procFDsInUse()) > 0);
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   128
                    Process process = builder.start();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   129
                    InputStream is = process.getInputStream();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   130
                    process.waitFor();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   131
                    is.close();
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   132
                } catch (InterruptedException e) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   133
                    break;
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   134
                } catch (Exception e) {
758d413359c4 8027348: (process) Enhancement of handling async close of ProcessInputStream
igerasim
parents: 21357
diff changeset
   135
                    throw new Error(e);
21357
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   136
                }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   137
            }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   138
        }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   139
    }
eb15eae19cd9 8024521: (process) Async close issues with Process InputStream
igerasim
parents:
diff changeset
   140
}