Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 8a7048e

Browse files
committed
eliminate fixed size buffer; fixes microsoft/vscode#45320
1 parent 1a4d387 commit 8a7048e

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/node/extension/childProcesses.ts

+32-23
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
'use strict';
77

88
import * as nls from 'vscode-nls';
9-
import { exec } from 'child_process';
9+
import { spawn } from 'child_process';
1010
import * as vscode from 'vscode';
11+
import { join } from 'path';
1112

1213
const localize = nls.loadMessageBundle();
1314

@@ -177,37 +178,45 @@ function findChildProcesses(rootPid: number, cb: (pid: number, cmd: string) => v
177178
}
178179
}
179180

181+
// returns a function that aggregates chunks of data until one or more complete lines are received and passes them to a callback.
182+
function lines(callback: (a: string) => void) {
183+
let unfinished = ''; // unfinished last line of chunk
184+
return (data: string | Buffer) => {
185+
const lines = data.toString().split(/\r?\n/);
186+
const finishedLines = lines.slice(0, lines.length - 1);
187+
finishedLines[0] = unfinished + finishedLines[0]; // complete previous unfinished line
188+
unfinished = lines[lines.length - 1]; // remember unfinished last line of this chunk for next round
189+
for (const s of finishedLines) {
190+
callback(s);
191+
}
192+
}
193+
}
194+
180195
if (process.platform === 'win32') {
181196

182-
const CMD = 'wmic process get CommandLine,ParentProcessId,ProcessId';
183197
const CMD_PAT = /^(.+)\s+([0-9]+)\s+([0-9]+)$/;
184198

185-
exec(CMD, { maxBuffer: 1000 * 1024 }, (err, stdout, stderr) => {
186-
if (!err && !stderr) {
187-
const lines = stdout.split('\r\n');
188-
for (let line of lines) {
189-
let matches = CMD_PAT.exec(line.trim());
190-
if (matches && matches.length === 4) {
191-
oneProcess(parseInt(matches[3]), parseInt(matches[2]), matches[1].trim());
192-
}
193-
}
199+
const wmic = join(process.env['WINDIR'] || 'C:\\Windows', 'System32', 'wbem', 'WMIC.exe');
200+
var proc = spawn(wmic, [ 'process', 'get', 'CommandLine,ParentProcessId,ProcessId' ]);
201+
proc.stdout.setEncoding('utf8');
202+
proc.stdout.on('data', lines(line => {
203+
let matches = CMD_PAT.exec(line.trim());
204+
if (matches && matches.length === 4) {
205+
oneProcess(parseInt(matches[3]), parseInt(matches[2]), matches[1].trim());
194206
}
195-
});
207+
}));
208+
196209
} else { // OS X & Linux
197210

198-
const CMD = 'ps -ax -o pid=,ppid=,command=';
199211
const CMD_PAT = /^\s*([0-9]+)\s+([0-9]+)\s+(.+)$/;
200212

201-
exec(CMD, { maxBuffer: 1000 * 1024 }, (err, stdout, stderr) => {
202-
if (!err && !stderr) {
203-
const lines = stdout.toString().split('\n');
204-
for (const line of lines) {
205-
let matches = CMD_PAT.exec(line.trim());
206-
if (matches && matches.length === 4) {
207-
oneProcess(parseInt(matches[1]), parseInt(matches[2]), matches[3]);
208-
}
209-
}
213+
var proc = spawn('/bin/ps', [ '-ax', '-o', 'pid=,ppid=,command=' ]);
214+
proc.stdout.setEncoding('utf8');
215+
proc.stdout.on('data', lines(line => {
216+
let matches = CMD_PAT.exec(line.trim());
217+
if (matches && matches.length === 4) {
218+
oneProcess(parseInt(matches[1]), parseInt(matches[2]), matches[3]);
210219
}
211-
});
220+
}));
212221
}
213222
}

0 commit comments

Comments
 (0)