-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: drain messages from internal message port
When the worker thread exits, drain the messages also from the internal message port so that the call to 'kDispose' will occur only after all the messages from the worker were processed in the parent, so stdio messages from the worker will be successfully pushed to their target streams in the parent. PR-URL: #24932 Fixes: #24636 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
1 parent
f854701
commit 331f604
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
require('../common'); | ||
|
||
// This test ensures that the messages from the internal | ||
// message port are drained before the call to 'kDispose', | ||
// and so all the stdio messages from the worker are processed | ||
// in the parent and are pushed to their target streams. | ||
|
||
const assert = require('assert'); | ||
const { | ||
Worker, | ||
isMainThread, | ||
parentPort, | ||
threadId, | ||
} = require('worker_threads'); | ||
|
||
if (isMainThread) { | ||
const workerIdsToOutput = new Map(); | ||
|
||
for (let i = 0; i < 2; i++) { | ||
const worker = new Worker(__filename, { stdout: true }); | ||
const workerOutput = []; | ||
workerIdsToOutput.set(worker.threadId, workerOutput); | ||
worker.on('message', console.log); | ||
worker.stdout.on('data', (chunk) => { | ||
workerOutput.push(chunk.toString().trim()); | ||
}); | ||
} | ||
|
||
process.on('exit', () => { | ||
for (const [threadId, workerOutput] of workerIdsToOutput) { | ||
assert.ok(workerOutput.includes(`1 threadId: ${threadId}`)); | ||
assert.ok(workerOutput.includes(`2 threadId: ${threadId}`)); | ||
} | ||
}); | ||
} else { | ||
console.log(`1 threadId: ${threadId}`); | ||
console.log(`2 threadId: ${threadId}`); | ||
parentPort.postMessage(Array(100).fill(1)); | ||
} |