GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — worker: drain messages from internal message port · nodejs/node@483ff7b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • worker.js
  • test/parallel
    • test-worker-message-port-drain.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/worker.js‎

Copy file name to clipboardExpand all lines: lib/internal/worker.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ class Worker extends EventEmitter {
317317
[kOnExit](code) {
318318
debug(`[${threadId}] hears end event for Worker ${this.threadId}`);
319319
MessagePortPrototype.drain.call(this[kPublicPort]);
320+
MessagePortPrototype.drain.call(this[kPort]);
320321
this[kDispose]();
321322
this.emit('exit', code);
322323
this.removeAllListeners();
Collapse file

‎test/parallel/test-worker-message-port-drain.js‎

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Flags: --experimental-worker
2+
'use strict';
3+
require('../common');
4+
5+
// This test ensures that the messages from the internal
6+
// message port are drained before the call to 'kDispose',
7+
// and so all the stdio messages from the worker are processed
8+
// in the parent and are pushed to their target streams.
9+
10+
const assert = require('assert');
11+
const {
12+
Worker,
13+
isMainThread,
14+
parentPort,
15+
threadId,
16+
} = require('worker_threads');
17+
18+
if (isMainThread) {
19+
const workerIdsToOutput = new Map();
20+
21+
for (let i = 0; i < 2; i++) {
22+
const worker = new Worker(__filename, { stdout: true });
23+
const workerOutput = [];
24+
workerIdsToOutput.set(worker.threadId, workerOutput);
25+
worker.on('message', console.log);
26+
worker.stdout.on('data', (chunk) => {
27+
workerOutput.push(chunk.toString().trim());
28+
});
29+
}
30+
31+
process.on('exit', () => {
32+
for (const [threadId, workerOutput] of workerIdsToOutput) {
33+
assert.ok(workerOutput.includes(`1 threadId: ${threadId}`));
34+
assert.ok(workerOutput.includes(`2 threadId: ${threadId}`));
35+
}
36+
});
37+
} else {
38+
console.log(`1 threadId: ${threadId}`);
39+
console.log(`2 threadId: ${threadId}`);
40+
parentPort.postMessage(Array(100).fill(1));
41+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.