ipc.cp.ts 8.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { ChildProcess, fork, ForkOptions } from 'child_process';
J
Joao Moreno 已提交
7
import { IDisposable, toDisposable, dispose } from 'vs/base/common/lifecycle';
J
Joao Moreno 已提交
8
import { Delayer, createCancelablePromise } from 'vs/base/common/async';
J
Johannes Rieken 已提交
9
import { deepClone, assign } from 'vs/base/common/objects';
J
Joao Moreno 已提交
10
import { Emitter, Event } from 'vs/base/common/event';
R
roblou 已提交
11
import { createQueuedSender } from 'vs/base/node/processes';
A
Alex Dima 已提交
12
import { IChannel, ChannelServer as IPCServer, ChannelClient as IPCClient, IChannelClient } from 'vs/base/parts/ipc/common/ipc';
13
import { isRemoteConsoleLog, log } from 'vs/base/common/console';
14 15
import { CancellationToken } from 'vs/base/common/cancellation';
import * as errors from 'vs/base/common/errors';
A
Alex Dima 已提交
16
import { VSBuffer } from 'vs/base/common/buffer';
E
Erich Gamma 已提交
17

18 19 20 21 22
/**
 * This implementation doesn't perform well since it uses base64 encoding for buffers.
 * We should move all implementations to use named ipc.net, so we stop depending on cp.fork.
 */

J
Joao Moreno 已提交
23 24
export class Server<TContext extends string> extends IPCServer<TContext> {
	constructor(ctx: TContext) {
E
Erich Gamma 已提交
25
		super({
M
Matt Bierner 已提交
26 27 28
			send: r => {
				try {
					if (process.send) {
A
Alex Dima 已提交
29
						process.send((<Buffer>r.buffer).toString('base64'));
M
Matt Bierner 已提交
30 31 32
					}
				} catch (e) { /* not much to do */ }
			},
A
Alex Dima 已提交
33
			onMessage: Event.fromNodeEventEmitter(process, 'message', msg => VSBuffer.wrap(Buffer.from(msg, 'base64')))
J
Joao Moreno 已提交
34
		}, ctx);
E
Erich Gamma 已提交
35 36 37 38 39

		process.once('disconnect', () => this.dispose());
	}
}

J
Joao Moreno 已提交
40
export interface IIPCOptions {
E
Erich Gamma 已提交
41 42 43 44 45 46 47

	/**
	 * A descriptive name for the server this connection is to. Used in logging.
	 */
	serverName: string;

	/**
J
Joao Moreno 已提交
48
	 * Time in millies before killing the ipc process. The next request after killing will start it again.
E
Erich Gamma 已提交
49
	 */
J
Johannes Rieken 已提交
50
	timeout?: number;
E
Erich Gamma 已提交
51 52 53 54

	/**
	 * Arguments to the module to execute.
	 */
J
Johannes Rieken 已提交
55
	args?: string[];
E
Erich Gamma 已提交
56 57

	/**
J
Joao Moreno 已提交
58
	 * Environment key-value pairs to be passed to the process that gets spawned for the ipc.
E
Erich Gamma 已提交
59
	 */
J
Johannes Rieken 已提交
60
	env?: any;
E
Erich Gamma 已提交
61 62 63 64

	/**
	 * Allows to assign a debug port for debugging the application executed.
	 */
J
Johannes Rieken 已提交
65
	debug?: number;
E
Erich Gamma 已提交
66 67 68 69

	/**
	 * Allows to assign a debug port for debugging the application and breaking it on the first line.
	 */
J
Johannes Rieken 已提交
70
	debugBrk?: number;
71

72 73 74
	/**
	 * See https://github.com/Microsoft/vscode/issues/27665
	 * Allows to pass in fresh execArgv to the forked process such that it doesn't inherit them from `process.execArgv`.
B
Benjamin Pasero 已提交
75 76
	 * e.g. Launching the extension host process with `--inspect-brk=xxx` and then forking a process from the extension host
	 * results in the forked process inheriting `--inspect-brk=xxx`.
77 78 79
	 */
	freshExecArgv?: boolean;

80 81 82 83 84
	/**
	 * Enables our createQueuedSender helper for this Client. Uses a queue when the internal Node.js queue is
	 * full of messages - see notes on that method.
	 */
	useQueue?: boolean;
E
Erich Gamma 已提交
85 86
}

J
Joao Moreno 已提交
87
export class Client implements IChannelClient, IDisposable {
E
Erich Gamma 已提交
88 89

