Skip to content

Commit

Permalink
child_process: retain reference to data with advanced serialization
Browse files Browse the repository at this point in the history
Do the same thing we do for other streams, and retain a reference to
the Buffer that was sent over IPC while the write request is active,
so that it doesn’t get garbage collected while the data is still in
flight.

(This is a good example of why it’s a bad idea that we’re not re-using
the general streams implementation for IPC and instead maintain
separate usage of the low-level I/O primitives.)

Fixes: #34797

PR-URL: #38728
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
addaleax authored and richardlau committed Jul 19, 2021
1 parent 18a8834 commit 7bfff39
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/internal/child_process/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { StringDecoder } = require('string_decoder');
const v8 = require('v8');
const { isArrayBufferView } = require('internal/util/types');
const assert = require('internal/assert');
const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap');

const kMessageBuffer = Symbol('kMessageBuffer');
const kJSONBuffer = Symbol('kJSONBuffer');
Expand Down Expand Up @@ -82,10 +83,16 @@ const advanced = {
const serializedMessage = ser.releaseBuffer();
const sizeBuffer = Buffer.allocUnsafe(4);
sizeBuffer.writeUInt32BE(serializedMessage.length);
return channel.writeBuffer(req, Buffer.concat([

const buffer = Buffer.concat([
sizeBuffer,
serializedMessage
]), handle);
serializedMessage,
]);
const result = channel.writeBuffer(req, buffer, handle);
// Mirror what stream_base_commons.js does for Buffer retention.
if (streamBaseState[kLastWriteWasAsync])
req.buffer = buffer;
return result;
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const child_process = require('child_process');

// Regression test for https://github.com/nodejs/node/issues/34797
const eightMB = 8 * 1024 * 1024;

if (process.argv[2] === 'child') {
for (let i = 0; i < 4; i++) {
process.send(new Uint8Array(eightMB).fill(i));
}
} else {
const child = child_process.spawn(process.execPath, [__filename, 'child'], {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
serialization: 'advanced'
});
const received = [];
child.on('message', common.mustCall((chunk) => {
assert.deepStrictEqual(chunk, new Uint8Array(eightMB).fill(chunk[0]));

received.push(chunk[0]);
if (received.length === 4) {
assert.deepStrictEqual(received, [0, 1, 2, 3]);
}
}, 4));
}

0 comments on commit 7bfff39

Please sign in to comment.