|
13 | 13 | *******************************************************************************/ |
14 | 14 | package com.redhat.qute.ls.commons; |
15 | 15 |
|
16 | | -import java.io.IOException; |
| 16 | +import java.util.Optional; |
17 | 17 | import java.util.concurrent.Executors; |
18 | 18 | import java.util.concurrent.ScheduledExecutorService; |
19 | 19 | import java.util.concurrent.ScheduledFuture; |
20 | 20 | import java.util.concurrent.TimeUnit; |
21 | 21 | import java.util.function.Function; |
22 | | -import java.util.logging.Level; |
23 | 22 | import java.util.logging.Logger; |
24 | 23 |
|
25 | 24 | import org.eclipse.lsp4j.jsonrpc.MessageConsumer; |
26 | 25 | import org.eclipse.lsp4j.services.LanguageServer; |
27 | 26 |
|
28 | | -import com.google.common.io.Closeables; |
29 | | - |
30 | 27 | /** |
31 | 28 | * Watches the parent process PID and invokes exit if it is no longer available. |
32 | 29 | * This implementation waits for periods of inactivity to start querying the |
@@ -86,58 +83,8 @@ private boolean parentProcessStillRunning() { |
86 | 83 | if (pid == 0 || lastActivityTime > (System.currentTimeMillis() - INACTIVITY_DELAY_SECS)) { |
87 | 84 | return true; |
88 | 85 | } |
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(); |
141 | 88 | } |
142 | 89 |
|
143 | 90 | @Override |
|
0 commit comments