Skip to content

Commit cf5bfa4

Browse files
gselzerclaude
andcommitted
code-lsp: stop servers' child processes too
A launcher may start a wrapper around the actual server (e.g. ty's Python launcher). Destroying only the wrapper, when a server fails to initialize or does not shut down, left the server running, orphaned. Now its descendants are destroyed as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0dfea04 commit cf5bfa4

2 files changed

Lines changed: 109 additions & 2 deletions

File tree

‎scijava-code-lsp/src/main/java/org/scijava/code/lsp/server/ServerProcess.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static ServerProcess start(final ServerLauncher launcher,
152152
published);
153153
}
154154
catch (final Exception exc) {
155-
process.destroy();
155+
destroy(process);
156156
throw exc;
157157
}
158158
}
@@ -229,13 +229,21 @@ public void shutdown() {
229229
// NB: Stopped below, regardless.
230230
}
231231
finally {
232-
process.destroy();
232+
destroy(process);
233233
listening.cancel(true);
234234
}
235235
}
236236

237237
// -- Helper methods --
238238

239+
/** Destroy the process and all of its descendants. */
240+
private static void destroy(final Process process) {
241+
// Destroy any children (some langauge servers are wrappers around other processes)
242+
process.descendants().forEach(ProcessHandle::destroy);
243+
// Then destroy the given process.
244+
process.destroy();
245+
}
246+
239247
/** Brings the server's copy of a document up to date: whether it was not. */
240248
private boolean sync(final String uri, final String text) {
241249
lastUsed = System.currentTimeMillis();

‎scijava-code-lsp/src/test/java/org/scijava/code/lsp/server/ServerProcessesTest.java‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@
3838
import static org.scijava.code.lsp.server.FakeServers.waitFor;
3939

4040
import java.io.IOException;
41+
import java.nio.file.Paths;
4142
import java.util.Collections;
4243
import java.util.List;
44+
import java.util.Optional;
4345
import java.util.concurrent.CompletableFuture;
4446
import java.util.concurrent.ExecutionException;
4547
import java.util.concurrent.TimeUnit;
48+
import java.util.concurrent.TimeoutException;
4649
import java.util.concurrent.atomic.AtomicLong;
4750

4851
import org.junit.jupiter.api.AfterEach;
@@ -202,4 +205,100 @@ public void testTooMany() throws Exception {
202205
assertFalse(started.get(0).shutdown);
203206
assertSame(first, a.running().get());
204207
}
208+
209+
/**
210+
* Tests that a process that fails to initialize is stopped, and that
211+
* any children it started are stopped as well.
212+
*/
213+
@Test
214+
public void testNestedProcessDestruction() throws Exception {
215+
final NestedServerLauncher nested = new NestedServerLauncher();
216+
try {
217+
// NB: The wrapper never answers, so initializing it times out. Started
218+
// directly, as ServerProcesses waits a minute for that.
219+
assertThrows(
220+
TimeoutException.class,
221+
() -> ServerProcess.start(nested, ENV, new StderrLogService(), 1000)
222+
);
223+
waitFor(() -> !nested.wrapper.isAlive());
224+
waitFor(() -> !nested.child.isAlive());
225+
}
226+
finally {
227+
// NB: Whatever happened, leave nothing running.
228+
if (nested.child != null) nested.child.destroyForcibly();
229+
if (nested.wrapper != null) nested.wrapper.destroyForcibly();
230+
}
231+
}
232+
233+
/**
234+
* Starts a wrapper (see {@link Wrapper}) {@link Process} which starts a child
235+
* of its own.
236+
*/
237+
static class NestedServerLauncher implements ServerLauncher {
238+
239+
volatile ProcessHandle wrapper, child;
240+
241+
@Override
242+
public String language() {
243+
return "python";
244+
}
245+
246+
@Override
247+
public Process start(final Environment env) throws IOException {
248+
// Starts a JVM process running the main class of Wrapper with a
249+
// "wrap" arg
250+
final Process process = new ProcessBuilder(java(), "-cp", System
251+
.getProperty("java.class.path"), Wrapper.class.getName(), "wrap")
252+
.redirectError(ProcessBuilder.Redirect.DISCARD).start();
253+
wrapper = process.toHandle();
254+
try {
255+
// Wait until the wrapper starts up its child to return.
256+
// NB: Notes the child now: once the wrapper is gone, it is no one's
257+
// descendant, so it could not be found to check.
258+
waitFor(() -> javaChild(process).isPresent());
259+
}
260+
catch (final InterruptedException exc) {
261+
throw new IOException(exc);
262+
}
263+
child = javaChild(process).get();
264+
return process;
265+
}
266+
267+
/**
268+
* The child the wrapper started, if yet. NB: Not e.g. the console host
269+
* Windows starts for it too.
270+
*/
271+
private static Optional<ProcessHandle> javaChild(final Process process) {
272+
return process.children().filter(h -> h.info().command().map(c -> c
273+
.toLowerCase().matches(".*[\\\\/]java(\\.exe)?")).orElse(false))
274+
.findAny();
275+
}
276+
}
277+
278+
/**
279+
* A process that sleeps forever; given {@code wrap}, it first starts another
280+
* (which just sleeps), sharing its input and output.
281+
*/
282+
public static class Wrapper {
283+
284+
public static void main(final String... args) throws Exception {
285+
if (args.length > 0 && args[0].equals("wrap")) {
286+
// If asked to wrap start up another instance.
287+
new ProcessBuilder(
288+
java(),
289+
"-cp",
290+
System.getProperty("java.class.path"),
291+
Wrapper.class.getName()
292+
).inheritIO().start();
293+
}
294+
// This Process does nothing (specifically, it DOESN'T speak LSP)
295+
Thread.sleep(Long.MAX_VALUE);
296+
}
297+
}
298+
299+
/** The java executable of this JVM. */
300+
private static String java() {
301+
return Paths.get(System.getProperty("java.home"), "bin", "java")
302+
.toString();
303+
}
205304
}

0 commit comments

Comments
 (0)