Skip to content

Commit

Permalink
lib: child process queue pending messages
Browse files Browse the repository at this point in the history
It fixes the problem for the child process not receiving messages.

Fixes: #41134
  • Loading branch information
ErickWendel committed Dec 17, 2021
1 parent 0d9f3bd commit 3b94fc1
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const { convertToValidSignal, deprecate } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const spawn_sync = internalBinding('spawn_sync');
const { kStateSymbol } = require('internal/dgram');
const console = require('console');

const {
UV_EACCES,
Expand Down Expand Up @@ -526,6 +527,7 @@ class Control extends EventEmitter {
constructor(channel) {
super();
this.#channel = channel;
this.pendingMessages = new Set();
}

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

target.on('newListener', function () {
if(!target.channel) return;

const messages = target.channel.pendingMessages;
if (!messages.size) return;

for (const messageParams of messages) {
process.nextTick(target.emit.bind(target), ...messageParams);
}

messages.clear();
});

target.send = function(message, handle, options, callback) {
if (typeof handle === 'function') {
callback = handle;
Expand Down Expand Up @@ -912,7 +927,15 @@ function setupChannel(target, channel, serializationMode) {
};

function emit(event, message, handle) {
target.emit(event, message, handle);
const args = [event, message, handle];
const isInternalMessage = "internalMessage" === event;
const hasListenersInstalled = target.listenerCount('message');
if (hasListenersInstalled || isInternalMessage) {
target.emit(...args);
return;
}

target.channel.pendingMessages.add(args);
}

function handleMessage(message, handle, internal) {
Expand Down

0 comments on commit 3b94fc1

Please sign in to comment.