Skip to content

Commit 420b973

Browse files
authored
fix: read stdout/stderr in parallel (#59)
Some nonsense happens when you read one after the other, which sometimes results in one never closing (the for loop never ends). This fixes it by running both in parallel.
1 parent ad3a79a commit 420b973

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/main.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ function combineSignals(signals: Iterable<AbortSignal>): AbortSignal {
9696
return controller.signal;
9797
}
9898

99+
async function readStream(stream: Readable): Promise<string> {
100+
let output = '';
101+
for await (const chunk of stream) {
102+
output += chunk.toString();
103+
}
104+
return output;
105+
}
106+
99107
export class ExecProcess implements Result {
100108
protected _process?: ChildProcess;
101109
protected _aborted: boolean = false;
@@ -210,20 +218,10 @@ export class ExecProcess implements Result {
210218
throw new Error('No process was started');
211219
}
212220

213-
let stderr = '';
214-
let stdout = '';
215-
216-
if (this._streamOut) {
217-
for await (const chunk of this._streamOut) {
218-
stdout += chunk.toString();
219-
}
220-
}
221-
222-
if (this._streamErr) {
223-
for await (const chunk of this._streamErr) {
224-
stderr += chunk.toString();
225-
}
226-
}
221+
const [stdout, stderr] = await Promise.all([
222+
this._streamOut ? readStream(this._streamOut) : '',
223+
this._streamErr ? readStream(this._streamErr) : ''
224+
]);
227225

228226
await this._processClosed;
229227

0 commit comments

Comments
 (0)