|
38 | 38 | import static org.scijava.code.lsp.server.FakeServers.waitFor; |
39 | 39 |
|
40 | 40 | import java.io.IOException; |
| 41 | +import java.nio.file.Paths; |
41 | 42 | import java.util.Collections; |
42 | 43 | import java.util.List; |
| 44 | +import java.util.Optional; |
43 | 45 | import java.util.concurrent.CompletableFuture; |
44 | 46 | import java.util.concurrent.ExecutionException; |
45 | 47 | import java.util.concurrent.TimeUnit; |
| 48 | +import java.util.concurrent.TimeoutException; |
46 | 49 | import java.util.concurrent.atomic.AtomicLong; |
47 | 50 |
|
48 | 51 | import org.junit.jupiter.api.AfterEach; |
@@ -202,4 +205,100 @@ public void testTooMany() throws Exception { |
202 | 205 | assertFalse(started.get(0).shutdown); |
203 | 206 | assertSame(first, a.running().get()); |
204 | 207 | } |
| 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 | + } |
205 | 304 | } |
0 commit comments