-
Notifications
You must be signed in to change notification settings - Fork 30.7k
/
Copy pathextensionHostStarter.ts
417 lines (352 loc) · 13.7 KB
/
extensionHostStarter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { canceled, SerializedError, transformErrorForSerialization } from 'vs/base/common/errors';
import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { IExtensionHostProcessOptions, IExtensionHostStarter } from 'vs/platform/extensions/common/extensionHostStarter';
import { Emitter, Event } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';
import { ILifecycleMainService } from 'vs/platform/lifecycle/electron-main/lifecycleMainService';
import { StopWatch } from 'vs/base/common/stopwatch';
import { ChildProcess, fork } from 'child_process';
import { StringDecoder } from 'string_decoder';
import { Promises, timeout } from 'vs/base/common/async';
import { FileAccess } from 'vs/base/common/network';
import { mixin } from 'vs/base/common/objects';
import * as platform from 'vs/base/common/platform';
import { cwd } from 'vs/base/common/process';
import type { EventEmitter } from 'events';
import * as electron from 'electron';
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
declare namespace UtilityProcessProposedApi {
interface UtilityProcessOptions {
serviceName?: string | undefined;
execArgv?: string[] | undefined;
env?: NodeJS.ProcessEnv | undefined;
}
export class UtilityProcess extends EventEmitter {
readonly pid?: number | undefined;
constructor(modulePath: string, args?: string[] | undefined, options?: UtilityProcessOptions);
postMessage(channel: string, message: any, transfer?: Electron.MessagePortMain[]): void;
kill(signal?: number | string): boolean;
on(event: 'exit', listener: (code: number | undefined) => void): this;
on(event: 'spawn', listener: () => void): this;
}
}
const UtilityProcess = <typeof UtilityProcessProposedApi.UtilityProcess>((electron as any).UtilityProcess);
const canUseUtilityProcess = (typeof UtilityProcess !== 'undefined');
export class ExtensionHostStarter implements IDisposable, IExtensionHostStarter {
_serviceBrand: undefined;
private static _lastId: number = 0;
protected readonly _extHosts: Map<string, ExtensionHostProcess | UtilityExtensionHostProcess>;
private _shutdown = false;
constructor(
@ILogService private readonly _logService: ILogService,
@ILifecycleMainService lifecycleMainService: ILifecycleMainService,
@IWindowsMainService private readonly _windowsMainService: IWindowsMainService,
) {
this._extHosts = new Map<string, ExtensionHostProcess | UtilityExtensionHostProcess>();
// On shutdown: gracefully await extension host shutdowns
lifecycleMainService.onWillShutdown((e) => {
this._shutdown = true;
e.join(this._waitForAllExit(6000));
});
}
dispose(): void {
// Intentionally not killing the extension host processes
}
private _getExtHost(id: string): ExtensionHostProcess | UtilityExtensionHostProcess {
const extHostProcess = this._extHosts.get(id);
if (!extHostProcess) {
throw new Error(`Unknown extension host!`);
}
return extHostProcess;
}
onDynamicStdout(id: string): Event<string> {
return this._getExtHost(id).onStdout;
}
onDynamicStderr(id: string): Event<string> {
return this._getExtHost(id).onStderr;
}
onDynamicMessage(id: string): Event<any> {
return this._getExtHost(id).onMessage;
}
onDynamicError(id: string): Event<{ error: SerializedError }> {
return this._getExtHost(id).onError;
}
onDynamicExit(id: string): Event<{ code: number; signal: string }> {
return this._getExtHost(id).onExit;
}
async canUseUtilityProcess(): Promise<boolean> {
return canUseUtilityProcess;
}
async createExtensionHost(useUtilityProcess: boolean): Promise<{ id: string }> {
if (this._shutdown) {
throw canceled();
}
const id = String(++ExtensionHostStarter._lastId);
let extHost: UtilityExtensionHostProcess | ExtensionHostProcess;
if (useUtilityProcess) {
if (!canUseUtilityProcess) {
throw new Error(`Cannot use UtilityProcess!`);
}
extHost = new UtilityExtensionHostProcess(id, this._logService, this._windowsMainService);
} else {
extHost = new ExtensionHostProcess(id, this._logService);
}
this._extHosts.set(id, extHost);
extHost.onExit(({ pid, code, signal }) => {
this._logService.info(`Extension host with pid ${pid} exited with code: ${code}, signal: ${signal}.`);
setTimeout(() => {
extHost.dispose();
this._extHosts.delete(id);
});
});
return { id };
}
async start(id: string, opts: IExtensionHostProcessOptions): Promise<void> {
if (this._shutdown) {
throw canceled();
}
return this._getExtHost(id).start(opts);
}
async enableInspectPort(id: string): Promise<boolean> {
if (this._shutdown) {
throw canceled();
}
const extHostProcess = this._extHosts.get(id);
if (!extHostProcess) {
return false;
}
return extHostProcess.enableInspectPort();
}
async kill(id: string): Promise<void> {
if (this._shutdown) {
throw canceled();
}
const extHostProcess = this._extHosts.get(id);
if (!extHostProcess) {
// already gone!
return;
}
extHostProcess.kill();
}
async _killAllNow(): Promise<void> {
for (const [, extHost] of this._extHosts) {
extHost.kill();
}
}
async _waitForAllExit(maxWaitTimeMs: number): Promise<void> {
const exitPromises: Promise<void>[] = [];
for (const [, extHost] of this._extHosts) {
exitPromises.push(extHost.waitForExit(maxWaitTimeMs));
}
return Promises.settled(exitPromises).then(() => { });
}
}
class ExtensionHostProcess extends Disposable {
readonly _onStdout = this._register(new Emitter<string>());
readonly onStdout = this._onStdout.event;
readonly _onStderr = this._register(new Emitter<string>());
readonly onStderr = this._onStderr.event;
readonly _onMessage = this._register(new Emitter<any>());
readonly onMessage = this._onMessage.event;
readonly _onError = this._register(new Emitter<{ error: SerializedError }>());
readonly onError = this._onError.event;
readonly _onExit = this._register(new Emitter<{ pid: number; code: number; signal: string }>());
readonly onExit = this._onExit.event;
private _process: ChildProcess | null = null;
private _hasExited: boolean = false;
constructor(
public readonly id: string,
@ILogService private readonly _logService: ILogService,
) {
super();
}
start(opts: IExtensionHostProcessOptions): void {
if (platform.isCI) {
this._logService.info(`Calling fork to start extension host...`);
}
const sw = StopWatch.create(false);
this._process = fork(
FileAccess.asFileUri('bootstrap-fork', require).fsPath,
['--type=extensionHost', '--skipWorkspaceStorageLock'],
mixin({ cwd: cwd() }, opts),
);
const forkTime = sw.elapsed();
const pid = this._process.pid!;
this._logService.info(`Starting extension host with pid ${pid} (fork() took ${forkTime} ms).`);
const stdoutDecoder = new StringDecoder('utf-8');
this._process.stdout?.on('data', (chunk) => {
const strChunk = typeof chunk === 'string' ? chunk : stdoutDecoder.write(chunk);
this._onStdout.fire(strChunk);
});
const stderrDecoder = new StringDecoder('utf-8');
this._process.stderr?.on('data', (chunk) => {
const strChunk = typeof chunk === 'string' ? chunk : stderrDecoder.write(chunk);
this._onStderr.fire(strChunk);
});
this._process.on('message', msg => {
this._onMessage.fire(msg);
});
this._process.on('error', (err) => {
this._onError.fire({ error: transformErrorForSerialization(err) });
});
this._process.on('exit', (code: number, signal: string) => {
this._hasExited = true;
this._onExit.fire({ pid, code, signal });
});
}
enableInspectPort(): boolean {
if (!this._process) {
return false;
}
this._logService.info(`Enabling inspect port on extension host with pid ${this._process.pid}.`);
interface ProcessExt {
_debugProcess?(n: number): any;
}
if (typeof (<ProcessExt>process)._debugProcess === 'function') {
// use (undocumented) _debugProcess feature of node
(<ProcessExt>process)._debugProcess!(this._process.pid!);
return true;
} else if (!platform.isWindows) {
// use KILL USR1 on non-windows platforms (fallback)
this._process.kill('SIGUSR1');
return true;
} else {
// not supported...
return false;
}
}
kill(): void {
if (!this._process) {
return;
}
this._logService.info(`Killing extension host with pid ${this._process.pid}.`);
this._process.kill();
}
async waitForExit(maxWaitTimeMs: number): Promise<void> {
if (!this._process) {
return;
}
const pid = this._process.pid;
this._logService.info(`Waiting for extension host with pid ${pid} to exit.`);
await Promise.race([Event.toPromise(this.onExit), timeout(maxWaitTimeMs)]);
if (!this._hasExited) {
// looks like we timed out
this._logService.info(`Extension host with pid ${pid} did not exit within ${maxWaitTimeMs}ms.`);
this._process.kill();
}
}
}
class UtilityExtensionHostProcess extends Disposable {
readonly onStdout = Event.None;
readonly onStderr = Event.None;
readonly onError = Event.None;
readonly _onMessage = this._register(new Emitter<any>());
readonly onMessage = this._onMessage.event;
readonly _onExit = this._register(new Emitter<{ pid: number; code: number; signal: string }>());
readonly onExit = this._onExit.event;
private _process: UtilityProcessProposedApi.UtilityProcess | null = null;
private _hasExited: boolean = false;
constructor(
public readonly id: string,
@ILogService private readonly _logService: ILogService,
@IWindowsMainService private readonly _windowsMainService: IWindowsMainService,
) {
super();
}
start(opts: IExtensionHostProcessOptions): void {
const codeWindow = this._windowsMainService.getWindowById(opts.responseWindowId);
if (!codeWindow) {
this._logService.info(`UtilityProcess<${this.id}>: Refusing to create new Extension Host UtilityProcess because requesting window cannot be found...`);
return;
}
const responseWindow = codeWindow.win;
if (!responseWindow || responseWindow.isDestroyed() || responseWindow.webContents.isDestroyed()) {
this._logService.info(`UtilityProcess<${this.id}>: Refusing to create new Extension Host UtilityProcess because requesting window cannot be found...`);
return;
}
const serviceName = `extensionHost${this.id}`;
const modulePath = FileAccess.asFileUri('bootstrap-fork.js', require).fsPath;
const args: string[] = ['--type=extensionHost', '--skipWorkspaceStorageLock'];
const execArgv: string[] = opts.execArgv || [];
const env: { [key: string]: any } = { ...opts.env };
// Make sure all values are strings, otherwise the process will not start
for (const key of Object.keys(env)) {
env[key] = String(env[key]);
}
this._logService.info(`UtilityProcess<${this.id}>: Creating new...`);
this._process = new UtilityProcess(modulePath, args, { serviceName, env, execArgv });
this._register(Event.fromNodeEventEmitter<void>(this._process, 'spawn')(() => {
this._logService.info(`UtilityProcess<${this.id}>: received spawn event.`);
}));
this._register(Event.fromNodeEventEmitter<number | undefined>(this._process, 'exit')((code: number | undefined) => {
code = code || 0;
this._logService.info(`UtilityProcess<${this.id}>: received exit event with code ${code}.`);
this._hasExited = true;
this._onExit.fire({ pid: this._process!.pid!, code, signal: '' });
}));
const listener = (event: electron.Event, details: electron.Details) => {
if (details.type !== 'Utility') {
return;
}
// Despite the fact that we pass the argument `seviceName`,
// the details have a field called `name` where this value appears
if (details.name === serviceName) {
this._logService.info(`UtilityProcess<${this.id}>: terminated unexpectedly with code ${details.exitCode}.`);
this._hasExited = true;
this._onExit.fire({ pid: this._process!.pid!, code: details.exitCode, signal: '' });
}
};
electron.app.on('child-process-gone', listener);
this._register(toDisposable(() => {
electron.app.off('child-process-gone', listener);
}));
const { port1, port2 } = new electron.MessageChannelMain();
this._process.postMessage('port', null, [port2]);
responseWindow.webContents.postMessage(opts.responseChannel, opts.responseNonce, [port1]);
}
enableInspectPort(): boolean {
if (!this._process) {
return false;
}
this._logService.info(`UtilityProcess<${this.id}>: Enabling inspect port on extension host with pid ${this._process.pid}.`);
interface ProcessExt {
_debugProcess?(n: number): any;
}
if (typeof (<ProcessExt>process)._debugProcess === 'function') {
// use (undocumented) _debugProcess feature of node
(<ProcessExt>process)._debugProcess!(this._process.pid!);
return true;
} else if (!platform.isWindows) {
// use KILL USR1 on non-windows platforms (fallback)
this._process.kill('SIGUSR1');
return true;
} else {
// not supported...
return false;
}
}
kill(): void {
if (!this._process) {
return;
}
this._logService.info(`UtilityProcess<${this.id}>: Killing extension host with pid ${this._process.pid}.`);
this._process.kill();
}
async waitForExit(maxWaitTimeMs: number): Promise<void> {
if (!this._process) {
return;
}
const pid = this._process.pid;
this._logService.info(`UtilityProcess<${this.id}>: Waiting for extension host with pid ${pid} to exit.`);
await Promise.race([Event.toPromise(this.onExit), timeout(maxWaitTimeMs)]);
if (!this._hasExited) {
// looks like we timed out
this._logService.info(`UtilityProcess<${this.id}>: Extension host with pid ${pid} did not exit within ${maxWaitTimeMs}ms, will kill it now.`);
this._process.kill();
}
}
}