Skip to content

Lock SessionManager.start() so only one session is started #4161

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

Merged
merged 1 commit into from
Aug 31, 2022
Merged
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
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<IPower
};
}

export function deactivate(): void {
export async function deactivate(): Promise<void> {
// Clean up all extension features
for (const languageClientConsumer of languageClientConsumers) {
languageClientConsumer.dispose();
Expand All @@ -192,11 +192,11 @@ export function deactivate(): void {
};

// Dispose of the current session
sessionManager.dispose();
await sessionManager.dispose();

// Dispose of the logger
logger.dispose();

// Dispose of telemetry reporter
telemetryReporter.dispose();
await telemetryReporter.dispose();
}
66 changes: 45 additions & 21 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class SessionManager implements Middleware {
private sessionDetails: IEditorServicesSessionDetails;
private sessionsFolder: vscode.Uri;
private bundledModulesPath: string;
private starting: boolean = false;
private started: boolean = false;

// Initialized by the start() method, since this requires settings
Expand Down Expand Up @@ -160,6 +161,19 @@ export class SessionManager implements Middleware {
}

public async start(exeNameOverride?: string) {
// A simple lock because this function isn't re-entrant.
if (this.started || this.starting) {
return await this.waitUntilStarted();
}
try {
this.starting = true;
await this._start(exeNameOverride);
} finally {
this.starting = false;
}
}

private async _start(exeNameOverride?: string) {
await Settings.validateCwdSetting();
this.sessionSettings = Settings.load();

Expand Down Expand Up @@ -275,31 +289,41 @@ Type 'help' to get help.
public async stop() {
this.log.write("Shutting down language client...");

if (this.sessionStatus === SessionStatus.Failed) {
// Before moving further, clear out the client and process if
// the process is already dead (i.e. it crashed).
this.languageClient = undefined;
this.languageServerProcess = undefined;
}
try {
if (this.sessionStatus === SessionStatus.Failed) {
// Before moving further, clear out the client and process if
// the process is already dead (i.e. it crashed).
this.languageClient.dispose();
this.languageClient = undefined;
this.languageServerProcess.dispose();
this.languageServerProcess = undefined;
}

this.sessionStatus = SessionStatus.Stopping;
this.sessionStatus = SessionStatus.Stopping;

// Stop the language client.
if (this.languageClient !== undefined) {
await this.languageClient.stop();
this.languageClient = undefined;
}
// Stop the language client.
if (this.languageClient !== undefined) {
await this.languageClient.stop();
this.languageClient.dispose();
this.languageClient = undefined;
}

// Kill the PowerShell process(es) we spawned.
if (this.debugSessionProcess) {
this.debugSessionProcess.dispose();
this.debugEventHandler.dispose();
}
if (this.languageServerProcess) {
this.languageServerProcess.dispose();
}
// Kill the PowerShell process(es) we spawned.
if (this.debugSessionProcess) {
this.debugSessionProcess.dispose();
this.debugSessionProcess = undefined;
this.debugEventHandler.dispose();
this.debugEventHandler = undefined;
}

this.sessionStatus = SessionStatus.NotStarted;
if (this.languageServerProcess) {
this.languageServerProcess.dispose();
this.languageServerProcess = undefined;
}
} finally {
this.sessionStatus = SessionStatus.NotStarted;
this.started = false;
}
}

public async restartSession(exeNameOverride?: string) {
Expand Down