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