Skip to content

Commit

Permalink
Start pty host when the first window opens
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed May 15, 2023
1 parent 853ba16 commit 25f6ada
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
11 changes: 8 additions & 3 deletions src/vs/platform/terminal/electron-main/electronPtyHostStarter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ import { Client as MessagePortClient } from 'vs/base/parts/ipc/electron-main/ipc
import { IpcMainEvent } from 'electron';
import { validatedIpcMain } from 'vs/base/parts/ipc/electron-main/ipcMain';
import { DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
import { Emitter } from 'vs/base/common/event';

export class ElectronPtyHostStarter implements IPtyHostStarter {

private utilityProcess: UtilityProcess | undefined = undefined;

private readonly _onBeforeWindowConnection = new Emitter<void>();
readonly onBeforeWindowConnection = this._onBeforeWindowConnection.event;

constructor(
private readonly _reconnectConstants: IReconnectConstants,
@IEnvironmentService private readonly _environmentService: INativeEnvironmentService,
@ILifecycleMainService private readonly _lifecycleMainService: ILifecycleMainService,
@ILogService private readonly _logService: ILogService
) {
// Listen for new windows to establish connection directly to pty host
validatedIpcMain.on('vscode:createPtyHostMessageChannel', (e, nonce) => this._onWindowConnection(e, nonce));
}

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

// Listen for new windows to establish connection directly to pty host
validatedIpcMain.on('vscode:createPtyHostMessageChannel', (e, nonce) => this._onWindowConnection(e, nonce));

// TODO: Do we need to listen for window close to close the port?

const store = new DisposableStore();
Expand Down Expand Up @@ -85,6 +88,8 @@ export class ElectronPtyHostStarter implements IPtyHostStarter {
}

private _onWindowConnection(e: IpcMainEvent, nonce: string) {
this._onBeforeWindowConnection.fire();

const port = this.utilityProcess!.connect();

// Check back if the requesting window meanwhile closed
Expand Down
2 changes: 2 additions & 0 deletions src/vs/platform/terminal/node/ptyHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface IPtyHostConnection {
}

export interface IPtyHostStarter {
onBeforeWindowConnection?: Event<void>;

/**
* Creates a pty host and connects to it.
*
Expand Down
44 changes: 27 additions & 17 deletions src/vs/platform/terminal/node/ptyHostService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ export class PtyHostService extends Disposable implements IPtyService {
// ProxyChannel is not used here because events get lost when forwarding across multiple proxies
private __proxy?: IPtyService;


private get _connection(): IPtyHostConnection {
if (!this.__connection) {
[this.__connection, this.__proxy] = this._startPtyHost();
}
return this.__connection;
this._ensurePtyHost();
return this.__connection!;
}
private get _proxy(): IPtyService {
if (!this.__proxy) {
this._ensurePtyHost();
return this.__proxy!;
}

private _ensurePtyHost() {
if (!this.__connection) {
[this.__connection, this.__proxy] = this._startPtyHost();
}
return this.__proxy;
}


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

// [this._connection, this._proxy] = this._startPtyHost();

this._register(this._configurationService.onDidChangeConfiguration(async e => {
if (e.affectsConfiguration(TerminalSettingId.IgnoreProcessNames)) {
await this._refreshIgnoreProcessNames();
}
}));
this._refreshIgnoreProcessNames();
// Force the pty host to start as the first window is starting if the starter has that
// capability
if (this._ptyHostStarter.onBeforeWindowConnection) {
Event.once(this._ptyHostStarter.onBeforeWindowConnection)(() => this._ensurePtyHost());
} else {
this._ensurePtyHost();
}
}

private get _ignoreProcessNames(): string[] {
Expand All @@ -141,6 +140,7 @@ export class PtyHostService extends Disposable implements IPtyService {
}

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

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

this.__connection = connection;
this.__proxy = proxy;

this._register(this._configurationService.onDidChangeConfiguration(async e => {
if (e.affectsConfiguration(TerminalSettingId.IgnoreProcessNames)) {
await this._refreshIgnoreProcessNames();
}
}));
this._refreshIgnoreProcessNames();

return [connection, proxy];
}

Expand Down Expand Up @@ -329,7 +339,7 @@ export class PtyHostService extends Disposable implements IPtyService {
async restartPtyHost(): Promise<void> {
this._disposePtyHost();
this._isResponsive = true;
[this.__connection, this.__proxy] = this._startPtyHost();
this._startPtyHost();
}

private _disposePtyHost(): void {
Expand Down

0 comments on commit 25f6ada

Please sign in to comment.