Skip to content

Commit b602549

Browse files
committed
Detect process third party audit being killed by OOM (#44269)
* Detect process third party audit being killed by OOM It's very common for the third party audit to be killed by the OOM killer when the system is running low on memory. Since the forbidden APIs call is expected to fail, we were ignoring these and incorrectly interpreting the partial output. With this change we detect and provide a proper error message when this happens.
1 parent 5f90ea0 commit b602549

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

buildSrc/src/main/java/org/elasticsearch/gradle/precommit/ThirdPartyAuditTask.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.gradle.precommit;
2020

21+
import de.thetaphi.forbiddenapis.cli.CliMain;
2122
import org.apache.commons.io.output.NullOutputStream;
2223
import org.elasticsearch.gradle.JdkJarHellCheck;
2324
import org.gradle.api.DefaultTask;
@@ -47,6 +48,7 @@
4748
import java.nio.charset.StandardCharsets;
4849
import java.util.Arrays;
4950
import java.util.Collections;
51+
import java.util.List;
5052
import java.util.Set;
5153
import java.util.TreeSet;
5254
import java.util.regex.Matcher;
@@ -65,6 +67,12 @@ public class ThirdPartyAuditTask extends DefaultTask {
6567
private static final Pattern VIOLATION_PATTERN = Pattern.compile(
6668
"\\s\\sin ([a-zA-Z0-9$.]+) \\(.*\\)"
6769
);
70+
private static final int SIG_KILL_EXIT_VALUE = 137;
71+
private static final List<Integer> EXPECTED_EXIT_CODES = Arrays.asList(
72+
CliMain.EXIT_SUCCESS,
73+
CliMain.EXIT_VIOLATION,
74+
CliMain.EXIT_UNSUPPORTED_JDK
75+
);
6876

6977
private Set<String> missingClassExcludes = new TreeSet<>();
7078

@@ -319,7 +327,7 @@ private String formatClassList(Set<String> classList) {
319327

320328
private String runForbiddenAPIsCli() throws IOException {
321329
ByteArrayOutputStream errorOut = new ByteArrayOutputStream();
322-
getProject().javaexec(spec -> {
330+
ExecResult result = getProject().javaexec(spec -> {
323331
if (javaHome != null) {
324332
spec.setExecutable(javaHome + "/bin/java");
325333
}
@@ -328,6 +336,7 @@ private String runForbiddenAPIsCli() throws IOException {
328336
getRuntimeConfiguration(),
329337
getProject().getConfigurations().getByName("compileOnly")
330338
);
339+
spec.jvmArgs("-Xmx1g");
331340
spec.setMain("de.thetaphi.forbiddenapis.cli.CliMain");
332341
spec.args(
333342
"-f", getSignatureFile().getAbsolutePath(),
@@ -340,10 +349,18 @@ private String runForbiddenAPIsCli() throws IOException {
340349
}
341350
spec.setIgnoreExitValue(true);
342351
});
352+
if (result.getExitValue() == SIG_KILL_EXIT_VALUE) {
353+
throw new IllegalStateException(
354+
"Third party audit was killed buy SIGKILL, could be a victim of the Linux OOM killer"
355+
);
356+
}
343357
final String forbiddenApisOutput;
344358
try (ByteArrayOutputStream outputStream = errorOut) {
345359
forbiddenApisOutput = outputStream.toString(StandardCharsets.UTF_8.name());
346360
}
361+
if (EXPECTED_EXIT_CODES.contains(result.getExitValue()) == false) {
362+
throw new IllegalStateException("Forbidden APIs cli failed: " + forbiddenApisOutput);
363+
}
347364
return forbiddenApisOutput;
348365
}
349366

buildSrc/src/test/java/org/elasticsearch/gradle/precommit/ThirdPartyAuditTaskIT.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,7 @@ public void testWithEmptyRules() {
4848
"-PcompileOnlyGroup=other.gradle:broken-log4j", "-PcompileOnlyVersion=0.0.1",
4949
"-PcompileGroup=other.gradle:dummy-io", "-PcompileVersion=0.0.1"
5050
)
51-
.build();
52-
53-
assertTaskSuccessful(result, ":empty");
54-
55-
result = getGradleRunner("thirdPartyAudit")
56-
.withArguments("empty", "-s",
57-
"-PcompileOnlyGroup=other.gradle:broken-log4j", "-PcompileOnlyVersion=0.0.1",
58-
"-PcompileGroup=other.gradle:dummy-io", "-PcompileVersion=0.0.1"
59-
)
60-
.build();
61-
62-
assertTaskUpToDate(result, ":empty");
63-
64-
result = getGradleRunner("thirdPartyAudit")
65-
.withArguments("empty", "-s",
66-
"-PcompileOnlyGroup=other.gradle:broken-log4j", "-PcompileOnlyVersion=0.0.1",
67-
"-PcompileGroup=other.gradle:dummy-io", "-PcompileVersion=0.0.2"
68-
)
69-
.build();
70-
71-
assertTaskSuccessful(result, ":empty");
51+
.buildAndFail();
7252
}
7353

7454
public void testViolationFoundAndCompileOnlyIgnored() {

0 commit comments

Comments
 (0)