|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { ChildProcessWithoutNullStreams } from 'child_process'; |
| 5 | +import { StringDecoder } from 'string_decoder'; |
| 6 | +import { CancellationError, CancellationToken, Disposable, l10n } from 'vscode'; |
| 7 | +import { spawnProcess } from './childProcess.apis'; |
| 8 | +import { traceVerbose } from './logging'; |
| 9 | + |
| 10 | +const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000; |
| 11 | +const DEFAULT_MAX_OUTPUT_BYTES = 4 * 1024 * 1024; |
| 12 | +const TERMINATION_GRACE_MS = 1000; |
| 13 | + |
| 14 | +export interface InstallerProcessOptions { |
| 15 | + readonly timeoutMs?: number; |
| 16 | + readonly cancellationToken?: CancellationToken; |
| 17 | + readonly onOutput?: (text: string) => void; |
| 18 | + readonly maxOutputBytes?: number; |
| 19 | +} |
| 20 | + |
| 21 | +export interface InstallerProcessResult { |
| 22 | + readonly stdout: string; |
| 23 | + readonly stderr: string; |
| 24 | +} |
| 25 | + |
| 26 | +export class InstallerProcessError extends Error { |
| 27 | + /** Diagnostic output, kept out of the user-facing message and telemetry. */ |
| 28 | + public stderr = ''; |
| 29 | + /** |
| 30 | + * Describes a process failure without including arguments or potentially sensitive output. |
| 31 | + * Uncertainty also covers descendants or external work that can survive the direct child. |
| 32 | + */ |
| 33 | + constructor( |
| 34 | + readonly kind: 'spawn' | 'exit' | 'timeout' | 'output-limit', |
| 35 | + readonly exitCode?: number | null, |
| 36 | + readonly processMayStillBeRunning = false, |
| 37 | + readonly cause?: unknown, |
| 38 | + ) { |
| 39 | + const messages = { |
| 40 | + spawn: l10n.t('The Python installer process could not be started.'), |
| 41 | + exit: l10n.t('The Python installer process failed.'), |
| 42 | + timeout: l10n.t('The Python installer process timed out.'), |
| 43 | + 'output-limit': l10n.t('The Python installer process exceeded its output limit.'), |
| 44 | + }; |
| 45 | + super(messages[kind]); |
| 46 | + this.name = 'InstallerProcessError'; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class InstallerCancellationError extends CancellationError { |
| 51 | + constructor(readonly processMayStillBeRunning: boolean) { |
| 52 | + super(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function traceLateError(): void { |
| 57 | + traceVerbose('Received an installer process or stream error after completion.'); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Runs an executable directly with piped stdio, bounded output and a finite timeout. |
| 62 | + * @param executable Executable path or command; never interpreted by a shell. |
| 63 | + * @param args Separate, unmodified arguments. |
| 64 | + * @param options Limits, cancellation, and opt-in raw output forwarding. |
| 65 | + * @returns Separate UTF-8 stdout/stderr after the child and its streams close. |
| 66 | + * Cancellation and forced stops wait briefly for close, but cannot guarantee that |
| 67 | + * descendants or external work (such as downloads) have stopped. |
| 68 | + */ |
| 69 | +export async function runInstallerProcess( |
| 70 | + executable: string, |
| 71 | + args: readonly string[], |
| 72 | + options: InstallerProcessOptions = {}, |
| 73 | +): Promise<InstallerProcessResult> { |
| 74 | + const { cancellationToken, onOutput } = options; |
| 75 | + if (cancellationToken?.isCancellationRequested) { |
| 76 | + throw new InstallerCancellationError(false); |
| 77 | + } |
| 78 | + const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS; |
| 79 | + const maxOutputBytes = options.maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES; |
| 80 | + if (!Number.isInteger(timeoutMs) || timeoutMs < 0 || timeoutMs > 2147483647) { |
| 81 | + throw new RangeError(l10n.t('The installer timeout must be a finite, non-negative number of milliseconds.')); |
| 82 | + } |
| 83 | + if (!Number.isSafeInteger(maxOutputBytes) || maxOutputBytes < 0) { |
| 84 | + throw new RangeError(l10n.t('The installer output limit must be a finite, non-negative number of bytes.')); |
| 85 | + } |
| 86 | + |
| 87 | + let child: ChildProcessWithoutNullStreams; |
| 88 | + try { |
| 89 | + child = spawnProcess(executable, [...args], { shell: false, windowsHide: true, stdio: 'pipe' }); |
| 90 | + } catch (error) { |
| 91 | + throw new InstallerProcessError('spawn', undefined, false, error); |
| 92 | + } |
| 93 | + |
| 94 | + return new Promise<InstallerProcessResult>((resolve, reject) => { |
| 95 | + const stdout = { decoder: new StringDecoder('utf8'), chunks: [] as string[] }; |
| 96 | + const stderr = { decoder: new StringDecoder('utf8'), chunks: [] as string[] }; |
| 97 | + let bytes = 0; |
| 98 | + let settled = false; |
| 99 | + let closed = false; |
| 100 | + let exited = false; |
| 101 | + let exitCode: number | null | undefined; |
| 102 | + let failure: InstallerProcessError | InstallerCancellationError | undefined; |
| 103 | + let timeout: ReturnType<typeof setTimeout> | undefined; |
| 104 | + let grace: ReturnType<typeof setTimeout> | undefined; |
| 105 | + let cancellation: Disposable | undefined; |
| 106 | + |
| 107 | + function cleanup(): void { |
| 108 | + clearTimeout(timeout); |
| 109 | + clearTimeout(grace); |
| 110 | + cancellation?.dispose(); |
| 111 | + child.removeListener('exit', onExit); |
| 112 | + child.removeListener('close', onClose); |
| 113 | + child.stdout.removeListener('data', onStdout); |
| 114 | + child.stderr.removeListener('data', onStderr); |
| 115 | + // Keep only stateless error guards: queued errors (notably stdin EPIPE) |
| 116 | + // can arrive after settlement or while the local pipes are destroyed. |
| 117 | + child.on('error', traceLateError); |
| 118 | + child.removeListener('error', onProcessError); |
| 119 | + child.stdin.on('error', traceLateError); |
| 120 | + child.stdin.removeListener('error', onStdinError); |
| 121 | + for (const stream of [child.stdout, child.stderr]) { |
| 122 | + stream.on('error', traceLateError); |
| 123 | + stream.removeListener('error', onStreamError); |
| 124 | + } |
| 125 | + child.stdin.destroy(); |
| 126 | + child.stdout.destroy(); |
| 127 | + child.stderr.destroy(); |
| 128 | + } |
| 129 | + |
| 130 | + function finish(): void { |
| 131 | + if (settled) { |
| 132 | + return; |
| 133 | + } |
| 134 | + settled = true; |
| 135 | + cleanup(); |
| 136 | + if (failure) { |
| 137 | + if (failure instanceof InstallerProcessError) { |
| 138 | + failure.stderr = stderr.chunks.join(''); |
| 139 | + } |
| 140 | + reject(failure); |
| 141 | + } else { |
| 142 | + resolve({ stdout: stdout.chunks.join(''), stderr: stderr.chunks.join('') }); |
| 143 | + } |
| 144 | + stdout.chunks.length = 0; |
| 145 | + stderr.chunks.length = 0; |
| 146 | + } |
| 147 | + |
| 148 | + function stop(error: InstallerProcessError | InstallerCancellationError, signalChild = true): void { |
| 149 | + if (settled || failure) { |
| 150 | + return; |
| 151 | + } |
| 152 | + failure = error; |
| 153 | + clearTimeout(timeout); |
| 154 | + if (closed) { |
| 155 | + return; |
| 156 | + } |
| 157 | + grace = setTimeout(finish, TERMINATION_GRACE_MS); |
| 158 | + if (signalChild && !exited) { |
| 159 | + try { |
| 160 | + if (!child.kill()) { |
| 161 | + traceVerbose('Installer termination signal was not accepted; waiting for close.'); |
| 162 | + } |
| 163 | + } catch (_error) { |
| 164 | + traceVerbose('Installer termination signaling failed; waiting for close.'); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + function append(target: typeof stdout, text: string): void { |
| 170 | + if (!text || failure || settled) { |
| 171 | + return; |
| 172 | + } |
| 173 | + target.chunks.push(text); |
| 174 | + try { |
| 175 | + onOutput?.(text); |
| 176 | + } catch (error) { |
| 177 | + stop(new InstallerProcessError('exit', exitCode, !closed, error)); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + function capture(target: typeof stdout, data: Buffer | string): void { |
| 182 | + if (failure || settled) { |
| 183 | + return; |
| 184 | + } |
| 185 | + const buffer = typeof data === 'string' ? Buffer.from(data, 'utf8') : data; |
| 186 | + bytes += buffer.length; |
| 187 | + if (bytes > maxOutputBytes) { |
| 188 | + stop(new InstallerProcessError('output-limit', exitCode, true)); |
| 189 | + return; |
| 190 | + } |
| 191 | + append(target, target.decoder.write(buffer)); |
| 192 | + } |
| 193 | + |
| 194 | + function onStdout(data: Buffer | string): void { |
| 195 | + capture(stdout, data); |
| 196 | + } |
| 197 | + |
| 198 | + function onStderr(data: Buffer | string): void { |
| 199 | + capture(stderr, data); |
| 200 | + } |
| 201 | + |
| 202 | + function onExit(code: number | null): void { |
| 203 | + exited = true; |
| 204 | + exitCode = code; |
| 205 | + } |
| 206 | + |
| 207 | + function onClose(code: number | null, signal: NodeJS.Signals | null): void { |
| 208 | + closed = true; |
| 209 | + exitCode = code; |
| 210 | + if (!failure && (code !== 0 || signal)) { |
| 211 | + failure = new InstallerProcessError('exit', code, Boolean(signal)); |
| 212 | + } |
| 213 | + if (!failure) { |
| 214 | + append(stdout, stdout.decoder.end()); |
| 215 | + append(stderr, stderr.decoder.end()); |
| 216 | + } |
| 217 | + finish(); |
| 218 | + } |
| 219 | + |
| 220 | + function onProcessError(error: Error): void { |
| 221 | + const mayBeRunning = child.pid !== undefined; |
| 222 | + stop(new InstallerProcessError('spawn', exitCode, mayBeRunning, error), mayBeRunning); |
| 223 | + } |
| 224 | + |
| 225 | + function onStreamError(error: Error): void { |
| 226 | + stop(new InstallerProcessError('exit', exitCode, !closed, error)); |
| 227 | + } |
| 228 | + |
| 229 | + function onStdinError(error: Error): void { |
| 230 | + if ('code' in error && (error.code === 'EPIPE' || error.code === 'ERR_STREAM_DESTROYED')) { |
| 231 | + traceVerbose('Installer closed stdin before consuming all input; waiting for close.'); |
| 232 | + return; |
| 233 | + } |
| 234 | + onStreamError(error); |
| 235 | + } |
| 236 | + |
| 237 | + child.on('error', onProcessError); |
| 238 | + child.on('exit', onExit); |
| 239 | + child.on('close', onClose); |
| 240 | + child.stdin.on('error', onStdinError); |
| 241 | + child.stdout.on('error', onStreamError); |
| 242 | + child.stderr.on('error', onStreamError); |
| 243 | + child.stdout.on('data', onStdout); |
| 244 | + child.stderr.on('data', onStderr); |
| 245 | + timeout = setTimeout(() => stop(new InstallerProcessError('timeout', exitCode, true)), timeoutMs); |
| 246 | + cancellation = cancellationToken?.onCancellationRequested(() => stop(new InstallerCancellationError(true))); |
| 247 | + // Cover cancellation between the initial check and subscribing, including |
| 248 | + // tokens whose registration invokes the callback synchronously. |
| 249 | + if (settled) { |
| 250 | + cancellation?.dispose(); |
| 251 | + return; |
| 252 | + } |
| 253 | + if (cancellationToken?.isCancellationRequested) { |
| 254 | + stop(new InstallerCancellationError(true)); |
| 255 | + } |
| 256 | + if (!failure) { |
| 257 | + try { |
| 258 | + child.stdin.end(); |
| 259 | + } catch (error) { |
| 260 | + onStdinError(error instanceof Error ? error : new Error(l10n.t('Installer input could not be written.'))); |
| 261 | + } |
| 262 | + } |
| 263 | + }); |
| 264 | +} |
0 commit comments