8366736: Closed System.out causes child process to hang on Windows

This commit is contained in:
iklam 2026-01-12 23:32:04 -08:00
parent e89c1290ca
commit 30b312e145
2 changed files with 105 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2026, 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
@ -115,30 +115,46 @@ final class ProcessImpl extends Process {
stdHandles[0] = fdAccess.getHandle(f0.getFD());
}
Redirect file1 = null;
if (redirects[1] == Redirect.PIPE) {
stdHandles[1] = -1L;
} else if (redirects[1] == Redirect.INHERIT) {
stdHandles[1] = fdAccess.getHandle(FileDescriptor.out);
if (stdHandles[1] == -1L) {
// FileDescriptor.out has been closed.
file1 = Redirect.DISCARD;
}
} else if (redirects[1] instanceof ProcessBuilder.RedirectPipeImpl) {
stdHandles[1] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[1]).getFd());
// Force getInputStream to return a null stream,
// the handle is directly assigned to the next process.
forceNullOutputStream = true;
} else {
f1 = newFileOutputStream(redirects[1].file(),
redirects[1].append());
file1 = redirects[1];
}
if (file1 != null) {
f1 = newFileOutputStream(file1.file(),
file1.append());
stdHandles[1] = fdAccess.getHandle(f1.getFD());
}
Redirect file2 = null;
if (redirects[2] == Redirect.PIPE) {
stdHandles[2] = -1L;
} else if (redirects[2] == Redirect.INHERIT) {
stdHandles[2] = fdAccess.getHandle(FileDescriptor.err);
if (stdHandles[2] == -1L) {
// FileDescriptor.err has been closed.
file2 = Redirect.DISCARD;
}
} else if (redirects[2] instanceof ProcessBuilder.RedirectPipeImpl) {
stdHandles[2] = fdAccess.getHandle(((ProcessBuilder.RedirectPipeImpl) redirects[2]).getFd());
} else {
f2 = newFileOutputStream(redirects[2].file(),
redirects[2].append());
file2 = redirects[2];
}
if (file2 != null) {
f2 = newFileOutputStream(file2.file(),
file2.append());
stdHandles[2] = fdAccess.getHandle(f2.getFD());
}
}

View File

@ -0,0 +1,84 @@
/*
* Copyright (c) 2026, 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
* @summary child process should not hang even if parent process has closed System.out/err
* @bug 8366736
* @run main/othervm InheritIOClosed close dontclose
* @run main/othervm InheritIOClosed dontclose close
* @run main/othervm InheritIOClosed close close
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class InheritIOClosed {
private static String JAVA_EXE = System.getProperty("java.home")
+ File.separator + "bin"
+ File.separator + "java";
private static String s = "1234567890" +
"1234567890" +
"1234567890" +
"1234567890" +
"1234567890" +
"1234567890" +
"1234567890" +
"1234567890" +
"1234567890" +
"1234567890";
public static void main(String[] args) throws Exception {
if (args.length == 2) {
// Main test process
if (args[0].equals("close")) {
System.out.close();
}
if (args[1].equals("close")) {
System.err.close();
}
ProcessBuilder pb = new ProcessBuilder().inheritIO()
.command(JAVA_EXE,
"-cp",
System.getProperty("java.class.path"),
InheritIOClosed.class.getName());
Process process = pb.start();
process.waitFor();
System.out.println("Done");
} else {
// Child process -- print to System.out/err. Without the fix in
// JDK-8366736, this process will hang on Windows.
for (int i = 0; i < 100; i++) {
System.out.println(s);
}
for (int i = 0; i < 100; i++) {
System.err.println(s);
}
}
}
}