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 — child_process: queue pending messages · nodejs/node@d9465ae

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • child_process.js
  • test/es-module
    • test-esm-child-process-fork-main.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/child_process.js‎

Copy file name to clipboardExpand all lines: lib/internal/child_process.js
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ let HTTPParser;
8181
const MAX_HANDLE_RETRANSMISSIONS = 3;
8282
const kChannelHandle = Symbol('kChannelHandle');
8383
const kIsUsedAsStdio = Symbol('kIsUsedAsStdio');
84+
const kPendingMessages = Symbol('kPendingMessages');
8485

8586
// This object contain function to convert TCP objects to native handle objects
8687
// and back again.
@@ -526,6 +527,7 @@ class Control extends EventEmitter {
526527
constructor(channel) {
527528
super();
528529
this.#channel = channel;
530+
this[kPendingMessages] = [];
529531
}
530532

531533
// The methods keeping track of the counter are being used to track the
@@ -699,6 +701,24 @@ function setupChannel(target, channel, serializationMode) {
699701
});
700702
});
701703

704+
target.on('newListener', function() {
705+
706+
process.nextTick(() => {
707+
if (!target.channel || !target.listenerCount('message'))
708+
return;
709+
710+
const messages = target.channel[kPendingMessages];
711+
const { length } = messages;
712+
if (!length) return;
713+
714+
for (let i = 0; i < length; i++) {
715+
ReflectApply(target.emit, target, messages[i]);
716+
}
717+
718+
target.channel[kPendingMessages] = [];
719+
});
720+
});
721+
702722
target.send = function(message, handle, options, callback) {
703723
if (typeof handle === 'function') {
704724
callback = handle;
@@ -912,7 +932,15 @@ function setupChannel(target, channel, serializationMode) {
912932
};
913933

914934
function emit(event, message, handle) {
915-
target.emit(event, message, handle);
935+
if ('internalMessage' === event || target.listenerCount('message')) {
936+
target.emit(event, message, handle);
937+
return;
938+
}
939+
940+
ArrayPrototypePush(
941+
target.channel[kPendingMessages],
942+
[event, message, handle]
943+
);
916944
}
917945

918946
function handleMessage(message, handle, internal) {
Collapse file

‎test/es-module/test-esm-child-process-fork-main.mjs‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import '../common/index.mjs';
2+
import assert from 'assert';
3+
import { fork } from 'child_process';
4+
import { once } from 'events';
5+
import { fileURLToPath } from 'url';
6+
7+
if (process.argv[2] !== 'child') {
8+
const filename = fileURLToPath(import.meta.url);
9+
const cp = fork(filename, ['child']);
10+
const message = 'Hello World';
11+
cp.send(message);
12+
13+
const [received] = await once(cp, 'message');
14+
assert.deepStrictEqual(received, message);
15+
16+
cp.disconnect();
17+
await once(cp, 'exit');
18+
} else {
19+
process.on('message', (msg) => process.send(msg));
20+
}

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.