Skip to content

Commit 3b94fc1

Browse files
committed
lib: child process queue pending messages
It fixes the problem for the child process not receiving messages. Fixes: #41134
1 parent 0d9f3bd commit 3b94fc1

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lib/internal/child_process.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const { convertToValidSignal, deprecate } = require('internal/util');
5959
const { isArrayBufferView } = require('internal/util/types');
6060
const spawn_sync = internalBinding('spawn_sync');
6161
const { kStateSymbol } = require('internal/dgram');
62+
const console = require('console');
6263

6364
const {
6465
UV_EACCES,
@@ -526,6 +527,7 @@ class Control extends EventEmitter {
526527
constructor(channel) {
527528
super();
528529
this.#channel = channel;
530+
this.pendingMessages = new Set();
529531
}
530532

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

704+
target.on('newListener', function () {
705+
if(!target.channel) return;
706+
707+
const messages = target.channel.pendingMessages;
708+
if (!messages.size) return;
709+
710+
for (const messageParams of messages) {
711+
process.nextTick(target.emit.bind(target), ...messageParams);
712+
}
713+
714+
messages.clear();
715+
});
716+
702717
target.send = function(message, handle, options, callback) {
703718
if (typeof handle === 'function') {
704719
callback = handle;
@@ -912,7 +927,15 @@ function setupChannel(target, channel, serializationMode) {
912927
};
913928

914929
function emit(event, message, handle) {
915-
target.emit(event, message, handle);
930+
const args = [event, message, handle];
931+
const isInternalMessage = "internalMessage" === event;
932+
const hasListenersInstalled = target.listenerCount('message');
933+
if (hasListenersInstalled || isInternalMessage) {
934+
target.emit(...args);
935+
return;
936+
}
937+
938+
target.channel.pendingMessages.add(args);
916939
}
917940

918941
function handleMessage(message, handle, internal) {

0 commit comments

Comments
 (0)