extensionHost.ts 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*---------------------------------------------------------------------------------------------
 *  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 * as objects from 'vs/base/common/objects';
import URI from 'vs/base/common/uri';
import { TPromise } from 'vs/base/common/winjs.base';
13
import { isWindows, isLinux } from 'vs/base/common/platform';
14 15
import { findFreePort } from 'vs/base/node/ports';
import { ILifecycleService, ShutdownEvent } from 'vs/platform/lifecycle/common/lifecycle';
16
import { IWindowsService, IWindowService } from 'vs/platform/windows/common/windows';
17
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
18
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
19 20
import { ChildProcess, fork } from 'child_process';
import { ipcRenderer as ipc } from 'electron';
21
import product from 'vs/platform/node/product';
22 23
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
24
import { generateRandomPipeName, Protocol } from 'vs/base/parts/ipc/node/ipc.net';
A
Alex Dima 已提交
25
import { createServer, Server, Socket } from 'net';
M
Matt Bierner 已提交
26
import { Event, Emitter, debounceEvent, mapEvent, anyEvent, fromNodeEventEmitter } from 'vs/base/common/event';
S
Sandeep Somavarapu 已提交
27
import { IInitData, IWorkspaceData, IConfigurationInitData } from 'vs/workbench/api/node/extHost.protocol';
28
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
29
import { IWorkspaceConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
30
import { ICrashReporterService } from 'vs/workbench/services/crashReporter/electron-browser/crashReporterService';
31
import { IBroadcastService, IBroadcast } from 'vs/platform/broadcast/electron-browser/broadcastService';
B
Benjamin Pasero 已提交
32
import { isEqual } from 'vs/base/common/paths';
33
import { EXTENSION_CLOSE_EXTHOST_BROADCAST_CHANNEL, EXTENSION_RELOAD_BROADCAST_CHANNEL, EXTENSION_ATTACH_BROADCAST_CHANNEL, EXTENSION_LOG_BROADCAST_CHANNEL, EXTENSION_TERMINATE_BROADCAST_CHANNEL } from 'vs/platform/extensions/common/extensionHost';
B
Benjamin Pasero 已提交
34
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
35
import { IRemoteConsoleLog, log, parse } from 'vs/base/node/console';
S
Sandeep Somavarapu 已提交
36
import { getScopes } from 'vs/platform/configuration/common/configurationRegistry';
S
Sandeep Somavarapu 已提交
37
import { ILogService } from 'vs/platform/log/common/log';
38
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
39 40

export class ExtensionHostProcessWorker {
41

M
Matt Bierner 已提交
42
	private readonly _onCrashed: Emitter<[number, string]> = new Emitter<[number, string]>();
43 44 45 46
	public readonly onCrashed: Event<[number, string]> = this._onCrashed.event;

	private readonly _toDispose: IDisposable[];

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

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

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

	constructor(
64
		/* intentionally not injected */private readonly _extensionService: IExtensionService,
A
Alex Dima 已提交
65
		@IWorkspaceContextService private readonly _contextService: IWorkspaceContextService,
66
		@INotificationService private readonly _notificationService: INotificationService,
A
Alex Dima 已提交
67 68 69 70 71 72 73
		@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,
S
Sandeep Somavarapu 已提交
74 75
		@ICrashReporterService private readonly _crashReporterService: ICrashReporterService,
		@ILogService private readonly _logService: ILogService
76 77
	) {
		// handle extension host lifecycle a bit special when we know we are developing an extension that runs inside
A
Alex Dima 已提交
78 79 80 81
		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;
82

A
Alex Dima 已提交
83 84 85 86 87
		this._lastExtensionHostError = null;
		this._terminating = false;

		this._namedPipeServer = null;
		this._extensionHostProcess = null;
88 89
		this._extensionHostConnection = null;
		this._messageProtocol = null;
A
Alex Dima 已提交
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104
		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 已提交
105

106 107
	public dispose(): void {
		this.terminate();
108 109
	}

A
Alex Dima 已提交
110
	private _onBroadcast(broadcast: IBroadcast): void {
111 112

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

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

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

134
		if (!this._messageProtocol) {
J
Johannes Rieken 已提交
135
			this._messageProtocol = TPromise.join([this._tryListenOnPipe(), this._tryFindDebugPort()]).then(data => {
136
				const pipeName = data[0];
J
Johannes Rieken 已提交
137
				const portData = data[1];
138 139

				const opts = {
J
Johannes Rieken 已提交
140
					env: objects.mixin(objects.deepClone(process.env), {
141 142 143 144
						AMD_ENTRYPOINT: 'vs/workbench/node/extensionHostProcess',
						PIPE_LOGGING: 'true',
						VERBOSE_LOGGING: true,
						VSCODE_IPC_HOOK_EXTHOST: pipeName,
J
Johannes Rieken 已提交
145
						VSCODE_HANDLES_UNCAUGHT_ERRORS: true,
146
						VSCODE_LOG_STACK: !this._isExtensionDevTestFromCli && (this._isExtensionDevHost || !this._environmentService.isBuilt || product.quality !== 'stable' || this._environmentService.verbose)
147 148 149 150 151 152
					}),
					// 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,
J
Johannes Rieken 已提交
153
					execArgv: <string[]>undefined,
154 155 156
					silent: true
				};

J
Johannes Rieken 已提交
157 158 159 160 161 162 163 164 165 166 167 168
				if (portData.actual) {
					opts.execArgv = [
						'--nolazy',
						(this._isExtensionDevDebugBrk ? '--inspect-brk=' : '--inspect=') + portData.actual
					];
					if (!portData.expected) {
						// No one asked for 'inspect' or 'inspect-brk', only us. We add another
						// option such that the extension host can manipulate the execArgv array
						opts.env.VSCODE_PREVENT_FOREIGN_INSPECT = true;
					}
				}

169 170 171
				const crashReporterOptions = this._crashReporterService.getChildProcessStartOptions('extensionHost');
				if (crashReporterOptions) {
					opts.env.CRASH_REPORTER_START_OPTIONS = JSON.stringify(crashReporterOptions);
172 173
				}

174 175 176 177 178 179 180
				// 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');
J
Joao Moreno 已提交
181 182
				const onStdout = fromNodeEventEmitter<string>(this._extensionHostProcess.stdout, 'data');
				const onStderr = fromNodeEventEmitter<string>(this._extensionHostProcess.stderr, 'data');
183
				const onOutput = anyEvent(
184 185 186 187 188 189 190 191 192 193 194 195 196
					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 => {
197 198 199 200 201 202 203 204
					const inspectorUrlIndex = !this._environmentService.isBuilt && data.data && data.data.indexOf('chrome-devtools://');
					if (inspectorUrlIndex >= 0) {
						console.log(`%c[Extension Host] %cdebugger inspector at ${data.data.substr(inspectorUrlIndex)}`, 'color: blue', 'color: black');
					} else {
						console.group('Extension Host');
						console.log(data.data, ...data.format);
						console.groupEnd();
					}
205
				});
206

207 208
				// Support logging from extension host
				this._extensionHostProcess.on('message', msg => {
209 210
					if (msg && (<IRemoteConsoleLog>msg).type === '__$console') {
						this._logExtensionHostMessage(<IRemoteConsoleLog>msg);
211
					}
B
Benjamin Pasero 已提交
212
				});
213

214 215
				// Lifecycle
				this._extensionHostProcess.on('error', (err) => this._onExtHostProcessError(err));
216
				this._extensionHostProcess.on('exit', (code: number, signal: string) => this._onExtHostProcessExit(code, signal));
217

218
				// Notify debugger that we are ready to attach to the process if we run a development extension
J
Johannes Rieken 已提交
219
				if (this._isExtensionDevHost && portData.actual) {
220 221 222 223
					this._broadcastService.broadcast({
						channel: EXTENSION_ATTACH_BROADCAST_CHANNEL,
						payload: {
							debugId: this._environmentService.debugExtensionHost.debugId,
J
Johannes Rieken 已提交
224
							port: portData.actual
225 226 227
						}
					});
				}
J
Johannes Rieken 已提交
228
				this._inspectPort = portData.actual;
229 230 231 232 233 234 235 236 237

				// 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.");

238
						this._notificationService.prompt(Severity.Warning, msg, [nls.localize('reloadWindow', "Reload Window")]).then(choice => {
239 240 241 242
							if (choice === 0) {
								this._windowService.reloadWindow();
							}
						});
243 244
					}, 10000);
				}
245

246 247 248 249 250
				// Initialize extension host process with hand shakes
				return this._tryExtHostHandshake().then((protocol) => {
					clearTimeout(startupTimeoutHandle);
					return protocol;
				});
251
			});
252 253 254
		}

		return this._messageProtocol;
255
	}
