Skip to content

Commit

Permalink
Fix ghost terminals after using "Developer: Restart Extension Host"
Browse files Browse the repository at this point in the history
When this features restarts us, we're unable to clean up our terminals
because we're gone before we can finish disposing. Therefore we must
search for stale terminals and dispose them.
  • Loading branch information
andyleejordan committed Jul 16, 2024
1 parent 488c54e commit d69ee8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class PowerShellProcess {
// This is used to warn the user that the extension is taking longer than expected to startup.
private static warnUserThreshold = 30;

private static title = "PowerShell Extension";

public onExited: vscode.Event<void>;
private onExitedEmitter?: vscode.EventEmitter<void>;

Expand All @@ -25,7 +27,7 @@ export class PowerShellProcess {
constructor(
public exePath: string,
private bundledModulesPath: string,
private title: string,
private isTemp: boolean,
private logger: ILogger,
private startPsesArgs: string,
private sessionFilePath: vscode.Uri,
Expand Down Expand Up @@ -99,7 +101,7 @@ export class PowerShellProcess {

// Launch PowerShell in the integrated terminal
const terminalOptions: vscode.TerminalOptions = {
name: this.title,
name: this.isTemp ? `${PowerShellProcess.title} (TEMP)` : PowerShellProcess.title,
shellPath: this.exePath,
shellArgs: powerShellArgs,
cwd: await validateCwdSetting(this.logger),
Expand Down Expand Up @@ -127,6 +129,17 @@ export class PowerShellProcess {
return await this.waitForSessionFile(cancellationToken);
}

// This function is used to clean-up stale PowerShell Extension terminals,
// which can happen with `restartExtensionHost` is called because we are
// unable to finish diposing before we're gone.
public static cleanUpTerminals(): void {
for (const terminal of vscode.window.terminals) {
if (terminal.name.startsWith(PowerShellProcess.title)) {
terminal.dispose();
}
}
}

// This function should only be used after a failure has occurred because it is slow!
public async getVersionCli(): Promise<string> {
const exec = promisify(cp.execFile);
Expand Down
7 changes: 5 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export class SessionManager implements Middleware {
new PowerShellProcess(
this.PowerShellExeDetails.exePath,
bundledModulesPath,
"[TEMP] PowerShell Extension",
true,
this.logger,
this.getEditorServicesArgs(bundledModulesPath, this.PowerShellExeDetails) + "-DebugServiceOnly ",
this.getNewSessionFilePath(),
Expand Down Expand Up @@ -528,11 +528,14 @@ export class SessionManager implements Middleware {
cancellationToken: vscode.CancellationToken): Promise<PowerShellProcess> {

const bundledModulesPath = await this.getBundledModulesPath();

// Dispose any stale terminals from previous killed sessions.
PowerShellProcess.cleanUpTerminals();
const languageServerProcess =
new PowerShellProcess(
powerShellExeDetails.exePath,
bundledModulesPath,
"PowerShell Extension",
false,
this.logger,
this.getEditorServicesArgs(bundledModulesPath, powerShellExeDetails),
this.getNewSessionFilePath(),
Expand Down

0 comments on commit d69ee8a

Please sign in to comment.