Skip to content

Commit d69ee8a

Browse files
committed
Fix ghost terminals after using "Developer: Restart Extension Host"
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.
1 parent 488c54e commit d69ee8a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/process.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class PowerShellProcess {
1414
// This is used to warn the user that the extension is taking longer than expected to startup.
1515
private static warnUserThreshold = 30;
1616

17+
private static title = "PowerShell Extension";
18+
1719
public onExited: vscode.Event<void>;
1820
private onExitedEmitter?: vscode.EventEmitter<void>;
1921

@@ -25,7 +27,7 @@ export class PowerShellProcess {
2527
constructor(
2628
public exePath: string,
2729
private bundledModulesPath: string,
28-
private title: string,
30+
private isTemp: boolean,
2931
private logger: ILogger,
3032
private startPsesArgs: string,
3133
private sessionFilePath: vscode.Uri,
@@ -99,7 +101,7 @@ export class PowerShellProcess {
99101

100102
// Launch PowerShell in the integrated terminal
101103
const terminalOptions: vscode.TerminalOptions = {
102-
name: this.title,
104+
name: this.isTemp ? `${PowerShellProcess.title} (TEMP)` : PowerShellProcess.title,
103105
shellPath: this.exePath,
104106
shellArgs: powerShellArgs,
105107
cwd: await validateCwdSetting(this.logger),
@@ -127,6 +129,17 @@ export class PowerShellProcess {
127129
return await this.waitForSessionFile(cancellationToken);
128130
}
129131

132+
// This function is used to clean-up stale PowerShell Extension terminals,
133+
// which can happen with `restartExtensionHost` is called because we are
134+
// unable to finish diposing before we're gone.
135+
public static cleanUpTerminals(): void {
136+
for (const terminal of vscode.window.terminals) {
137+
if (terminal.name.startsWith(PowerShellProcess.title)) {
138+
terminal.dispose();
139+
}
140+
}
141+
}
142+
130143
// This function should only be used after a failure has occurred because it is slow!
131144
public async getVersionCli(): Promise<string> {
132145
const exec = promisify(cp.execFile);

src/session.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export class SessionManager implements Middleware {
350350
new PowerShellProcess(
351351
this.PowerShellExeDetails.exePath,
352352
bundledModulesPath,
353-
"[TEMP] PowerShell Extension",
353+
true,
354354
this.logger,
355355
this.getEditorServicesArgs(bundledModulesPath, this.PowerShellExeDetails) + "-DebugServiceOnly ",
356356
this.getNewSessionFilePath(),
@@ -528,11 +528,14 @@ export class SessionManager implements Middleware {
528528
cancellationToken: vscode.CancellationToken): Promise<PowerShellProcess> {
529529

530530
const bundledModulesPath = await this.getBundledModulesPath();
531+
532+
// Dispose any stale terminals from previous killed sessions.
533+
PowerShellProcess.cleanUpTerminals();
531534
const languageServerProcess =
532535
new PowerShellProcess(
533536
powerShellExeDetails.exePath,
534537
bundledModulesPath,
535-
"PowerShell Extension",
538+
false,
536539
this.logger,
537540
this.getEditorServicesArgs(bundledModulesPath, powerShellExeDetails),
538541
this.getNewSessionFilePath(),

0 commit comments

Comments
 (0)