256

A
Alex Dima 已提交
257
	/**
258
	 * Start a server (`this._namedPipeServer`) that listens on a named pipe and return the named pipe name.
A
Alex Dima 已提交
259
	 */
260 261 262
	private _tryListenOnPipe(): TPromise<string> {
		return new TPromise<string>((resolve, reject) => {
			const pipeName = generateRandomPipeName();
A
Alex Dima 已提交
263 264 265

			this._namedPipeServer = createServer();
			this._namedPipeServer.on('error', reject);
266
			this._namedPipeServer.listen(pipeName, () => {
A
Alex Dima 已提交
267
				this._namedPipeServer.removeListener('error', reject);
268
				resolve(pipeName);
269
			});
270
		});
271 272
	}

A
Alex Dima 已提交
273 274 275
	/**
	 * Find a free port if extension host debugging is enabled.
	 */
J
Johannes Rieken 已提交
276 277 278 279 280
	private _tryFindDebugPort(): TPromise<{ expected: number; actual: number }> {
		let expected: number;
		let startPort = 9333;
		if (typeof this._environmentService.debugExtensionHost.port === 'number') {
			startPort = expected = this._environmentService.debugExtensionHost.port;
J
Johannes Rieken 已提交
281 282
		} else {
			return TPromise.as({ expected: undefined, actual: 0 });
283
		}
J
Johannes Rieken 已提交
284 285
		return new TPromise((c, e) => {
			return findFreePort(startPort, 10 /* try 10 ports */, 5000 /* try up to 5 seconds */).then(port => {
286 287 288
				if (!port) {
					console.warn('%c[Extension Host] %cCould not find a free port for debugging', 'color: blue', 'color: black');
				} else {
J
Johannes Rieken 已提交
289 290 291 292 293 294 295 296
					if (expected && port !== expected) {
						console.warn(`%c[Extension Host] %cProvided debugging port ${expected} is not free, using ${port} instead.`, 'color: blue', 'color: black');
					}
					if (this._isExtensionDevDebugBrk) {
						console.warn(`%c[Extension Host] %cSTOPPED on first line for debugging on port ${port}`, 'color: blue', 'color: black');
					} else {
						console.info(`%c[Extension Host] %cdebugger listening on port ${port}`, 'color: blue', 'color: black');
					}
297
				}
J
Johannes Rieken 已提交
298
				return c({ expected, actual: port });
299 300 301 302
			});
		});
	}

