Skip to content

Commit ae849c1

Browse files
committed
8080569: java/lang/ProcessBuilder/DestroyTest.java fails with "RuntimeException: Process terminated prematurely"
Backport-of: 2ee8882
1 parent 62207ef commit ae849c1

File tree

1 file changed

+23
-61
lines changed

1 file changed

+23
-61
lines changed

test/jdk/java/lang/ProcessBuilder/DestroyTest.java

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -28,27 +28,17 @@
2828
* destroyForcibly.
2929
*/
3030

31-
import java.io.*;
32-
import java.util.ArrayList;
33-
import java.util.concurrent.TimeUnit;
31+
import java.io.BufferedReader;
32+
import java.io.BufferedWriter;
33+
import java.io.File;
34+
import java.io.FileWriter;
35+
import java.io.IOException;
36+
import java.io.InputStreamReader;
3437

3538
abstract class ProcessTest implements Runnable {
3639
ProcessBuilder bldr;
3740
Process p;
3841

39-
public Process killProc(boolean force) throws Exception {
40-
if (force) {
41-
p.destroyForcibly();
42-
} else {
43-
p.destroy();
44-
}
45-
return p;
46-
}
47-
48-
public boolean isAlive() {
49-
return p.isAlive();
50-
}
51-
5242
public void run() {
5343
try {
5444
String line;
@@ -63,7 +53,17 @@ public void run() {
6353
}
6454
}
6555

66-
public abstract void runTest() throws Exception;
56+
public void runTest() throws Exception {
57+
// The destroy() method is not tested because
58+
// the process streams are closed by the destroy() call.
59+
// After a destroy() call, the process terminates with a
60+
// SIGPIPE even if it was trapping SIGTERM.
61+
// So skip the trap test and go straight to destroyForcibly().
62+
p.destroyForcibly();
63+
p.waitFor();
64+
if (p.isAlive())
65+
throw new RuntimeException("Problem terminating the process.");
66+
}
6767
}
6868

6969
class UnixTest extends ProcessTest {
@@ -78,16 +78,10 @@ public UnixTest(File script) throws IOException {
7878

7979
void createScript(File processTrapScript) throws IOException {
8080
processTrapScript.deleteOnExit();
81-
FileWriter fstream = new FileWriter(processTrapScript);
82-
try (BufferedWriter out = new BufferedWriter(fstream)) {
81+
try (FileWriter fstream = new FileWriter(processTrapScript);
82+
BufferedWriter out = new BufferedWriter(fstream)) {
8383
out.write("#!/bin/bash\n" +
84-
"echo \\\"ProcessTrap.sh started: trapping SIGTERM/SIGINT\\\"\n" +
85-
"trap bashtrap SIGTERM SIGINT\n" +
86-
"bashtrap()\n" +
87-
"{\n" +
88-
" echo \\\"SIGTERM/SIGINT detected!\\\"\n" +
89-
"}\n" +
90-
"\n" +
84+
"echo \\\"ProcessTrap.sh started\\\"\n" +
9185
"while :\n" +
9286
"do\n" +
9387
" sleep 1;\n" +
@@ -96,33 +90,6 @@ void createScript(File processTrapScript) throws IOException {
9690
processTrapScript.setExecutable(true, true);
9791
}
9892

99-
@Override
100-
public void runTest() throws Exception {
101-
killProc(false);
102-
Thread.sleep(1000);
103-
if (!p.isAlive())
104-
throw new RuntimeException("Process terminated prematurely.");
105-
killProc(true).waitFor();
106-
if (p.isAlive())
107-
throw new RuntimeException("Problem terminating the process.");
108-
}
109-
}
110-
111-
class MacTest extends UnixTest {
112-
public MacTest(File script) throws IOException {
113-
super(script);
114-
}
115-
116-
@Override
117-
public void runTest() throws Exception {
118-
// On Mac, it appears that when we close the processes streams
119-
// after a destroy() call, the process terminates with a
120-
// SIGPIPE even if it was trapping the SIGTERM, so as with
121-
// windows, we skip the trap test and go straight to destroyForcibly().
122-
killProc(true).waitFor();
123-
if (p.isAlive())
124-
throw new RuntimeException("Problem terminating the process.");
125-
}
12693
}
12794

12895
class WindowsTest extends ProcessTest {
@@ -133,10 +100,6 @@ public WindowsTest() throws IOException {
133100
p = bldr.start();
134101
}
135102

136-
@Override
137-
public void runTest() throws Exception {
138-
killProc(true).waitFor();
139-
}
140103
}
141104

142105
public class DestroyTest {
@@ -148,12 +111,11 @@ public static ProcessTest getTest() throws Exception {
148111
} else {
149112
File userDir = new File(System.getProperty("user.dir", "."));
150113
File tempFile = File.createTempFile("ProcessTrap-", ".sh", userDir);
151-
if (osName.startsWith("Linux") == true
114+
if (osName.startsWith("Linux")
115+
|| osName.startsWith("Mac OS")
152116
|| osName.equals("SunOS")
153117
|| osName.equals("AIX")) {
154118
return new UnixTest(tempFile);
155-
} else if (osName.startsWith("Mac OS")) {
156-
return new MacTest(tempFile);
157119
}
158120
}
159121
return null;

0 commit comments

Comments
 (0)