worker: flush stdout and stderr on exit · nodejs/node@aab53e6 · GitHub
Skip to content

Commit aab53e6

Browse files
mcollinaRafaelGSS
authored andcommitted
worker: flush stdout and stderr on exit
Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #56428 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent e1887d2 commit aab53e6

4 files changed

Lines changed: 68 additions & 4 deletions

File tree

lib/internal/bootstrap/switches/is_not_main_thread.js

Lines changed: 12 additions & 1 deletion

lib/internal/worker/io.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,13 @@ class WritableWorkerStdio extends Writable {
292292
chunks: ArrayPrototypeMap(chunks,
293293
({ chunk, encoding }) => ({ chunk, encoding })),
294294
});
295-
ArrayPrototypePush(this[kWritableCallbacks], cb);
296-
if (this[kPort][kWaitingStreams]++ === 0)
297-
this[kPort].ref();
295+
if (process._exiting) {
296+
cb();
297+
} else {
298+
ArrayPrototypePush(this[kWritableCallbacks], cb);
299+
if (this[kPort][kWaitingStreams]++ === 0)
300+
this[kPort].ref();
301+
}
298302
}
299303

300304
_final(cb) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const { Worker, isMainThread } = require('worker_threads');
5+
6+
if (isMainThread) {
7+
const w = new Worker(__filename, { stdout: true });
8+
const expected = 'hello world';
9+
10+
let data = '';
11+
w.stdout.setEncoding('utf8');
12+
w.stdout.on('data', (chunk) => {
13+
data += chunk;
14+
});
15+
16+
w.on('exit', common.mustCall(() => {
17+
assert.strictEqual(data, expected);
18+
}));
19+
} else {
20+
process.stdout.write('hello');
21+
process.stdout.write(' ');
22+
process.stdout.write('world');
23+
process.exit(0);
24+
}
Lines changed: 25 additions & 0 deletions

0 commit comments

Comments
 (0)