Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v14.x backport] child_process: queue pending messages #42840

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const {
ArrayIsArray,
ArrayPrototypePush,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
Symbol,
Uint8Array,
} = primordials;
Expand Down Expand Up @@ -73,6 +75,7 @@ let HTTPParser;
const MAX_HANDLE_RETRANSMISSIONS = 3;
const kChannelHandle = Symbol('kChannelHandle');
const kIsUsedAsStdio = Symbol('kIsUsedAsStdio');
const kPendingMessages = Symbol('kPendingMessages');

// This object contain function to convert TCP objects to native handle objects
// and back again.
Expand Down Expand Up @@ -520,6 +523,7 @@ class Control extends EventEmitter {
constructor(channel) {
super();
this.#channel = channel;
this[kPendingMessages] = [];
}

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

target.on('newListener', function() {

process.nextTick(() => {
if (!target.channel || !target.listenerCount('message'))
return;

const messages = target.channel[kPendingMessages];
const { length } = messages;
if (!length) return;

for (let i = 0; i < length; i++) {
ReflectApply(target.emit, target, messages[i]);
}

target.channel[kPendingMessages] = [];
});
});

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

function emit(event, message, handle) {
target.emit(event, message, handle);
if ('internalMessage' === event || target.listenerCount('message')) {
target.emit(event, message, handle);
return;
}

ArrayPrototypePush(
target.channel[kPendingMessages],
[event, message, handle]
);
}

function handleMessage(message, handle, internal) {
Expand Down
20 changes: 20 additions & 0 deletions test/es-module/test-esm-child-process-fork-main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import '../common/index.mjs';
import assert from 'assert';
import { fork } from 'child_process';
import { once } from 'events';
import { fileURLToPath } from 'url';

if (process.argv[2] !== 'child') {
const filename = fileURLToPath(import.meta.url);
const cp = fork(filename, ['child']);
const message = 'Hello World';
cp.send(message);

const [received] = await once(cp, 'message');
assert.deepStrictEqual(received, message);

cp.disconnect();
await once(cp, 'exit');
} else {
process.on('message', (msg) => process.send(msg));
}