Skip to content

Commit a12c412

Browse files
committed
Start pty host when the first window opens
1 parent 2fbfcb1 commit a12c412

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

src/vs/platform/terminal/electron-main/electronPtyHostStarter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@ import { Client as MessagePortClient } from 'vs/base/parts/ipc/electron-main/ipc
1515
import { IpcMainEvent } from 'electron';
1616
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
1717
import { DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
18+
import { Emitter } from 'vs/base/common/event';
1819

1920
export class ElectronPtyHostStarter implements IPtyHostStarter {
2021

2122
private utilityProcess: UtilityProcess | undefined = undefined;
2223

24+
private readonly _onBeforeWindowConnection = new Emitter<void>();
25+
readonly onBeforeWindowConnection = this._onBeforeWindowConnection.event;
26+
2327
constructor(
2428
private readonly _reconnectConstants: IReconnectConstants,
2529
@IEnvironmentService private readonly _environmentService: INativeEnvironmentService,
2630
@ILifecycleMainService private readonly _lifecycleMainService: ILifecycleMainService,
2731
@ILogService private readonly _logService: ILogService
2832
) {
33+
// Listen for new windows to establish connection directly to pty host
34+
validatedIpcMain.on('vscode:createPtyHostMessageChannel', (e, nonce) => this._onWindowConnection(e, nonce));
2935
}
3036

3137
start(lastPtyId: number): IPtyHostConnection {
@@ -52,9 +58,6 @@ export class ElectronPtyHostStarter implements IPtyHostStarter {
5258
const port = this.utilityProcess.connect();
5359
const client = new MessagePortClient(port, 'ptyHost');
5460

55-
// Listen for new windows to establish connection directly to pty host
56-
validatedIpcMain.on('vscode:createPtyHostMessageChannel', (e, nonce) => this._onWindowConnection(e, nonce));
57-
5861
// TODO: Do we need to listen for window close to close the port?
5962

6063
const store = new DisposableStore();
@@ -85,6 +88,8 @@ export class ElectronPtyHostStarter implements IPtyHostStarter {
8588
}
8689

8790
private _onWindowConnection(e: IpcMainEvent, nonce: string) {
91+
this._onBeforeWindowConnection.fire();
92+
8893
const port = this.utilityProcess!.connect();
8994

9095
// Check back if the requesting window meanwhile closed

src/vs/platform/terminal/node/ptyHost.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface IPtyHostConnection {
1414
}
1515

1616
export interface IPtyHostStarter {
17+
onBeforeWindowConnection?: Event<void>;
18+
1719
/**
1820
* Creates a pty host and connects to it.
1921
*

src/vs/platform/terminal/node/ptyHostService.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,24 @@ let lastPtyId = 0;
3636
export class PtyHostService extends Disposable implements IPtyService {
3737
declare readonly _serviceBrand: undefined;
3838

39-
private _connection: IPtyHostConnection;
39+
private __connection?: IPtyHostConnection;
4040
// ProxyChannel is not used here because events get lost when forwarding across multiple proxies
41-
private _proxy: IPtyService;
41+
private __proxy?: IPtyService;
42+
43+
private get _connection(): IPtyHostConnection {
44+
this._ensurePtyHost();
45+
return this.__connection!;
46+
}
47+
private get _proxy(): IPtyService {
48+
this._ensurePtyHost();
49+
return this.__proxy!;
50+
}
51+
52+
private _ensurePtyHost() {
53+
if (!this.__connection) {
54+
[this.__connection, this.__proxy] = this._startPtyHost();
55+
}
56+
}
4257

4358
private readonly _shellEnv: Promise<typeof process.env>;
4459
private readonly _resolveVariablesRequestStore: RequestStore<string[], { workspaceId: string; originalText: string[] }>;
@@ -93,14 +108,13 @@ export class PtyHostService extends Disposable implements IPtyService {
93108
this._resolveVariablesRequestStore = this._register(new RequestStore(undefined, this._logService));
94109
this._resolveVariablesRequestStore.onCreateRequest(this._onPtyHostRequestResolveVariables.fire, this._onPtyHostRequestResolveVariables);
95110

96-
[this._connection, this._proxy] = this._startPtyHost();
97-
98-
this._register(this._configurationService.onDidChangeConfiguration(async e => {
99-
if (e.affectsConfiguration(TerminalSettingId.IgnoreProcessNames)) {
100-
await this._refreshIgnoreProcessNames();
101-
}
102-
}));
103-
this._refreshIgnoreProcessNames();
111+
// Force the pty host to start as the first window is starting if the starter has that
112+
// capability
113+
if (this._ptyHostStarter.onBeforeWindowConnection) {
114+
Event.once(this._ptyHostStarter.onBeforeWindowConnection)(() => this._ensurePtyHost());
115+
} else {
116+
this._ensurePtyHost();
117+
}
104118
}
105119

106120
private get _ignoreProcessNames(): string[] {
@@ -126,6 +140,7 @@ export class PtyHostService extends Disposable implements IPtyService {
126140
}
127141

128142
private _startPtyHost(): [IPtyHostConnection, IPtyService] {
143+
this._logService.info('startPtyHost', new Error().stack);
129144
const connection = this._ptyHostStarter.start(lastPtyId);
130145
const client = connection.client;
131146

@@ -166,6 +181,16 @@ export class PtyHostService extends Disposable implements IPtyService {
166181
this._register(new RemoteLoggerChannelClient(this._loggerService, client.getChannel(TerminalIpcChannels.Logger)));
167182
});
168183

184+
this.__connection = connection;
185+
this.__proxy = proxy;
186+
187+
this._register(this._configurationService.onDidChangeConfiguration(async e => {
188+
if (e.affectsConfiguration(TerminalSettingId.IgnoreProcessNames)) {
189+
await this._refreshIgnoreProcessNames();
190+
}
191+
}));
192+
this._refreshIgnoreProcessNames();
193+
169194
return [connection, proxy];
170195
}
171196

@@ -314,7 +339,7 @@ export class PtyHostService extends Disposable implements IPtyService {
314339
async restartPtyHost(): Promise<void> {
315340
this._disposePtyHost();
316341
this._isResponsive = true;
317-
[this._connection, this._proxy] = this._startPtyHost();
342+
this._startPtyHost();
318343
}
319344

320345
private _disposePtyHost(): void {

0 commit comments

Comments
 (0)