jdk/test/java/lang/ProcessBuilder/InheritIOEHandle.java
author uta
Thu, 08 Aug 2013 09:16:16 +0400
changeset 19372 e404c834f1cd
child 21294 545dfa5b947e
permissions -rw-r--r--
7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever Reviewed-by: alanb, robm, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19372
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     1
/*
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     4
 *
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     8
 *
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    13
 * accompanied this code).
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    14
 *
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    18
 *
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    21
 * questions.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    22
 */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    23
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    24
/**
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    25
 * @test
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    26
 * @bug 7147084
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    27
 * @run main/othervm InheritIOEHandle
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    28
 * @summary inherit IOE handles and MS CreateProcess limitations (kb315939)
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    29
 */
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    30
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    31
import java.io.BufferedReader;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    32
import java.io.File;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    33
import java.io.IOException;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    34
import java.io.InputStreamReader;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    35
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    36
public class InheritIOEHandle {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    37
    private static enum APP {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    38
        B, C;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    39
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    40
    private static File stopC = new File(".\\StopC.txt");
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    41
    private static String SIGNAL = "After call child process";
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    42
    private static String JAVA_EXE = System.getProperty("java.home")
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    43
        + File.separator + "bin"
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    44
        + File.separator + "java";
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    45
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    46
    private static String[] getCommandArray(String processName) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    47
        String[] cmdArray = {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    48
            JAVA_EXE,
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    49
            "-cp",
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    50
            System.getProperty("java.class.path"),
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    51
            InheritIOEHandle.class.getName(),
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    52
            processName
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    53
        };
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    54
        return cmdArray;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    55
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    56
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    57
    public static void main(String[] args) throws Exception {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    58
        if (!System.getProperty("os.name").startsWith("Windows")) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    59
            return;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    60
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    61
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    62
        if (args.length > 0) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    63
            APP app = APP.valueOf(args[0]);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    64
            switch (app) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    65
            case B:
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    66
                performB();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    67
                break;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    68
            case C:
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    69
                performC();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    70
                break;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    71
            }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    72
            return;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    73
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    74
        performA();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    75
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    76
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    77
    private static void performA() {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    78
        try {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    79
            stopC.delete();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    80
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    81
            ProcessBuilder builder = new ProcessBuilder(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    82
                    getCommandArray(APP.B.name()));
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    83
            builder.redirectErrorStream(true);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    84
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    85
            Process process = builder.start();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    86
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    87
            process.getOutputStream().close();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    88
            process.getErrorStream().close();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    89
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    90
            try (BufferedReader in = new BufferedReader( new InputStreamReader(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    91
                         process.getInputStream(), "utf-8")))
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    92
            {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    93
                String result;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    94
                while ((result = in.readLine()) != null) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    95
                    if (!SIGNAL.equals(result)) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    96
                        throw new Error("Catastrophe in process B! Bad output.");
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    97
                    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    98
                }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
    99
            }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   100
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   101
            // If JDK-7147084 is not fixed that point is unreachable.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   102
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   103
            // write signal file
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   104
            stopC.createNewFile();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   105
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   106
            System.err.println("Read stream finished.");
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   107
        } catch (IOException ex) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   108
            throw new Error("Catastrophe in process A!", ex);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   109
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   110
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   111
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   112
    private static void performB() {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   113
        try {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   114
            ProcessBuilder builder = new ProcessBuilder(
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   115
                    getCommandArray(APP.C.name()));
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   116
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   117
            Process process = builder.start();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   118
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   119
            process.getInputStream().close();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   120
            process.getOutputStream().close();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   121
            process.getErrorStream().close();
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   122
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   123
            System.out.println(SIGNAL);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   124
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   125
            // JDK-7147084 subject:
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   126
            // Process C inherits the [System.out] handle and
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   127
            // handle close in B does not finalize the streaming for A.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   128
            // (handle reference count > 1).
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   129
        } catch (IOException ex) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   130
            throw new Error("Catastrophe in process B!", ex);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   131
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   132
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   133
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   134
    private static void performC() {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   135
        // If JDK-7147084 is not fixed the loop is 5min long.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   136
        for (int i = 0; i < 5*60; ++i) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   137
            try {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   138
                Thread.sleep(1000);
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   139
                // check for sucess
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   140
                if (stopC.exists())
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   141
                    break;
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   142
            } catch (InterruptedException ex) {
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   143
                // that is ok. Longer sleep - better effect.
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   144
            }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   145
        }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   146
    }
e404c834f1cd 7147084: (process) appA hangs when read output stream of appB which starts appC that runs forever
uta
parents:
diff changeset
   147
}