Skip to content

Commit 3876a21

Browse files
committed
Tidy up window<->pty host connection
1 parent 3b26dc1 commit 3876a21

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import { IReconnectConstants } from 'vs/platform/terminal/common/terminal';
1212
import { IPtyHostConnection, IPtyHostStarter } from 'vs/platform/terminal/node/ptyHost';
1313
import { UtilityProcess } from 'vs/platform/utilityProcess/electron-main/utilityProcess';
1414
import { Client as MessagePortClient } from 'vs/base/parts/ipc/electron-main/ipc.mp';
15-
import { MessageChannelMain, MessagePortMain } from 'electron';
15+
import { IpcMainEvent } from 'electron';
1616
import { assertIsDefined } from 'vs/base/common/types';
17+
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
1718

1819
export class ElectronPtyHostStarter implements IPtyHostStarter {
1920

@@ -27,7 +28,7 @@ export class ElectronPtyHostStarter implements IPtyHostStarter {
2728
) {
2829
}
2930

30-
async start(lastPtyId: number): Promise<IPtyHostConnection> {
31+
start(lastPtyId: number): IPtyHostConnection {
3132
this.utilityProcess = new UtilityProcess(this._logService, NullTelemetryService, this._lifecycleMainService);
3233

3334
const inspectParams = parsePtyHostDebugPort(this._environmentService.args, this._environmentService.isBuilt);
@@ -51,6 +52,9 @@ export class ElectronPtyHostStarter implements IPtyHostStarter {
5152
const port = this.utilityProcess.connect();
5253
const client = new MessagePortClient(port, 'ptyHost');
5354

55+
// Listen for new windows to establish connection directly to pty host
56+
validatedIpcMain.on('vscode:createPtyHostMessageChannel', (e, nonce) => this._onWindowConnection(e, nonce));
57+
5458
return {
5559
client,
5660
port,
@@ -71,4 +75,20 @@ export class ElectronPtyHostStarter implements IPtyHostStarter {
7175
VSCODE_RECONNECT_SCROLLBACK: this._reconnectConstants.scrollback
7276
};
7377
}
78+
79+
private _onWindowConnection(e: IpcMainEvent, nonce: string) {
80+
const port = this.utilityProcess!.connect();
81+
82+
// Check back if the requesting window meanwhile closed
83+
// Since shared process is delayed on startup there is
84+
// a chance that the window close before the shared process
85+
// was ready for a connection.
86+
87+
if (e.sender.isDestroyed()) {
88+
port.close();
89+
return;
90+
}
91+
92+
e.sender.postMessage('vscode:createPtyHostMessageChannelResult', nonce, [port]);
93+
}
7494
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export interface IPtyHostStarter {
2222
* @param lastPtyId Tracks the last terminal ID from the pty host so we can give it to the new
2323
* pty host if it's restarted and avoid ID conflicts.
2424
*/
25-
start(lastPtyId: number): IPtyHostConnection | Promise<IPtyHostConnection>;
25+
start(lastPtyId: number): IPtyHostConnection;
2626
}

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

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

6-
import { IpcMainEvent, MessagePortMain } from 'electron';
76
import { Emitter, Event } from 'vs/base/common/event';
87
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
98
import { IProcessEnvironment, OperatingSystem, isWindows } from 'vs/base/common/platform';
109
import { ProxyChannel } from 'vs/base/parts/ipc/common/ipc';
11-
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
1210
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
1311
import { ILogService, ILoggerService } from 'vs/platform/log/common/log';
1412
import { RemoteLoggerChannelClient } from 'vs/platform/log/common/logIpc';
@@ -96,38 +94,14 @@ export class PtyHostService extends Disposable implements IPtyService {
9694
this._resolveVariablesRequestStore = this._register(new RequestStore(undefined, this._logService));
9795
this._resolveVariablesRequestStore.onCreateRequest(this._onPtyHostRequestResolveVariables.fire, this._onPtyHostRequestResolveVariables);
9896

99-
validatedIpcMain.on('vscode:createPtyHostMessageChannel', (e, nonce) => this._onWindowConnection(e, nonce));
97+
[this._connection, this._proxy] = this._startPtyHost();
10098

101-
// TODO: Start on demand on first window connection (see SharedProcess.onWindowConnection
102-
// TODO: Make direct message port connection to each window when requested
103-
104-
this._startPtyHost().then(value => {
105-
this._connection = value[0];
106-
this._proxy = value[1];
107-
108-
this._register(this._configurationService.onDidChangeConfiguration(async e => {
109-
if (e.affectsConfiguration(TerminalSettingId.IgnoreProcessNames)) {
110-
await this._refreshIgnoreProcessNames();
111-
}
112-
}));
113-
this._refreshIgnoreProcessNames();
114-
});
115-
}
116-
117-
private _onWindowConnection(e: IpcMainEvent, nonce: string) {
118-
const port = this._connection.connect!() as MessagePortMain;
119-
120-
121-
// Check back if the requesting window meanwhile closed
122-
// Since shared process is delayed on startup there is
123-
// a chance that the window close before the shared process
124-
// was ready for a connection.
125-
126-
if (e.sender.isDestroyed()) {
127-
return port.close();
128-
}
129-
130-
e.sender.postMessage('vscode:createPtyHostMessageChannelResult', nonce, [port]);
99+
this._register(this._configurationService.onDidChangeConfiguration(async e => {
100+
if (e.affectsConfiguration(TerminalSettingId.IgnoreProcessNames)) {
101+
await this._refreshIgnoreProcessNames();
102+
}
103+
}));
104+
this._refreshIgnoreProcessNames();
131105
}
132106

133107
private get _ignoreProcessNames(): string[] {
@@ -152,8 +126,8 @@ export class PtyHostService extends Disposable implements IPtyService {
152126
}
153127
}
154128

155-
private async _startPtyHost(): Promise<[IPtyHostConnection, IPtyService]> {
156-
const connection = await this._ptyHostStarter.start(lastPtyId);
129+
private _startPtyHost(): [IPtyHostConnection, IPtyService] {
130+
const connection = this._ptyHostStarter.start(lastPtyId);
157131
const client = connection.client;
158132

159133
this._onPtyHostStart.fire();

0 commit comments

Comments
 (0)