diff --git a/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java b/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java index 49006e225..cad67e9b3 100644 --- a/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java +++ b/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java @@ -36,6 +36,7 @@ import hudson.slaves.JNLPLauncher; import hudson.slaves.RetentionStrategy; import hudson.slaves.SlaveComputer; +import hudson.util.ProcessTree; import hudson.util.StreamCopyThread; import hudson.util.VersionNumber; import java.io.File; @@ -46,6 +47,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.jar.JarFile; @@ -77,6 +79,7 @@ public final class InboundAgentRule extends ExternalResource { private static final Logger LOGGER = Logger.getLogger(InboundAgentRule.class.getName()); + private final String id = UUID.randomUUID().toString(); private final ConcurrentMap procs = new ConcurrentHashMap<>(); /** @@ -348,6 +351,8 @@ public void start(AgentArguments agentArguments, Options options) throws Excepti cmd.addAll(agentArguments.commandLineArgs); ProcessBuilder pb = new ProcessBuilder(cmd); pb.redirectErrorStream(true); + pb.environment().put("INBOUND_AGENT_RULE_ID", id); + pb.environment().put("INBOUND_AGENT_RULE_NAME", options.getName()); LOGGER.info(() -> "Running: " + pb.command()); Process proc = pb.start(); procs.put(options.getName(), proc); @@ -391,6 +396,7 @@ public void stop(@NonNull RealJenkinsRule rjr, @NonNull String name) throws Thro public void stop(@NonNull String name) throws InterruptedException { Process proc = procs.remove(name); if (proc != null) { + ProcessTree.get().killAll(proc, Map.of("INBOUND_AGENT_RULE_ID", id, "INBOUND_AGENT_RULE_NAME", name)); proc.destroyForcibly(); proc.waitFor(); }