extensionHost.ts 19.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as nls from 'vs/nls';
import { toErrorMessage } from 'vs/base/common/errorMessage';
import { stringify } from 'vs/base/common/marshalling';
import * as objects from 'vs/base/common/objects';
import URI from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
14
import { isWindows, isLinux } from 'vs/base/common/platform';
15 16 17
import { findFreePort } from 'vs/base/node/ports';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
import { ILifecycleService, ShutdownEvent } from 'vs/platform/lifecycle/common/lifecycle';
18
import { IWindowsService, IWindowService } from 'vs/platform/windows/common/windows';
19
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
20
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
21 22
import { ChildProcess, fork } from 'child_process';
import { ipcRenderer as ipc } from 'electron';
23
import product from 'vs/platform/node/product';
24 25
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
26
import { generateRandomPipeName, Protocol } from 'vs/base/parts/ipc/node/ipc.net';
A
Alex Dima 已提交
27
import { createServer, Server, Socket } from 'net';
28
import Event, { Emitter, debounceEvent, mapEvent, any } from 'vs/base/common/event';
J
Joao Moreno 已提交
29
import { fromEventEmitter } from 'vs/base/node/event';
30
import { IInitData, IWorkspaceData } from 'vs/workbench/api/node/extHost.protocol';
B
Benjamin Pasero 已提交
31
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
32
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
33
import { ICrashReporterService } from 'vs/workbench/services/crashReporter/common/crashReporterService';
34
import { IBroadcastService, IBroadcast } from 'vs/platform/broadcast/electron-browser/broadcastService';
B
Benjamin Pasero 已提交
35 36 37
import { isEqual } from 'vs/base/common/paths';
import { EXTENSION_CLOSE_EXTHOST_BROADCAST_CHANNEL, EXTENSION_RELOAD_BROADCAST_CHANNEL, ILogEntry, EXTENSION_ATTACH_BROADCAST_CHANNEL, EXTENSION_LOG_BROADCAST_CHANNEL, EXTENSION_TERMINATE_BROADCAST_CHANNEL } from 'vs/platform/extensions/common/extensionHost';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
38 39

export class ExtensionHostProcessWorker {
40

41 42 43 44 45
	private _onCrashed: Emitter<[number, string]> = new Emitter<[number, string]>();
	public readonly onCrashed: Event<[number, string]> = this._onCrashed.event;

	private readonly _toDispose: IDisposable[];

A
Alex Dima 已提交
46 47 48
	private readonly _isExtensionDevHost: boolean;
	private readonly _isExtensionDevDebug: boolean;
	private readonly _isExtensionDevDebugBrk: boolean;
A
Alex Dima 已提交
49 50 51 52 53
	private readonly _isExtensionDevTestFromCli: boolean;

	// State
	private _lastExtensionHostError: string;
	private _terminating: boolean;
54

55
	// Resources, in order they get acquired/created when .start() is called:
A
Alex Dima 已提交
56 57 58
	private _namedPipeServer: Server;
	private _extensionHostProcess: ChildProcess;
	private _extensionHostConnection: Socket;
59
	private _messageProtocol: TPromise<IMessagePassingProtocol>;
60 61

