Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: drain the messages from the internal message port #24932

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ class Worker extends EventEmitter {
[kOnExit](code) {
debug(`[${threadId}] hears end event for Worker ${this.threadId}`);
MessagePortPrototype.drain.call(this[kPublicPort]);
MessagePortPrototype.drain.call(this[kPort]);
this[kDispose]();
this.emit('exit', code);
this.removeAllListeners();
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-worker-message-port-drain.js
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));
}