From e9840889e49053ab02cc3a38ab93ecb94e949020 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Sun, 24 Feb 2019 00:28:42 +0100 Subject: [PATCH] File handle leak on Win with Java 11(.0.2) Signed-off-by: Snjezana Peco --- .../lsp4xml/commons/ParentProcessWatcher.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java index 8919f788a..b3574cbeb 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/commons/ParentProcessWatcher.java @@ -21,6 +21,8 @@ import org.eclipse.lsp4j.jsonrpc.MessageConsumer; import org.eclipse.lsp4j.services.LanguageServer; +import com.google.common.io.Closeables; + /** * Watches the parent process PID and invokes exit if it is no longer available. * This implementation waits for periods of inactivity to start querying the PIDs. @@ -28,6 +30,7 @@ public final class ParentProcessWatcher implements Runnable, Function{ private static final Logger LOGGER = Logger.getLogger(ParentProcessWatcher.class.getName()); + private static final boolean isJava1x = System.getProperty("java.version").startsWith("1."); /** * Exit code returned when XML Language Server is forced to exit. @@ -37,7 +40,7 @@ public final class ParentProcessWatcher implements Runnable, Function= 0; private static final long INACTIVITY_DELAY_SECS = 30 *1000; - private static final int POLL_DELAY_SECS = 2; + private static final int POLL_DELAY_SECS = 10; private volatile long lastActivityTime; private final ProcessLanguageServer server; private ScheduledFuture task; @@ -106,6 +109,16 @@ private boolean parentProcessStillRunning() { // On Windows, when the Java LS is idle, we need to explicitly request a GC, // to prevent an accumulation of zombie processes, as finalize() will be called. if (isWindows) { + // Java >= 9 doesn't close the handle when the process is garbage collected + // We need to close the opened streams + if (!isJava1x) { + Closeables.closeQuietly(process.getInputStream()); + Closeables.closeQuietly(process.getErrorStream()); + try { + Closeables.close(process.getOutputStream(), false); + } catch (IOException e) { + } + } System.gc(); } } @@ -121,4 +134,4 @@ public MessageConsumer apply(final MessageConsumer consumer) { consumer.consume(message); }; } -} \ No newline at end of file +}