Skip to content

Commit 35bcfca

Browse files
committed
Simplify ParentProcessWatcher
See eclipse-lemminx/lemminx#1753 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 438428a commit 35bcfca

1 file changed

Lines changed: 3 additions & 56 deletions

File tree

qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/ls/commons/ParentProcessWatcher.java

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,17 @@
1313
*******************************************************************************/
1414
package com.redhat.qute.ls.commons;
1515

16-
import java.io.IOException;
16+
import java.util.Optional;
1717
import java.util.concurrent.Executors;
1818
import java.util.concurrent.ScheduledExecutorService;
1919
import java.util.concurrent.ScheduledFuture;
2020
import java.util.concurrent.TimeUnit;
2121
import java.util.function.Function;
22-
import java.util.logging.Level;
2322
import java.util.logging.Logger;
2423

2524
import org.eclipse.lsp4j.jsonrpc.MessageConsumer;
2625
import org.eclipse.lsp4j.services.LanguageServer;
2726

28-
import com.google.common.io.Closeables;
29-
3027
/**
3128
* Watches the parent process PID and invokes exit if it is no longer available.
3229
* This implementation waits for periods of inactivity to start querying the
@@ -86,58 +83,8 @@ private boolean parentProcessStillRunning() {
8683
if (pid == 0 || lastActivityTime > (System.currentTimeMillis() - INACTIVITY_DELAY_SECS)) {
8784
return true;
8885
}
89-
String command;
90-
if (isWindows) {
91-
command = "cmd /c \"tasklist /FI \"PID eq " + pid + "\" | findstr " + pid + "\"";
92-
} else {
93-
command = "kill -0 " + pid;
94-
}
95-
Process process = null;
96-
boolean finished = false;
97-
try {
98-
process = Runtime.getRuntime().exec(command);
99-
finished = process.waitFor(POLL_DELAY_SECS, TimeUnit.SECONDS);
100-
if (!finished) {
101-
process.destroy();
102-
finished = process.waitFor(POLL_DELAY_SECS, TimeUnit.SECONDS); // wait for the process to stop
103-
}
104-
if (isWindows && finished && process.exitValue() > 1) {
105-
// the tasklist command should return 0 (parent process exists) or 1 (parent
106-
// process doesn't exist)
107-
LOGGER.warning("The tasklist command: '" + command + "' returns " + process.exitValue());
108-
return true;
109-
}
110-
return !finished || process.exitValue() == 0;
111-
} catch (IOException | InterruptedException e) {
112-
LOGGER.log(Level.WARNING, e.getMessage(), e);
113-
return true;
114-
} finally {
115-
if (process != null) {
116-
if (!finished) {
117-
process.destroyForcibly();
118-
}
119-
// Terminating or destroying the Process doesn't close the process handle on
120-
// Windows.
121-
// It is only closed when the Process object is garbage collected (in its
122-
// finalize() method).
123-
// On Windows, when the Java LS is idle, we need to explicitly request a GC,
124-
// to prevent an accumulation of zombie processes, as finalize() will be called.
125-
if (isWindows) {
126-
// Java >= 9 doesn't close the handle when the process is garbage collected
127-
// We need to close the opened streams
128-
if (!isJava1x) {
129-
Closeables.closeQuietly(process.getInputStream());
130-
Closeables.closeQuietly(process.getErrorStream());
131-
try {
132-
Closeables.close(process.getOutputStream(), false);
133-
} catch (IOException e) {
134-
}
135-
}
136-
System.gc();
137-
}
138-
}
139-
}
140-
86+
Optional<ProcessHandle> optionalHandle = ProcessHandle.of(pid);
87+
return optionalHandle.isPresent() && optionalHandle.get().isAlive();
14188
}
14289

14390
@Override

0 commit comments

Comments
 (0)