Skip to content

Commit eb8c12a

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 eb8c12a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/internal/child_process.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ class Control extends EventEmitter {
526526
constructor(channel) {
527527
super();
528528
this.#channel = channel;
529+
this.pendingMessages = new Set();
529530
}
530531

531532
// The methods keeping track of the counter are being used to track the
@@ -699,6 +700,19 @@ function setupChannel(target, channel, serializationMode) {
699700
});
700701
});
701702

703+
target.on('newListener', function () {
704+
if(!target.channel) return;
705+
706+
const messages = target.channel.pendingMessages;
707+
if (!messages.size) return;
708+
709+
for (const messageParams of messages) {
710+
process.nextTick(target.emit.bind(target), ...messageParams);
711+
}
712+
713+
messages.clear();
714+
});
715+
702716
target.send = function(message, handle, options, callback) {
703717
if (typeof handle === 'function') {
704718
callback = handle;
@@ -912,7 +926,15 @@ function setupChannel(target, channel, serializationMode) {
912926
};
913927

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

918940
function handleMessage(message, handle, internal) {

0 commit comments

Comments
 (0)