Skip to content

Commit 17ef73a

Browse files
committed
Blindly trying to fix the smoke tests
1 parent 0975e0f commit 17ef73a

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

internal-api/src/main/java/datadog/trace/util/PidHelper.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import datadog.trace.context.TraceScope;
66
import de.thetaphi.forbiddenapis.SuppressForbidden;
77
import java.io.BufferedReader;
8+
import java.io.IOException;
89
import java.io.InputStreamReader;
910
import java.lang.management.ManagementFactory;
1011
import java.util.Collections;
1112
import java.util.Set;
13+
import java.util.concurrent.CompletableFuture;
1214
import java.util.concurrent.TimeUnit;
1315
import java.util.function.Supplier;
1416
import org.slf4j.Logger;
@@ -70,18 +72,29 @@ public static Set<String> getJavaPids() {
7072
ProcessBuilder pb = new ProcessBuilder("jps");
7173
try (TraceScope ignored = AgentTracer.get().muteTracing()) {
7274
Process p = pb.start();
75+
// start draining the subcommand's pipes asynchronously to avoid flooding them
76+
CompletableFuture<Set<String>> collecting =
77+
CompletableFuture.supplyAsync(
78+
(Supplier<Set<String>>)
79+
() -> {
80+
try (BufferedReader br =
81+
new BufferedReader(new InputStreamReader(p.getInputStream()))) {
82+
return br.lines()
83+
.filter(l -> !l.contains("jps"))
84+
.map(
85+
l -> {
86+
int idx = l.indexOf(' ');
87+
return l.substring(0, idx);
88+
})
89+
.collect(java.util.stream.Collectors.toSet());
90+
} catch (IOException e) {
91+
log.debug("Unable to list java processes via 'jps'", e);
92+
return Collections.emptySet();
93+
}
94+
});
7395
if (p.waitFor(500, TimeUnit.MILLISECONDS)) {
7496
if (p.exitValue() == 0) {
75-
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
76-
return br.lines()
77-
.filter(l -> !l.contains("jps"))
78-
.map(
79-
l -> {
80-
int idx = l.indexOf(' ');
81-
return l.substring(0, idx);
82-
})
83-
.collect(java.util.stream.Collectors.toSet());
84-
}
97+
return collecting.get();
8598
} else {
8699
log.debug("Execution of 'jps' failed with exit code {}", p.exitValue());
87100
}

0 commit comments

Comments
 (0)