mirror of https://github.com/apache/cloudstack.git
Fix potential leaks in executePipedCommands (#12478)
This commit is contained in:
parent
b5e9178078
commit
cd5bb09d0d
|
|
@ -40,9 +40,11 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.cloudstack.utils.security.KeyStoreUtils;
|
import org.apache.cloudstack.utils.security.KeyStoreUtils;
|
||||||
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
@ -708,13 +710,31 @@ public class Script implements Callable<String> {
|
||||||
return executeCommandForExitValue(0, command);
|
return executeCommandForExitValue(0, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void cleanupProcesses(AtomicReference<List<Process>> processesRef) {
|
||||||
|
List<Process> processes = processesRef.get();
|
||||||
|
if (CollectionUtils.isNotEmpty(processes)) {
|
||||||
|
for (Process process : processes) {
|
||||||
|
if (process == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
LOGGER.trace(String.format("Cleaning up process [%s] from piped commands.", process.pid()));
|
||||||
|
IOUtils.closeQuietly(process.getErrorStream());
|
||||||
|
IOUtils.closeQuietly(process.getOutputStream());
|
||||||
|
IOUtils.closeQuietly(process.getInputStream());
|
||||||
|
process.destroyForcibly();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Pair<Integer, String> executePipedCommands(List<String[]> commands, long timeout) {
|
public static Pair<Integer, String> executePipedCommands(List<String[]> commands, long timeout) {
|
||||||
if (timeout <= 0) {
|
if (timeout <= 0) {
|
||||||
timeout = DEFAULT_TIMEOUT;
|
timeout = DEFAULT_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
final AtomicReference<List<Process>> processesRef = new AtomicReference<>();
|
||||||
Callable<Pair<Integer, String>> commandRunner = () -> {
|
Callable<Pair<Integer, String>> commandRunner = () -> {
|
||||||
List<ProcessBuilder> builders = commands.stream().map(ProcessBuilder::new).collect(Collectors.toList());
|
List<ProcessBuilder> builders = commands.stream().map(ProcessBuilder::new).collect(Collectors.toList());
|
||||||
List<Process> processes = ProcessBuilder.startPipeline(builders);
|
List<Process> processes = ProcessBuilder.startPipeline(builders);
|
||||||
|
processesRef.set(processes);
|
||||||
Process last = processes.get(processes.size()-1);
|
Process last = processes.get(processes.size()-1);
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(last.getInputStream()))) {
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(last.getInputStream()))) {
|
||||||
String line;
|
String line;
|
||||||
|
|
@ -741,6 +761,8 @@ public class Script implements Callable<String> {
|
||||||
result.second(ERR_TIMEOUT);
|
result.second(ERR_TIMEOUT);
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
LOGGER.error("Error executing piped commands", e);
|
LOGGER.error("Error executing piped commands", e);
|
||||||
|
} finally {
|
||||||
|
cleanupProcesses(processesRef);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue