Repository navigation
Terminate Copilot language server process tree on shutdown #505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dietrich Travkin (travkin79)
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
travkin79:fix-408-stop-CLS-on-close
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bab4102
test: reproduce CLS surviving language server stop
travkin79 af1275e
fix: terminate CLS process tree on stop
travkin79 2694ad8
fix: do not start CLS while Copilot core is stopping
travkin79 286169c
test: reproduce shutdown blocked by busy CLS
travkin79 6630e79
fix: do not block shutdown on busy CLS when saving chat preferences
travkin79 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
...re.test/src/com/microsoft/copilot/eclipse/core/lsp/CopilotLanguageServerProcessTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| package com.microsoft.copilot.eclipse.core.lsp; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.eclipse.lsp4e.LanguageServerWrapper; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| class CopilotLanguageServerProcessTests { | ||
|
|
||
| // Launched as single-file source in a separate JVM: the OSGi test runtime offers no plain classpath to | ||
| // start a compiled test class with. | ||
| private static final String SLEEPER_SOURCE = """ | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Arrays; | ||
|
|
||
| public class Sleeper { | ||
| public static void main(String[] args) throws Exception { | ||
| int first = 0; | ||
| if (args.length > 0 && args[0].startsWith("--await=")) { | ||
| // Spawn the child only once the trigger file exists | ||
| Path trigger = Path.of(args[0].substring("--await=".length())); | ||
| while (!Files.exists(trigger)) { | ||
| Thread.sleep(50); | ||
| } | ||
| first = 1; | ||
| } | ||
| if (args.length > first) { | ||
| new ProcessBuilder(Arrays.copyOfRange(args, first, args.length)) | ||
| .redirectOutput(ProcessBuilder.Redirect.DISCARD) | ||
| .redirectError(ProcessBuilder.Redirect.DISCARD).start(); | ||
| } | ||
| Thread.sleep(120_000); | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| @TempDir | ||
| Path tempDir; | ||
|
|
||
| private Process server; | ||
| private ProcessHandle serverChild; | ||
|
|
||
| @AfterEach | ||
| void killLeftovers() { | ||
| if (serverChild != null) { | ||
| serverChild.destroyForcibly(); | ||
| } | ||
| if (server != null) { | ||
| server.destroyForcibly(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testStop_serverIgnoresShutdown_terminatesServerProcessTree() throws Exception { | ||
| Path sleeper = tempDir.resolve("Sleeper.java"); | ||
| Files.writeString(sleeper, SLEEPER_SOURCE); | ||
| String java = ProcessHandle.current().info().command().orElseThrow(); | ||
| // The "server" spawns a child, like the CLS spawns MCP servers. | ||
| server = new ProcessBuilder(List.of(java, sleeper.toString(), java, sleeper.toString())) | ||
| .redirectOutput(ProcessBuilder.Redirect.DISCARD).redirectError(ProcessBuilder.Redirect.DISCARD).start(); | ||
| serverChild = awaitChild(server); | ||
|
|
||
| // A mocked wrapper does nothing on stop(), like LSP4E when the JVM exits before its async close runs. | ||
| LanguageServerWrapper wrapper = mock(LanguageServerWrapper.class); | ||
| when(wrapper.getProcessHandle()).thenReturn(server.toHandle()); | ||
|
|
||
| new CopilotLanguageServerConnection(wrapper).stop(); | ||
|
|
||
| server.onExit().get(10, TimeUnit.SECONDS); | ||
| assertFalse(server.isAlive(), "language server must be terminated"); | ||
| serverChild.onExit().get(10, TimeUnit.SECONDS); | ||
| assertFalse(serverChild.isAlive(), "language server child must be terminated"); | ||
| } | ||
|
|
||
| @Test | ||
| void testStop_childSpawnedWhileStopping_terminatesChild() throws Exception { | ||
| Path sleeper = tempDir.resolve("Sleeper.java"); | ||
| Files.writeString(sleeper, SLEEPER_SOURCE); | ||
| Path trigger = tempDir.resolve("spawn-child"); | ||
| String java = ProcessHandle.current().info().command().orElseThrow(); | ||
| server = new ProcessBuilder(List.of(java, sleeper.toString(), "--await=" + trigger, java, sleeper.toString())) | ||
| .redirectOutput(ProcessBuilder.Redirect.DISCARD).redirectError(ProcessBuilder.Redirect.DISCARD).start(); | ||
| LanguageServerWrapper wrapper = mock(LanguageServerWrapper.class); | ||
| when(wrapper.getProcessHandle()).thenReturn(server.toHandle()); | ||
| // stop() captures the process tree first, so the child spawned here is new to it | ||
| doAnswer(invocation -> { | ||
| Files.createFile(trigger); | ||
| serverChild = awaitChild(server); | ||
| return null; | ||
| }).when(wrapper).stop(); | ||
|
|
||
| new CopilotLanguageServerConnection(wrapper).stop(); | ||
|
|
||
| serverChild.onExit().get(10, TimeUnit.SECONDS); | ||
| assertFalse(serverChild.isAlive(), "child spawned while stopping must be terminated"); | ||
| } | ||
|
|
||
| private static ProcessHandle awaitChild(Process process) throws IOException, InterruptedException { | ||
| long deadline = System.currentTimeMillis() + 60_000; | ||
| while (System.currentTimeMillis() < deadline) { | ||
| Optional<ProcessHandle> child = process.children().filter(CopilotLanguageServerProcessTests::isJava) | ||
| .findFirst(); | ||
| if (child.isPresent()) { | ||
| return child.get(); | ||
| } | ||
| assertTrue(process.isAlive(), "test server process died unexpectedly"); | ||
| Thread.sleep(100); | ||
| } | ||
| throw new IOException("Test server process did not start a child process"); | ||
| } | ||
|
|
||
| // Skips helper processes such as conhost.exe on Windows | ||
| private static boolean isJava(ProcessHandle process) { | ||
| return process.info().command().map(command -> Path.of(command).getFileName().toString().startsWith("java")) | ||
| .orElse(false); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.