GeezerVison is showing github.com — worker: flush stdout and stderr on exit · nodejs/node@aab53e6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/switches/is_not_main_thread.js +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,11 +33,22 @@ process.removeListener('removeListener', stopListeningIfSignal); | 33 | 33 |
| 34 | 34 | const { | 35 | 35 | createWorkerStdio, | | 36 | + kStdioWantsMoreDataCallback, | 36 | 37 | } = require('internal/worker/io'); | 37 | 38 |
| 38 | 39 | let workerStdio; | 39 | 40 | function lazyWorkerStdio() { | 40 | | - return workerStdio ??= createWorkerStdio(); | | 41 | + if (workerStdio === undefined) { | | 42 | + workerStdio = createWorkerStdio(); | | 43 | + process.on('exit', flushSync); | | 44 | + } | | 45 | + | | 46 | + return workerStdio; | | 47 | +} | | 48 | + | | 49 | +function flushSync() { | | 50 | + workerStdio.stdout[kStdioWantsMoreDataCallback](); | | 51 | + workerStdio.stderr[kStdioWantsMoreDataCallback](); | 41 | 52 | } | 42 | 53 |
| 43 | 54 | function getStdout() { return lazyWorkerStdio().stdout; } | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/worker/io.js +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -292,9 +292,13 @@ class WritableWorkerStdio extends Writable { | 292 | 292 | chunks: ArrayPrototypeMap(chunks, | 293 | 293 | ({ chunk, encoding }) => ({ chunk, encoding })), | 294 | 294 | }); | 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 | + } | 298 | 302 | } | 299 | 303 |
| 300 | 304 | _final(cb) { | |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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.on('exit', () => { | | 21 | + process.stdout.write(' '); | | 22 | + process.stdout.write('world'); | | 23 | + }); | | 24 | + process.stdout.write('hello'); | | 25 | +} |
|