Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ import { RequestCancelled, ServerCancelled, createProtocolFilter } from './proto
import * as refs from './references';
import { CppSettings, OtherSettings, SettingsParams, WorkspaceFolderSettingsParams } from './settings';
import { SettingsTracker } from './settingsTracker';
import { StuckInitializationNotice } from './stuckInitializationNotice';
import { ConfigurationType, LanguageStatusUI, getUI } from './ui';
import { handleChangedFromCppToC, makeLspRange, makeVscodeLocation, makeVscodeRange, withCancellation } from './utils';
import { CrashAction, maxConsecutiveInitializationCrashes, stuckInitializingNoticeDelayMs, WorkspaceInitializationMonitor } from './workspaceInitializationMonitor';
import minimatch = require("minimatch");

function deepCopy(obj: any) {
Expand All @@ -90,7 +92,23 @@ const languageClient: LanguageClient = new LanguageClient();
let firstClientStarted: Promise<{ wasShutdown: boolean }>;
let languageClientHasCrashed: boolean = false;
let languageClientCrashedNeedsRestart: boolean = false;
const languageClientCrashTimes: number[] = [];
// The key is not shared with the excessive files warning: dismissing one message says nothing about the other.
const showStuckInitializationNoticeKey: string = 'CPP.showStuckInitializationNotice';
const stuckInitializationNotice: StuckInitializationNotice = new StuckInitializationNotice({
appendLine: (message: string) => getOutputChannelLogger().appendLine(message),
showInformationMessage: (message: string, ...items: string[]) => vscode.window.showInformationMessage(message, ...items),
openLearnMore: () => void vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://go.microsoft.com/fwlink/?linkid=2333292')),
isSuppressed: () => !new PersistentWorkspaceState<boolean>(showStuckInitializationNoticeKey, true).Value,
suppress: () => { new PersistentWorkspaceState<boolean>(showStuckInitializationNoticeKey, true).Value = false; }
});
const workspaceInitializationMonitor: WorkspaceInitializationMonitor = new WorkspaceInitializationMonitor(() => {
void stuckInitializationNotice.notify(
localize('workspace.initialization.stuck',
'The C/C++ extension has been initializing this workspace for more than {0} minutes. If the workspace is very large or contains symbolic links that lead back into it, excluding folders with the "C_Cpp.files.exclude" setting may help.',
stuckInitializingNoticeDelayMs / 60000),
localize('learn.more', 'Learn More'),
localize('dont.show.again', 'Don\'t Show Again')).catch(logAndReturn.undefined);
});
let compilerDefaults: configs.CompilerDefaults | undefined;
let diagnosticsCollectionIntelliSense: vscode.DiagnosticCollection;
let diagnosticsCollectionRefactor: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -1730,21 +1748,10 @@ export class DefaultClient implements Client {
error: (_error, _message, _count) => ({ action: ErrorAction.Continue }),
closed: () => {
languageClientHasCrashed = true;
languageClientCrashTimes.push(Date.now());
languageClientCrashedNeedsRestart = true;
let restart: boolean = true;
if (languageClientCrashTimes.length < 5) {
void clients.recreateClients();
} else {
const elapsed: number = languageClientCrashTimes[languageClientCrashTimes.length - 1] - languageClientCrashTimes[0];
if (elapsed <= 3 * 60 * 1000) {
void clients.recreateClients(true);
restart = false;
} else {
languageClientCrashTimes.shift();
void clients.recreateClients();
}
}
const crashAction: CrashAction = workspaceInitializationMonitor.serverCrashed(Date.now());
const restart: boolean = crashAction === CrashAction.Restart;
void clients.recreateClients(!restart);

// Wait 1 second to allow time for the file watcher to signal a crash call stack write has occurred.
setTimeout(() => {
Expand All @@ -1767,7 +1774,10 @@ export class DefaultClient implements Client {
}, 1000);

const message: string = restart ? localize('server.crashed.restart', 'The language server crashed. Restarting...')
: localize('server.crashed2', 'The language server crashed 5 times in the last 3 minutes. It will not be restarted.');
: crashAction === CrashAction.StopCrashedTooOften ? localize('server.crashed2', 'The language server crashed 5 times in the last 3 minutes. It will not be restarted.')
: localize('server.crashed3',
'The language server crashed {0} times in a row while initializing the workspace. It will not be restarted. If the workspace is very large or contains symbolic links that lead back into it, excluding folders with the "C_Cpp.files.exclude" setting may help.',
maxConsecutiveInitializationCrashes);

// We manually restart the language server so tell the LanguageClient not to do it automatically for us.
return { action: CloseAction.DoNotRestart, message };
Expand All @@ -1784,6 +1794,7 @@ export class DefaultClient implements Client {
};

// Reset all UI state to default, in case this is a restart after a crash.
workspaceInitializationMonitor.serverStarted();
this.model.isIndexingWorkspace.Value = false;
this.model.isParsingWorkspace.Value = false;
this.model.isParsingWorkspacePaused.Value = false;
Expand Down Expand Up @@ -2912,6 +2923,7 @@ export class DefaultClient implements Client {
testHook.updateStatus(status);
} else if (message.endsWith("Parsing")) {
this.model.isParsingWorkspace.Value = true;
workspaceInitializationMonitor.setParsingWorkspace(true);
this.model.isInitializingWorkspace.Value = false;
this.model.isIndexingWorkspace.Value = false;
const status: IntelliSenseStatus = { status: Status.TagParsingBegun };
Expand All @@ -2920,14 +2932,17 @@ export class DefaultClient implements Client {
this.model.isInitializingWorkspace.Value = true;
this.model.isIndexingWorkspace.Value = false;
this.model.isParsingWorkspace.Value = false;
workspaceInitializationMonitor.setParsingWorkspace(false);
} else if (message.endsWith("Indexing")) {
this.model.isIndexingWorkspace.Value = true;
this.model.isInitializingWorkspace.Value = false;
this.model.isParsingWorkspace.Value = false;
workspaceInitializationMonitor.setParsingWorkspace(false);
} else if (message.endsWith("Failed")) {
this.model.isInitializingWorkspace.Value = false;
this.model.isIndexingWorkspace.Value = false;
this.model.isParsingWorkspace.Value = false;
workspaceInitializationMonitor.setParsingWorkspace(false);
this.model.isParsingFiles.Value = false;
} else if (message.endsWith("files")) {
this.model.isParsingFiles.Value = true;
Expand All @@ -2943,6 +2958,7 @@ export class DefaultClient implements Client {
testHook.updateStatus(status);
} else if (message.endsWith("Parsing done")) { // Tag Parser Ready
this.model.isParsingWorkspace.Value = false;
workspaceInitializationMonitor.setParsingWorkspace(false);
const status: IntelliSenseStatus = { status: Status.TagParsingDone };
testHook.updateStatus(status);
util.setProgress(util.getProgressParseRootSuccess());
Expand Down Expand Up @@ -2970,6 +2986,7 @@ export class DefaultClient implements Client {
}

private updateTagParseStatus(tagParseStatus: TagParseStatus): void {
workspaceInitializationMonitor.tagParseStatusChanged(tagParseStatus.localizeStringParams.text, tagParseStatus.isPaused);
this.model.parsingWorkspaceStatus.Value = getLocalizedString(tagParseStatus.localizeStringParams);
this.model.isParsingWorkspacePaused.Value = tagParseStatus.isPaused;
}
Expand Down Expand Up @@ -3315,10 +3332,12 @@ export class DefaultClient implements Client {
}

public async pauseParsing(): Promise<void> {
workspaceInitializationMonitor.pauseRequested();
return this.languageClient.sendNotification(PauseParsingNotification);
}

public async resumeParsing(): Promise<void> {
workspaceInitializationMonitor.resumeRequested();
return this.languageClient.sendNotification(ResumeParsingNotification);
}

Expand Down Expand Up @@ -4402,6 +4421,7 @@ export class DefaultClient implements Client {
}

public static stopLanguageClient(): Thenable<void> {
workspaceInitializationMonitor.serverStopped();
return languageClient ? languageClient.stop() : Promise.resolve();
}

Expand Down
41 changes: 41 additions & 0 deletions Extension/src/LanguageServer/stuckInitializationNotice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';

/** What the notice needs from VS Code. It is passed in so that the notice can be tested without it. */
export interface StuckInitializationNoticeHost {
/** Writes a line to the C/C++ output channel. */
appendLine(message: string): void;
showInformationMessage(message: string, ...items: string[]): Thenable<string | undefined>;
openLearnMore(): void;
/** Whether the user chose not to see the message in this workspace again. */
isSuppressed(): boolean;
suppress(): void;
}

/**
* Tells the user that the language server has been initializing the workspace for a long time.
* Every occurrence is logged. The message is shown once per session, because a language server that is
* restarted after a crash usually gets into the same state again.
*/
export class StuckInitializationNotice {
private shown: boolean = false;

constructor(private readonly host: StuckInitializationNoticeHost) { }

public async notify(message: string, learnMore: string, dontShowAgain: string): Promise<void> {
this.host.appendLine(message);
if (this.shown || this.host.isSuppressed()) {
return;
}
this.shown = true;
const response: string | undefined = await this.host.showInformationMessage(message, learnMore, dontShowAgain);
if (response === dontShowAgain) {
this.host.suppress();
} else if (response === learnMore) {
this.host.openLearnMore();
}
}
}
Loading
Loading