	private disposeDelayer: Delayer<void>;
J
Joao Moreno 已提交
90
	private activeRequests = new Set<IDisposable>();
M
Matt Bierner 已提交
91 92
	private child: ChildProcess | null;
	private _client: IPCClient | null;
J
Joao Moreno 已提交
93
	private channels = new Map<string, IChannel>();
E
Erich Gamma 已提交
94

95 96 97
	private _onDidProcessExit = new Emitter<{ code: number, signal: string }>();
	readonly onDidProcessExit = this._onDidProcessExit.event;

J
Joao Moreno 已提交
98 99
	constructor(private modulePath: string, private options: IIPCOptions) {
		const timeout = options && options.timeout ? options.timeout : 60000;
E
Erich Gamma 已提交
100
		this.disposeDelayer = new Delayer<void>(timeout);
101
		this.child = null;
E
Erich Gamma 已提交
102 103 104
		this._client = null;
	}

J
Joao Moreno 已提交
105
	getChannel<T extends IChannel>(channelName: string): T {
106 107 108
		const that = this;

		return {
J
Johannes Rieken 已提交
109
			call<T>(command: string, arg?: any, cancellationToken?: CancellationToken): Promise<T> {
J
Joao Moreno 已提交
110
				return that.requestPromise<T>(channelName, command, arg, cancellationToken);
111 112 113 114 115
			},
			listen(event: string, arg?: any) {
				return that.requestEvent(channelName, event, arg);
			}
		} as T;
E
Erich Gamma 已提交
116 117
	}

J
Johannes Rieken 已提交
118
	protected requestPromise<T>(channelName: string, name: string, arg?: any, cancellationToken = CancellationToken.None): Promise<T> {
J
Joao Moreno 已提交
119
		if (!this.disposeDelayer) {
120
			return Promise.reject(new Error('disposed'));
J
Joao Moreno 已提交
121 122
		}

123
		if (cancellationToken.isCancellationRequested) {
124
			return Promise.reject(errors.canceled());
125 126
		}

E
Erich Gamma 已提交
127 128
		this.disposeDelayer.cancel();

J
Joao Moreno 已提交
129
		const channel = this.getCachedChannel(channelName);
J
Joao Moreno 已提交
130
		const result = createCancelablePromise(token => channel.call<T>(name, arg, token));
131
		const cancellationTokenListener = cancellationToken.onCancellationRequested(() => result.cancel());
E
Erich Gamma 已提交
132

J
Joao Moreno 已提交
133
		const disposable = toDisposable(() => result.cancel());
J
Joao Moreno 已提交
134
		this.activeRequests.add(disposable);
135

J
Joao Moreno 已提交
136
		result.finally(() => {
137 138 139 140 141 142 143 144
			cancellationTokenListener.dispose();
			this.activeRequests.delete(disposable);

			if (this.activeRequests.size === 0) {
				this.disposeDelayer.trigger(() => this.disposeClient());
			}
		});

E
Erich Gamma 已提交
145 146 147
		return result;
	}

148
	protected requestEvent<T>(channelName: string, name: string, arg?: any): Event<T> {
J
Joao Moreno 已提交
149 150 151 152 153 154 155 156 157
		if (!this.disposeDelayer) {
			return Event.None;
		}

		this.disposeDelayer.cancel();

		let listener: IDisposable;
		const emitter = new Emitter<any>({
			onFirstListenerAdd: () => {
J
Joao Moreno 已提交
158
				const channel = this.getCachedChannel(channelName);
J
Joao Moreno 已提交
159 160 161
				const event: Event<T> = channel.listen(name, arg);

				listener = event(emitter.fire, emitter);
J
Joao Moreno 已提交
162
				this.activeRequests.add(listener);
J
Joao Moreno 已提交
163 164
			},
			onLastListenerRemove: () => {
J
Joao Moreno 已提交
165
				this.activeRequests.delete(listener);
166
				listener.dispose();
J
Joao Moreno 已提交
167

R
Rob Lourens 已提交
168
				if (this.activeRequests.size === 0 && this.disposeDelayer) {
J
Joao Moreno 已提交
169 170 171 172 173 174 175 176
					this.disposeDelayer.trigger(() => this.disposeClient());
				}
			}
		});

		return emitter.event;
	}

177
	private get client(): IPCClient {
E
Erich Gamma 已提交
178
		if (!this._client) {
179
			const args = this.options && this.options.args ? this.options.args : [];
180
			const forkOpts: ForkOptions = Object.create(null);
E
Erich Gamma 已提交
181

J
Johannes Rieken 已提交
182
			forkOpts.env = assign(deepClone(process.env), { 'VSCODE_PARENT_PID': String(process.pid) });
E
Erich Gamma 已提交
183

J
Joao Moreno 已提交
184 185 186
			if (this.options && this.options.env) {
				forkOpts.env = assign(forkOpts.env, this.options.env);
			}
E
Erich Gamma 已提交
187

188 189 190 191
			if (this.options && this.options.freshExecArgv) {
				forkOpts.execArgv = [];
			}

J
Joao Moreno 已提交
192
			if (this.options && typeof this.options.debug === 'number') {
B
Benjamin Pasero 已提交
193
				forkOpts.execArgv = ['--nolazy', '--inspect=' + this.options.debug];
J
Joao Moreno 已提交
194
			}
195

J
Joao Moreno 已提交
196
			if (this.options && typeof this.options.debugBrk === 'number') {
B
Benjamin Pasero 已提交
197
				forkOpts.execArgv = ['--nolazy', '--inspect-brk=' + this.options.debugBrk];
198 199 200
			}

			this.child = fork(this.modulePath, args, forkOpts);
J
Joao Moreno 已提交
201

A
Alex Dima 已提交
202
			const onMessageEmitter = new Emitter<VSBuffer>();
J
Joao Moreno 已提交
203
			const onRawMessage = Event.fromNodeEventEmitter(this.child, 'message', msg => msg);
J
Joao Moreno 已提交
204 205

			onRawMessage(msg => {
206 207 208 209

				// Handle remote console logs specially
				if (isRemoteConsoleLog(msg)) {
					log(msg, `IPC Library: ${this.options.serverName}`);
M
Matt Bierner 已提交
210
					return;
J
Joao Moreno 已提交
211 212 213
				}

				// Anything else goes to the outside
A
Alex Dima 已提交
214
				onMessageEmitter.fire(VSBuffer.wrap(Buffer.from(msg, 'base64')));
215
			});
E
Erich Gamma 已提交
216

217
			const sender = this.options.useQueue ? createQueuedSender(this.child) : this.child;
A
Alex Dima 已提交
218
			const send = (r: VSBuffer) => this.child && this.child.connected && sender.send((<Buffer>r.buffer).toString('base64'));
J
Joao Moreno 已提交
219 220 221 222 223
			const onMessage = onMessageEmitter.event;
			const protocol = { send, onMessage };

			this._client = new IPCClient(protocol);

224 225
			const onExit = () => this.disposeClient();
			process.once('exit', onExit);
E
Erich Gamma 已提交
226

227
			this.child.on('error', err => console.warn('IPC "' + this.options.serverName + '" errored with ' + err));
E
Erich Gamma 已提交
228

229 230
			this.child.on('exit', (code: any, signal: any) => {
				process.removeListener('exit', onExit);
E
Erich Gamma 已提交
231

R
Rob Lourens 已提交
232
				this.activeRequests.forEach(r => dispose(r));
J
Joao Moreno 已提交
233
				this.activeRequests.clear();
E
Erich Gamma 已提交
234

J
Joao Moreno 已提交
235
				if (code !== 0 && signal !== 'SIGTERM') {
236
					console.warn('IPC "' + this.options.serverName + '" crashed with exit code ' + code + ' and signal ' + signal);
237
				}
238

M
Matt Bierner 已提交
239 240 241
				if (this.disposeDelayer) {
					this.disposeDelayer.cancel();
				}
J
Joao Moreno 已提交
242
				this.disposeClient();
243
				this._onDidProcessExit.fire({ code, signal });
E
Erich Gamma 已提交
244 245 246 247 248 249
			});
		}

		return this._client;
	}

J
Joao Moreno 已提交
250 251 252 253 254 255 256 257 258 259 260
	private getCachedChannel(name: string): IChannel {
		let channel = this.channels.get(name);

		if (!channel) {
			channel = this.client.getChannel(name);
			this.channels.set(name, channel);
		}

		return channel;
	}

E
Erich Gamma 已提交
261 262
	private disposeClient() {
		if (this._client) {
M
Matt Bierner 已提交
263 264 265 266
			if (this.child) {
				this.child.kill();
				this.child = null;
			}
267
			this._client = null;
J
Joao Moreno 已提交
268
			this.channels.clear();
E
Erich Gamma 已提交
269 270 271 272
		}
	}

	dispose() {
J
Joao Moreno 已提交
273
		this._onDidProcessExit.dispose();
E
Erich Gamma 已提交
274
		this.disposeDelayer.cancel();
M
Matt Bierner 已提交
275
		this.disposeDelayer = null!; // StrictNullOverride: nulling out ok in dispose
E
Erich Gamma 已提交
276
		this.disposeClient();
J
Joao Moreno 已提交
277
		this.activeRequests.clear();
E
Erich Gamma 已提交
278
	}
279
}