303
	private _tryExtHostHandshake(): TPromise<IMessagePassingProtocol> {
304

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

A
Alex Dima 已提交
307 308
			// Wait for the extension host to connect to our named pipe
			// and wrap the socket in the message passing protocol
309 310 311 312 313 314
			let handle = setTimeout(() => {
				this._namedPipeServer.close();
				this._namedPipeServer = null;
				reject('timeout');
			}, 60 * 1000);

A
Alex Dima 已提交
315
			this._namedPipeServer.on('connection', socket => {
316
				clearTimeout(handle);
317 318
				this._namedPipeServer.close();
				this._namedPipeServer = null;
A
Alex Dima 已提交
319
				this._extensionHostConnection = socket;
320
				resolve(new Protocol(this._extensionHostConnection));
321
			});
322

323
		}).then((protocol) => {
A
Alex Dima 已提交
324 325 326

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

329 330 331 332 333
				let handle = setTimeout(() => {
					reject('timeout');
				}, 60 * 1000);

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

335
					if (msg === 'ready') {
336
						// 1) Extension Host is ready to receive messages, initialize it
337
						this._createExtHostInitData().then(data => protocol.send(JSON.stringify(data)));
A
Alex Dima 已提交
338 339 340 341
						return;
					}

					if (msg === 'initialized') {
342 343 344
						// 2) Extension Host is initialized

						clearTimeout(handle);
A
Alex Dima 已提交
345 346 347 348 349

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

						// release this promise
350
						resolve(protocol);
A
Alex Dima 已提交
351
						return;
352
					}
A
Alex Dima 已提交
353 354

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

357
			});
A
Alex Dima 已提交
358

359 360 361
		});
	}