	constructor(
62
		/* intentionally not injected */private readonly _extensionService: IExtensionService,
A
Alex Dima 已提交
63 64 65 66 67 68 69 70 71 72
		@IWorkspaceContextService private readonly _contextService: IWorkspaceContextService,
		@IMessageService private readonly _messageService: IMessageService,
		@IWindowsService private readonly _windowsService: IWindowsService,
		@IWindowService private readonly _windowService: IWindowService,
		@IBroadcastService private readonly _broadcastService: IBroadcastService,
		@ILifecycleService private readonly _lifecycleService: ILifecycleService,
		@IEnvironmentService private readonly _environmentService: IEnvironmentService,
		@IWorkspaceConfigurationService private readonly _configurationService: IWorkspaceConfigurationService,
		@ITelemetryService private readonly _telemetryService: ITelemetryService,
		@ICrashReporterService private readonly _crashReporterService: ICrashReporterService
73 74
	) {
		// handle extension host lifecycle a bit special when we know we are developing an extension that runs inside
A
Alex Dima 已提交
75 76 77 78
		this._isExtensionDevHost = this._environmentService.isExtensionDevelopment;
		this._isExtensionDevDebug = (typeof this._environmentService.debugExtensionHost.port === 'number');
		this._isExtensionDevDebugBrk = !!this._environmentService.debugExtensionHost.break;
		this._isExtensionDevTestFromCli = this._isExtensionDevHost && !!this._environmentService.extensionTestsPath && !this._environmentService.debugExtensionHost.break;
79

A
Alex Dima 已提交
80 81 82 83 84
		this._lastExtensionHostError = null;
		this._terminating = false;

		this._namedPipeServer = null;
		this._extensionHostProcess = null;
85 86
		this._extensionHostConnection = null;
		this._messageProtocol = null;
A
Alex Dima 已提交
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101
		this._toDispose = [];
		this._toDispose.push(this._onCrashed);
		this._toDispose.push(this._lifecycleService.onWillShutdown((e) => this._onWillShutdown(e)));
		this._toDispose.push(this._lifecycleService.onShutdown(reason => this.terminate()));
		this._toDispose.push(this._broadcastService.onBroadcast(b => this._onBroadcast(b)));

		const globalExitListener = () => this.terminate();
		process.once('exit', globalExitListener);
		this._toDispose.push({
			dispose: () => {
				process.removeListener('exit', globalExitListener);
			}
		});
	}
A
Alex Dima 已提交
102

103 104
	public dispose(): void {
		this.terminate();
105 106
	}

A
Alex Dima 已提交
107
	private _onBroadcast(broadcast: IBroadcast): void {
108 109

		// Close Ext Host Window Request
A
Alex Dima 已提交
110
		if (broadcast.channel === EXTENSION_CLOSE_EXTHOST_BROADCAST_CHANNEL && this._isExtensionDevHost) {
111
			const extensionPaths = broadcast.payload as string[];
A
Alex Dima 已提交
112 113
			if (Array.isArray(extensionPaths) && extensionPaths.some(path => isEqual(this._environmentService.extensionDevelopmentPath, path, !isLinux))) {
				this._windowService.closeWindow();
114 115
			}
		}
116

A
Alex Dima 已提交
117
		if (broadcast.channel === EXTENSION_RELOAD_BROADCAST_CHANNEL && this._isExtensionDevHost) {
118
			const extensionPaths = broadcast.payload as string[];
A
Alex Dima 已提交
119 120
			if (Array.isArray(extensionPaths) && extensionPaths.some(path => isEqual(this._environmentService.extensionDevelopmentPath, path, !isLinux))) {
				this._windowService.reloadWindow();
121 122
			}
		}
123 124
	}

125
	public start(): TPromise<IMessagePassingProtocol> {
126 127 128 129
		if (this._terminating) {
			// .terminate() was called
			return null;
		}
J
Joao Moreno 已提交
130

131 132 133 134 135 136 137 138 139 140 141 142 143
		if (!this._messageProtocol) {
			this._messageProtocol = TPromise.join<any>([this._tryListenOnPipe(), this._tryFindDebugPort()]).then((data: [string, number]) => {
				const pipeName = data[0];
				// The port will be 0 if there's no need to debug or if a free port was not found
				const port = data[1];

				const opts = {
					env: objects.mixin(objects.clone(process.env), {
						AMD_ENTRYPOINT: 'vs/workbench/node/extensionHostProcess',
						PIPE_LOGGING: 'true',
						VERBOSE_LOGGING: true,
						VSCODE_WINDOW_ID: String(this._windowService.getCurrentWindowId()),
						VSCODE_IPC_HOOK_EXTHOST: pipeName,
J
Johannes Rieken 已提交
144
						VSCODE_HANDLES_UNCAUGHT_ERRORS: true,
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
						ELECTRON_NO_ASAR: '1'
					}),
					// We only detach the extension host on windows. Linux and Mac orphan by default
					// and detach under Linux and Mac create another process group.
					// We detach because we have noticed that when the renderer exits, its child processes
					// (i.e. extension host) are taken down in a brutal fashion by the OS
					detached: !!isWindows,
					execArgv: port
						? ['--nolazy', (this._isExtensionDevDebugBrk ? '--inspect-brk=' : '--inspect=') + port]
						: undefined,
					silent: true
				};

				const crashReporterOptions = this._crashReporterService.getChildProcessStartOptions('extensionHost');
				if (crashReporterOptions) {
					opts.env.CRASH_REPORTER_START_OPTIONS = JSON.stringify(crashReporterOptions);
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
				// Run Extension Host as fork of current process
				this._extensionHostProcess = fork(URI.parse(require.toUrl('bootstrap')).fsPath, ['--type=extensionHost'], opts);

				// Catch all output coming from the extension host process
				type Output = { data: string, format: string[] };
				this._extensionHostProcess.stdout.setEncoding('utf8');
				this._extensionHostProcess.stderr.setEncoding('utf8');
				const onStdout = fromEventEmitter<string>(this._extensionHostProcess.stdout, 'data');
				const onStderr = fromEventEmitter<string>(this._extensionHostProcess.stderr, 'data');
				const onOutput = any(
					mapEvent(onStdout, o => ({ data: `%c${o}`, format: [''] })),
					mapEvent(onStderr, o => ({ data: `%c${o}`, format: ['color: red'] }))
				);

				// Debounce all output, so we can render it in the Chrome console as a group
				const onDebouncedOutput = debounceEvent<Output>(onOutput, (r, o) => {
					return r
						? { data: r.data + o.data, format: [...r.format, ...o.format] }
						: { data: o.data, format: o.format };
				}, 100);

				// Print out extension host output
				onDebouncedOutput(data => {
					console.group('Extension Host');
					console.log(data.data, ...data.format);
					console.groupEnd();
				});
190

191 192 193 194
				// Support logging from extension host
				this._extensionHostProcess.on('message', msg => {
					if (msg && (<ILogEntry>msg).type === '__$console') {
						this._logExtensionHostMessage(<ILogEntry>msg);
195
					}
B
Benjamin Pasero 已提交
196
				});
197

198 199
				// Lifecycle
				this._extensionHostProcess.on('error', (err) => this._onExtHostProcessError(err));
200
				this._extensionHostProcess.on('exit', (code: number, signal: string) => this._onExtHostProcessExit(code, signal));
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
				// Notify debugger that we are ready to attach to the process if we run a development extension
				if (this._isExtensionDevHost && port) {
					this._broadcastService.broadcast({
						channel: EXTENSION_ATTACH_BROADCAST_CHANNEL,
						payload: {
							debugId: this._environmentService.debugExtensionHost.debugId,
							port
						}
					});
				}

				// Help in case we fail to start it
				let startupTimeoutHandle: number;
				if (!this._environmentService.isBuilt || this._isExtensionDevHost) {
					startupTimeoutHandle = setTimeout(() => {
						const msg = this._isExtensionDevDebugBrk
							? nls.localize('extensionHostProcess.startupFailDebug', "Extension host did not start in 10 seconds, it might be stopped on the first line and needs a debugger to continue.")
							: nls.localize('extensionHostProcess.startupFail', "Extension host did not start in 10 seconds, that might be a problem.");

						this._messageService.show(Severity.Warning, msg);
					}, 10000);
				}
224

225 226 227 228 229
				// Initialize extension host process with hand shakes
				return this._tryExtHostHandshake().then((protocol) => {
					clearTimeout(startupTimeoutHandle);
					return protocol;
				});
230
			});
231 232 233
		}

		return this._messageProtocol;
234
	}
235

A
Alex Dima 已提交
236
	/**
237
	 * Start a server (`this._namedPipeServer`) that listens on a named pipe and return the named pipe name.
A
Alex Dima 已提交
238
	 */
239 240 241
	private _tryListenOnPipe(): TPromise<string> {
		return new TPromise<string>((resolve, reject) => {
			const pipeName = generateRandomPipeName();
A
Alex Dima 已提交
242 243 244

			this._namedPipeServer = createServer();
			this._namedPipeServer.on('error', reject);
245
			this._namedPipeServer.listen(pipeName, () => {
A
Alex Dima 已提交
246
				this._namedPipeServer.removeListener('error', reject);
247
				resolve(pipeName);
248
			});
249
		});
250 251
	}

A
Alex Dima 已提交
252 253 254
	/**
	 * Find a free port if extension host debugging is enabled.
	 */
A
Alex Dima 已提交
255 256
	private _tryFindDebugPort(): TPromise<number> {
		const extensionHostPort = this._environmentService.debugExtensionHost.port;
257
		if (typeof extensionHostPort !== 'number') {
A
Alex Dima 已提交
258
			return TPromise.wrap<number>(0);
259 260 261 262 263
		}
		return new TPromise<number>((c, e) => {
			findFreePort(extensionHostPort, 10 /* try 10 ports */, 5000 /* try up to 5 seconds */, (port) => {
				if (!port) {
					console.warn('%c[Extension Host] %cCould not find a free port for debugging', 'color: blue', 'color: black');
B
Benjamin Pasero 已提交
264
					return c(void 0);
265 266
				}
				if (port !== extensionHostPort) {
B
Benjamin Pasero 已提交
267
					console.warn(`%c[Extension Host] %cProvided debugging port ${extensionHostPort} is not free, using ${port} instead.`, 'color: blue', 'color: black');
268
				}
A
Alex Dima 已提交
269
				if (this._isExtensionDevDebugBrk) {
B
Benjamin Pasero 已提交
270
					console.warn(`%c[Extension Host] %cSTOPPED on first line for debugging on port ${port}`, 'color: blue', 'color: black');
271
				} else {
B
Benjamin Pasero 已提交
272
					console.info(`%c[Extension Host] %cdebugger listening on port ${port}`, 'color: blue', 'color: black');
273 274 275 276 277 278
				}
				return c(port);
			});
		});
	}

279
	private _tryExtHostHandshake(): TPromise<IMessagePassingProtocol> {
280

281
		return new TPromise<IMessagePassingProtocol>((resolve, reject) => {
A
Alex Dima 已提交
282

A
Alex Dima 已提交
283 284
			// Wait for the extension host to connect to our named pipe
			// and wrap the socket in the message passing protocol
285 286 287 288 289 290
			let handle = setTimeout(() => {
				this._namedPipeServer.close();
				this._namedPipeServer = null;
				reject('timeout');
			}, 60 * 1000);

A
Alex Dima 已提交
291
			this._namedPipeServer.on('connection', socket => {
292
				clearTimeout(handle);
293 294
				this._namedPipeServer.close();
				this._namedPipeServer = null;
A
Alex Dima 已提交
295
				this._extensionHostConnection = socket;
296
				resolve(new Protocol(this._extensionHostConnection));
297
			});
298

299
		}).then((protocol) => {
A
Alex Dima 已提交
300 301 302

			// 1) wait for the incoming `ready` event and send the initialization data.
			// 2) wait for the incoming `initialized` event.
303
			return new TPromise<IMessagePassingProtocol>((resolve, reject) => {
A
Alex Dima 已提交
304

305 306 307 308 309
				let handle = setTimeout(() => {
					reject('timeout');
				}, 60 * 1000);

				const disposable = protocol.onMessage(msg => {
310

311
					if (msg === 'ready') {
312 313
						// 1) Extension Host is ready to receive messages, initialize it
						this._createExtHostInitData().then(data => protocol.send(stringify(data)));
A
Alex Dima 已提交
314 315 316 317
						return;
					}

					if (msg === 'initialized') {
318 319 320
						// 2) Extension Host is initialized

						clearTimeout(handle);
A
Alex Dima 已提交
321 322 323 324 325

						// stop listening for messages here
						disposable.dispose();

						// release this promise
326
						resolve(protocol);
A
Alex Dima 已提交
327
						return;
328
					}
A
Alex Dima 已提交
329 330

					console.error(`received unexpected message during handshake phase from the extension host: `, msg);
331
				});
A
Alex Dima 已提交
332

333
			});
A
Alex Dima 已提交
334

335 336 337
		});
	}

A
Alex Dima 已提交
338
	private _createExtHostInitData(): TPromise<IInitData> {
339
		return TPromise.join<any>([this._telemetryService.getTelemetryInfo(), this._extensionService.getExtensions()]).then(([telemetryInfo, extensionDescriptions]) => {
A
Alex Dima 已提交
340
			const r: IInitData = {
341 342
				parentPid: process.pid,
				environment: {
A
Alex Dima 已提交
343 344 345 346 347 348
					isExtensionDevelopmentDebug: this._isExtensionDevDebug,
					appSettingsHome: this._environmentService.appSettingsHome,
					disableExtensions: this._environmentService.disableExtensions,
					userExtensionsHome: this._environmentService.extensionsPath,
					extensionDevelopmentPath: this._environmentService.extensionDevelopmentPath,
					extensionTestsPath: this._environmentService.extensionTestsPath,
349
					// globally disable proposed api when built and not insiders developing extensions
A
Alex Dima 已提交
350 351
					enableProposedApiForAll: !this._environmentService.isBuilt || (!!this._environmentService.extensionDevelopmentPath && product.nameLong.indexOf('Insiders') >= 0),
					enableProposedApiFor: this._environmentService.args['enable-proposed-api'] || []
352
				},
A
Alex Dima 已提交
353
				workspace: <IWorkspaceData>this._contextService.getWorkspace(),
354
				extensions: extensionDescriptions,
A
Alex Dima 已提交
355
				configuration: this._configurationService.getConfigurationData(),
356
				telemetryInfo
357
			};
358
			return r;
359 360 361
		});
	}

A
Alex Dima 已提交
362
	private _logExtensionHostMessage(logEntry: ILogEntry) {
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
		let args = [];
		try {
			let parsed = JSON.parse(logEntry.arguments);
			args.push(...Object.getOwnPropertyNames(parsed).map(o => parsed[o]));
		} catch (error) {
			args.push(logEntry.arguments);
		}

		// If the first argument is a string, check for % which indicates that the message
		// uses substitution for variables. In this case, we cannot just inject our colored
		// [Extension Host] to the front because it breaks substitution.
		let consoleArgs = [];
		if (typeof args[0] === 'string' && args[0].indexOf('%') >= 0) {
			consoleArgs = [`%c[Extension Host]%c ${args[0]}`, 'color: blue', 'color: black', ...args.slice(1)];
		} else {
			consoleArgs = ['%c[Extension Host]', 'color: blue', ...args];
		}

		// Send to local console unless we run tests from cli
A
Alex Dima 已提交
382
		if (!this._isExtensionDevTestFromCli) {
383 384 385 386
			console[logEntry.severity].apply(console, consoleArgs);
		}

		// Log on main side if running tests from cli
A
Alex Dima 已提交
387 388
		if (this._isExtensionDevTestFromCli) {
			this._windowsService.log(logEntry.severity, ...args);
389 390 391
		}

		// Broadcast to other windows if we are in development mode
A
Alex Dima 已提交
392 393
		else if (!this._environmentService.isBuilt || this._isExtensionDevHost) {
			this._broadcastService.broadcast({
394
				channel: EXTENSION_LOG_BROADCAST_CHANNEL,
395 396
				payload: {
					logEntry,
A
Alex Dima 已提交
397
					debugId: this._environmentService.debugExtensionHost.debugId
398
				}
B
Benjamin Pasero 已提交
399
			});
400 401 402
		}
	}

A
Alex Dima 已提交
403
	private _onExtHostProcessError(err: any): void {
404
		let errorMessage = toErrorMessage(err);
A
Alex Dima 已提交
405
		if (errorMessage === this._lastExtensionHostError) {
406 407 408
			return; // prevent error spam
		}

A
Alex Dima 已提交
409
		this._lastExtensionHostError = errorMessage;
410

A
Alex Dima 已提交
411
		this._messageService.show(Severity.Error, nls.localize('extensionHostProcess.error', "Error from the extension host: {0}", errorMessage));
412 413
	}

A
Alex Dima 已提交
414 415 416
	private _onExtHostProcessExit(code: number, signal: string): void {
		if (this._terminating) {
			// Expected termination path (we asked the process to terminate)
A
Alex Dima 已提交
417 418
			return;
		}
419

A
Alex Dima 已提交
420 421
		// Unexpected termination
		if (!this._isExtensionDevHost) {
422
			this._onCrashed.fire([code, signal]);
A
Alex Dima 已提交
423
		}
424

A
Alex Dima 已提交
425 426 427 428 429 430 431 432
		// Expected development extension termination: When the extension host goes down we also shutdown the window
		else if (!this._isExtensionDevTestFromCli) {
			this._windowService.closeWindow();
		}

		// When CLI testing make sure to exit with proper exit code
		else {
			ipc.send('vscode:exit', code);
433 434 435 436
		}
	}

	public terminate(): void {
437 438 439
		if (this._terminating) {
			return;
		}
A
Alex Dima 已提交
440 441
		this._terminating = true;

442 443
		dispose(this._toDispose);

444 445 446 447 448 449
		if (!this._messageProtocol) {
			// .start() was not called
			return;
		}

		this._messageProtocol.then((protocol) => {
450 451 452

			// Send the extension host a request to terminate itself
			// (graceful termination)
453
			protocol.send({
454 455
				type: '__$terminate'
			});
456 457 458 459 460 461 462 463 464 465

			// Give the extension host 60s, after which we will
			// try to kill the process and release any resources
			setTimeout(() => this._cleanResources(), 60 * 1000);

		}, (err) => {

			// Establishing a protocol with the extension host failed, so
			// try to kill the process and release any resources.
			this._cleanResources();
466
		});
467
	}
468

469 470 471 472 473 474 475 476 477 478 479 480 481
	private _cleanResources(): void {
		if (this._namedPipeServer) {
			this._namedPipeServer.close();
			this._namedPipeServer = null;
		}
		if (this._extensionHostConnection) {
			this._extensionHostConnection.end();
			this._extensionHostConnection = null;
		}
		if (this._extensionHostProcess) {
			this._extensionHostProcess.kill();
			this._extensionHostProcess = null;
		}
482 483 484 485 486 487
	}

	private _onWillShutdown(event: ShutdownEvent): void {

		// If the extension development host was started without debugger attached we need
		// to communicate this back to the main side to terminate the debug session
A
Alex Dima 已提交
488 489
		if (this._isExtensionDevHost && !this._isExtensionDevTestFromCli && !this._isExtensionDevDebug) {
			this._broadcastService.broadcast({
490
				channel: EXTENSION_TERMINATE_BROADCAST_CHANNEL,
491
				payload: {
A
Alex Dima 已提交
492
					debugId: this._environmentService.debugExtensionHost.debugId
493
				}
B
Benjamin Pasero 已提交
494
			});
495 496 497 498

			event.veto(TPromise.timeout(100 /* wait a bit for IPC to get delivered */).then(() => false));
		}
	}
499
}