Skip to content

Commit 17f149d

Browse files
authored
Shutdown LSP server on request (#13475)
1 parent 2ca08b0 commit 17f149d

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Extension/src/LanguageServer/client.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function hasTrustedCompilerPaths(): boolean {
8989

9090
// Data shared by all clients.
9191
let languageClient: LanguageClient;
92-
let firstClientStarted: Promise<void>;
92+
let firstClientStarted: Promise<{ wasShutdown: boolean }>;
9393
let languageClientCrashedNeedsRestart: boolean = false;
9494
const languageClientCrashTimes: number[] = [];
9595
let compilerDefaults: configs.CompilerDefaults | undefined;
@@ -508,6 +508,10 @@ interface CppInitializationParams {
508508
settings: SettingsParams;
509509
}
510510

511+
interface CppInitializationResult {
512+
shouldShutdown: boolean;
513+
}
514+
511515
interface TagParseStatus {
512516
localizeStringParams: LocalizeStringParams;
513517
isPaused: boolean;
@@ -585,7 +589,7 @@ export interface CopilotCompletionContextParams {
585589

586590
// Requests
587591
const PreInitializationRequest: RequestType<void, string, void> = new RequestType<void, string, void>('cpptools/preinitialize');
588-
const InitializationRequest: RequestType<CppInitializationParams, void, void> = new RequestType<CppInitializationParams, void, void>('cpptools/initialize');
592+
const InitializationRequest: RequestType<CppInitializationParams, CppInitializationResult, void> = new RequestType<CppInitializationParams, CppInitializationResult, void>('cpptools/initialize');
589593
const QueryCompilerDefaultsRequest: RequestType<QueryDefaultCompilerParams, configs.CompilerDefaults, void> = new RequestType<QueryDefaultCompilerParams, configs.CompilerDefaults, void>('cpptools/queryCompilerDefaults');
590594
const SwitchHeaderSourceRequest: RequestType<SwitchHeaderSourceParams, string, void> = new RequestType<SwitchHeaderSourceParams, string, void>('cpptools/didSwitchHeaderSource');
591595
const GetDiagnosticsRequest: RequestType<void, GetDiagnosticsResult, void> = new RequestType<void, GetDiagnosticsResult, void>('cpptools/getDiagnostics');
@@ -1310,7 +1314,12 @@ export class DefaultClient implements Client {
13101314
private async init(rootUri: vscode.Uri | undefined, isFirstClient: boolean) {
13111315
ui = getUI();
13121316
ui.bind(this);
1313-
await firstClientStarted;
1317+
if ((await firstClientStarted).wasShutdown) {
1318+
this.isSupported = false;
1319+
DefaultClient.isStarted.resolve();
1320+
return;
1321+
}
1322+
13141323
try {
13151324
const workspaceFolder: vscode.WorkspaceFolder | undefined = this.rootFolder;
13161325
this.innerConfiguration = new configs.CppProperties(this, rootUri, workspaceFolder);
@@ -1574,7 +1583,7 @@ export class DefaultClient implements Client {
15741583
};
15751584
}
15761585

1577-
private async createLanguageClient(): Promise<void> {
1586+
private async createLanguageClient(): Promise<{ wasShutdown: boolean }> {
15781587
this.currentCaseSensitiveFileSupport = new PersistentWorkspaceState<boolean>("CPP.currentCaseSensitiveFileSupport", false);
15791588
let resetDatabase: boolean = false;
15801589
const serverModule: string = getLanguageServerFileName();
@@ -1707,7 +1716,15 @@ export class DefaultClient implements Client {
17071716
// Move initialization to a separate message, so we can see log output from it.
17081717
// A request is used in order to wait for completion and ensure that no subsequent
17091718
// higher priority message may be processed before the Initialization request.
1710-
await languageClient.sendRequest(InitializationRequest, cppInitializationParams);
1719+
const initializeResult = await languageClient.sendRequest(InitializationRequest, cppInitializationParams);
1720+
1721+
// If the server requested shutdown, then reload with the failsafe (null) client.
1722+
if (initializeResult.shouldShutdown) {
1723+
await languageClient.stop();
1724+
await clients.recreateClients(true);
1725+
}
1726+
1727+
return { wasShutdown: initializeResult.shouldShutdown };
17111728
}
17121729

17131730
public async sendDidChangeSettings(): Promise<void> {

0 commit comments

Comments
 (0)