A
Alex Dima 已提交
362
	private _createExtHostInitData(): TPromise<IInitData> {
363
		return TPromise.join<any>([this._telemetryService.getTelemetryInfo(), this._extensionService.getExtensions()]).then(([telemetryInfo, extensionDescriptions]) => {
S
Sandeep Somavarapu 已提交
364
			const configurationData: IConfigurationInitData = { ...this._configurationService.getConfigurationData(), configurationScopes: {} };
A
Alex Dima 已提交
365
			const r: IInitData = {
366 367
				parentPid: process.pid,
				environment: {
A
Alex Dima 已提交
368
					isExtensionDevelopmentDebug: this._isExtensionDevDebug,
J
Johannes Rieken 已提交
369
					appRoot: this._environmentService.appRoot,
A
Alex Dima 已提交
370 371 372 373 374
					appSettingsHome: this._environmentService.appSettingsHome,
					disableExtensions: this._environmentService.disableExtensions,
					userExtensionsHome: this._environmentService.extensionsPath,
					extensionDevelopmentPath: this._environmentService.extensionDevelopmentPath,
					extensionTestsPath: this._environmentService.extensionTestsPath,
375
					// globally disable proposed api when built and not insiders developing extensions
A
Alex Dima 已提交
376 377
					enableProposedApiForAll: !this._environmentService.isBuilt || (!!this._environmentService.extensionDevelopmentPath && product.nameLong.indexOf('Insiders') >= 0),
					enableProposedApiFor: this._environmentService.args['enable-proposed-api'] || []
378
				},
379
				workspace: this._contextService.getWorkbenchState() === WorkbenchState.EMPTY ? null : <IWorkspaceData>this._contextService.getWorkspace(),
380
				extensions: extensionDescriptions,
S
Sandeep Somavarapu 已提交
381
				// Send configurations scopes only in development mode.
S
Sandeep Somavarapu 已提交
382
				configuration: !this._environmentService.isBuilt || this._environmentService.isExtensionDevelopment ? { ...configurationData, configurationScopes: getScopes() } : configurationData,
J
Joao Moreno 已提交
383
				telemetryInfo,
S
Sandeep Somavarapu 已提交
384
				windowId: this._windowService.getCurrentWindowId(),
A
Alex Dima 已提交
385 386
				logLevel: this._logService.getLevel(),
				logsPath: this._environmentService.logsPath
387
			};
388
			return r;
389 390 391
		});
	}

392
	private _logExtensionHostMessage(entry: IRemoteConsoleLog) {
393 394

		// Send to local console unless we run tests from cli
A
Alex Dima 已提交
395
		if (!this._isExtensionDevTestFromCli) {
396
			log(entry, 'Extension Host');
397 398 399
		}

		// Log on main side if running tests from cli
A
Alex Dima 已提交
400
		if (this._isExtensionDevTestFromCli) {
401
			this._windowsService.log(entry.severity, ...parse(entry).args);
402 403 404
		}

		// Broadcast to other windows if we are in development mode
A
Alex Dima 已提交
405 406
		else if (!this._environmentService.isBuilt || this._isExtensionDevHost) {
			this._broadcastService.broadcast({
407
				channel: EXTENSION_LOG_BROADCAST_CHANNEL,
408
				payload: {
409
					logEntry: entry,
A
Alex Dima 已提交
410
					debugId: this._environmentService.debugExtensionHost.debugId
411
				}
B
Benjamin Pasero 已提交
412
			});
413 414 415
		}
	}

A
Alex Dima 已提交
416
	private _onExtHostProcessError(err: any): void {
417
		let errorMessage = toErrorMessage(err);
A
Alex Dima 已提交
418
		if (errorMessage === this._lastExtensionHostError) {
419 420 421
			return; // prevent error spam
		}

A
Alex Dima 已提交
422
		this._lastExtensionHostError = errorMessage;
423

424
		this._notificationService.error(nls.localize('extensionHostProcess.error', "Error from the extension host: {0}", errorMessage));
425 426
	}

A
Alex Dima 已提交
427 428 429
	private _onExtHostProcessExit(code: number, signal: string): void {
		if (this._terminating) {
			// Expected termination path (we asked the process to terminate)
A
Alex Dima 已提交
430 431
			return;
		}
432

A
Alex Dima 已提交
433 434
		// Unexpected termination
		if (!this._isExtensionDevHost) {
435
			this._onCrashed.fire([code, signal]);
A
Alex Dima 已提交
436
		}
437

A
Alex Dima 已提交
438 439 440 441 442 443 444 445
		// 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);
446 447 448
		}
	}

449 450 451 452
	public getInspectPort(): number {
		return this._inspectPort;
	}

453
	public terminate(): void {
454 455 456
		if (this._terminating) {
			return;
		}
A
Alex Dima 已提交
457 458
		this._terminating = true;

459 460
		dispose(this._toDispose);

461 462 463 464 465 466
		if (!this._messageProtocol) {
			// .start() was not called
			return;
		}

		this._messageProtocol.then((protocol) => {
467 468 469

			// Send the extension host a request to terminate itself
			// (graceful termination)
470
			protocol.send({
471 472
				type: '__$terminate'
			});
473 474 475 476 477 478 479 480 481 482

			// 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();
483
		});
484
	}
485

486 487 488 489 490 491 492 493 494 495 496 497 498
	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;
		}
499 500 501 502 503 504
	}

	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 已提交
505 506
		if (this._isExtensionDevHost && !this._isExtensionDevTestFromCli && !this._isExtensionDevDebug) {
			this._broadcastService.broadcast({
507
				channel: EXTENSION_TERMINATE_BROADCAST_CHANNEL,
508
				payload: {
A
Alex Dima 已提交
509
					debugId: this._environmentService.debugExtensionHost.debugId
510
				}
B
Benjamin Pasero 已提交
511
			});
512 513 514 515

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