Skip to content

Commit 89b9cd7

Browse files
committed
shared process - add separate whenIpcReady
1 parent 2c7809e commit 89b9cd7

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

src/vs/code/electron-browser/sharedProcess/sharedProcessMain.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async function main(server: Server, initData: ISharedProcessInitData, configurat
100100

101101
const onExit = () => disposables.dispose();
102102
process.once('exit', onExit);
103-
ipcRenderer.once('handshake:goodbye', onExit);
103+
ipcRenderer.once('handshake:mainprocess-goodbye', onExit);
104104

105105
disposables.add(server);
106106

@@ -271,13 +271,19 @@ function setupIPC(hook: string): Promise<Server> {
271271
}
272272

273273
async function handshake(configuration: ISharedProcessConfiguration): Promise<void> {
274+
275+
// shared process -> main: give me payload for IPC connection
276+
// main -> shared process: payload for IPC connection
274277
const data = await new Promise<ISharedProcessInitData>(c => {
275-
ipcRenderer.once('handshake:hey there', (_: any, r: ISharedProcessInitData) => c(r));
276-
ipcRenderer.send('handshake:hello');
278+
ipcRenderer.once('handshake:main-payload', (_: any, r: ISharedProcessInitData) => c(r));
279+
ipcRenderer.send('handshake:sharedprocess-hello');
277280
});
278281

282+
// shared process => main: IPC connection established
279283
const server = await setupIPC(data.sharedIPCHandle);
284+
ipcRenderer.send('handshake:sharedprocess-ipc-ready');
280285

286+
// shared process => main: initialization done
281287
await main(server, data, configuration);
282-
ipcRenderer.send('handshake:im ready');
288+
ipcRenderer.send('handshake:sharedprocess-init-ready');
283289
}

src/vs/code/electron-main/app.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ export class CodeApplication extends Disposable {
364364

365365
// Spawn shared process after the first window has opened and 3s have passed
366366
const sharedProcess = this.instantiationService.createInstance(SharedProcess, machineId, this.userEnv);
367-
const sharedProcessClient = sharedProcess.whenReady().then(() => connect(this.environmentService.sharedIPCHandle, 'main'));
367+
const sharedProcessClient = sharedProcess.whenIpcReady().then(() => connect(this.environmentService.sharedIPCHandle, 'main'));
368+
const sharedProcessReady = sharedProcess.whenReady().then(() => sharedProcessClient);
368369
this.lifecycleMainService.when(LifecycleMainPhase.AfterWindowOpen).then(() => {
369370
this._register(new RunOnceScheduler(async () => {
370371
const userEnv = await getShellEnvironment(this.logService, this.environmentService);
@@ -374,7 +375,7 @@ export class CodeApplication extends Disposable {
374375
});
375376

376377
// Services
377-
const appInstantiationService = await this.createServices(machineId, trueMachineId, sharedProcess, sharedProcessClient);
378+
const appInstantiationService = await this.createServices(machineId, trueMachineId, sharedProcess, sharedProcessReady);
378379

379380
// Create driver
380381
if (this.environmentService.driverHandle) {
@@ -388,7 +389,7 @@ export class CodeApplication extends Disposable {
388389
this._register(new ProxyAuthHandler());
389390

390391
// Open Windows
391-
const windows = appInstantiationService.invokeFunction(accessor => this.openFirstWindow(accessor, electronIpcServer, sharedProcessClient));
392+
const windows = appInstantiationService.invokeFunction(accessor => this.openFirstWindow(accessor, electronIpcServer, sharedProcessReady));
392393

393394
// Post Open Windows Tasks
394395
this.afterWindowOpen();

src/vs/code/electron-main/sharedProcess.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ export class SharedProcess implements ISharedProcess {
2121

2222
private window: BrowserWindow | null = null;
2323

24+
private readonly _whenReady: Promise<void>;
25+
2426
constructor(
2527
private readonly machineId: string,
2628
private userEnv: NodeJS.ProcessEnv,
2729
@IEnvironmentService private readonly environmentService: IEnvironmentService,
2830
@ILifecycleMainService private readonly lifecycleMainService: ILifecycleMainService,
2931
@ILogService private readonly logService: ILogService,
3032
@IThemeMainService private readonly themeMainService: IThemeMainService
31-
) { }
33+
) {
34+
this._whenReady = new Promise<void>(c => ipcMain.once('handshake:sharedprocess-init-ready', () => c(undefined)));
35+
}
3236

3337
@memoize
34-
private get _whenReady(): Promise<void> {
38+
private get _whenIpcReady(): Promise<void> {
3539
this.window = new BrowserWindow({
3640
show: false,
3741
backgroundColor: this.themeMainService.getBackgroundColor(),
@@ -98,16 +102,16 @@ export class SharedProcess implements ISharedProcess {
98102
});
99103

100104
return new Promise<void>(c => {
101-
const onHello = Event.once(Event.fromNodeEventEmitter(ipcMain, 'handshake:hello', ({ sender }: { sender: WebContents }) => sender));
105+
const onHello = Event.once(Event.fromNodeEventEmitter(ipcMain, 'handshake:sharedprocess-hello', ({ sender }: { sender: WebContents }) => sender));
102106
disposables.add(onHello(sender => {
103-
sender.send('handshake:hey there', {
107+
sender.send('handshake:main-payload', {
104108
sharedIPCHandle: this.environmentService.sharedIPCHandle,
105109
args: this.environmentService.args,
106110
logLevel: this.logService.getLevel()
107111
});
108112

109-
disposables.add(toDisposable(() => sender.send('handshake:goodbye')));
110-
ipcMain.once('handshake:im ready', () => c(undefined));
113+
disposables.add(toDisposable(() => sender.send('handshake:mainprocess-goodbye')));
114+
ipcMain.once('handshake:sharedprocess-ipc-ready', () => c(undefined));
111115
}));
112116
});
113117
}
@@ -122,6 +126,11 @@ export class SharedProcess implements ISharedProcess {
122126
await this._whenReady;
123127
}
124128

129+
async whenIpcReady(): Promise<void> {
130+
await this.barrier.wait();
131+
await this._whenIpcReady;
132+
}
133+
125134
toggle(): void {
126135
if (!this.window || this.window.isVisible()) {
127136
this.hide();

0 commit comments

Comments
 (0)