Skip to content

Commit 163ceca

Browse files
committed
Restore pty host connection correctly after restart
Part of microsoft#185254
1 parent 42457bd commit 163ceca

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/vs/workbench/contrib/terminal/browser/baseTerminalBackend.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export abstract class BaseTerminalBackend extends Disposable {
4242

4343
let unresponsiveStatusBarEntry: IStatusbarEntry;
4444
let statusBarAccessor: IStatusbarEntryAccessor;
45+
let hasStarted = false;
4546

4647
// Attach pty host listeners
4748
if (this._ptyHostController.onPtyHostExit) {
@@ -51,7 +52,12 @@ export abstract class BaseTerminalBackend extends Disposable {
5152
}
5253
if (this._ptyHostController.onPtyHostStart) {
5354
this._register(this._ptyHostController.onPtyHostStart(() => {
54-
this._onPtyHostRestart.fire();
55+
this._logService.debug(`The terminal's pty host process is starting`);
56+
// Only fire the event on the 2nd
57+
if (hasStarted) {
58+
this._onPtyHostRestart.fire();
59+
}
60+
hasStarted = true;
5561
statusBarAccessor?.dispose();
5662
this._isPtyHostUnresponsive = false;
5763
}));

src/vs/workbench/contrib/terminal/electron-sandbox/localTerminalBackend.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { Emitter } from 'vs/base/common/event';
6+
import { Emitter, Event } from 'vs/base/common/event';
77
import { IProcessEnvironment, isMacintosh, isWindows, OperatingSystem } from 'vs/base/common/platform';
88
import { withNullAsUndefined } from 'vs/base/common/types';
99
import { URI } from 'vs/base/common/uri';
@@ -52,8 +52,7 @@ export class LocalTerminalBackendContribution implements IWorkbenchContribution
5252
class LocalTerminalBackend extends BaseTerminalBackend implements ITerminalBackend {
5353
readonly remoteAuthority = undefined;
5454

55-
private readonly _proxy: IPtyService;
56-
private readonly _clientEventually: DeferredPromise<MessagePortClient> = new DeferredPromise();
55+
private _proxy!: IPtyService;
5756
private readonly _ptys: Map<number, LocalPty> = new Map();
5857

5958
private readonly _whenConnected = new DeferredPromise<void>();
@@ -84,12 +83,15 @@ class LocalTerminalBackend extends BaseTerminalBackend implements ITerminalBacke
8483
) {
8584
super(_localPtyService, logService, historyService, _configurationResolverService, statusBarService, workspaceContextService);
8685

87-
this._proxy = ProxyChannel.toService<IPtyService>(getDelayedChannel(this._clientEventually.p.then(client => client.getChannel(TerminalIpcChannels.PtyHostWindow))));
88-
89-
this._connectToDirectProxy();
86+
this._register(Event.runAndSubscribe(this.onPtyHostRestart, () => {
87+
this._logService.debug('Starting pty host');
88+
const clientEventually = new DeferredPromise<MessagePortClient>();
89+
this._proxy = ProxyChannel.toService<IPtyService>(getDelayedChannel(clientEventually.p.then(client => client.getChannel(TerminalIpcChannels.PtyHostWindow))));
90+
this._connectToDirectProxy(clientEventually);
91+
}));
9092
}
9193

92-
private async _connectToDirectProxy(): Promise<void> {
94+
private async _connectToDirectProxy(clientEventually: DeferredPromise<MessagePortClient>): Promise<void> {
9395
// The pty host should not get launched until the first window restored phase
9496
await this._lifecycleService.when(LifecyclePhase.Restored);
9597

@@ -103,7 +105,7 @@ class LocalTerminalBackend extends BaseTerminalBackend implements ITerminalBacke
103105
// used for pty host management messages, it would make sense in the future to use a
104106
// separate interface/service for this one.
105107
const client = new MessagePortClient(port, `window:${this._environmentService.window.id}`);
106-
this._clientEventually.complete(client);
108+
clientEventually.complete(client);
107109

108110
// Attach process listeners
109111
this._proxy.onProcessData(e => this._ptys.get(e.id)?.handleData(e.event));

0 commit comments

Comments
 (0)