|
5 | 5 | import datadog.trace.context.TraceScope; |
6 | 6 | import de.thetaphi.forbiddenapis.SuppressForbidden; |
7 | 7 | import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
8 | 9 | import java.io.InputStreamReader; |
9 | 10 | import java.lang.management.ManagementFactory; |
10 | 11 | import java.util.Collections; |
11 | 12 | import java.util.Set; |
| 13 | +import java.util.concurrent.CompletableFuture; |
12 | 14 | import java.util.concurrent.TimeUnit; |
13 | 15 | import java.util.function.Supplier; |
14 | 16 | import org.slf4j.Logger; |
@@ -70,18 +72,29 @@ public static Set<String> getJavaPids() { |
70 | 72 | ProcessBuilder pb = new ProcessBuilder("jps"); |
71 | 73 | try (TraceScope ignored = AgentTracer.get().muteTracing()) { |
72 | 74 | 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 | + }); |
73 | 95 | if (p.waitFor(500, TimeUnit.MILLISECONDS)) { |
74 | 96 | 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(); |
85 | 98 | } else { |
86 | 99 | log.debug("Execution of 'jps' failed with exit code {}", p.exitValue()); |
87 | 100 | } |
|
0 commit comments