Skip to content

Commit 717a8b6

Browse files
addaleaxdanielleadams
authored andcommitted
child_process: retain reference to data with advanced serialization
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]>
1 parent 7a9d0fd commit 717a8b6

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

‎lib/internal/child_process/serialization.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const{StringDecoder } = require('string_decoder');
1212
constv8=require('v8');
1313
const{ isArrayBufferView }=require('internal/util/types');
1414
constassert=require('internal/assert');
15+
const{ streamBaseState, kLastWriteWasAsync }=internalBinding('stream_wrap');
1516

1617
constkMessageBuffer=Symbol('kMessageBuffer');
1718
constkJSONBuffer=Symbol('kJSONBuffer');
@@ -82,10 +83,16 @@ const advanced ={
8283
constserializedMessage=ser.releaseBuffer();
8384
constsizeBuffer=Buffer.allocUnsafe(4);
8485
sizeBuffer.writeUInt32BE(serializedMessage.length);
85-
returnchannel.writeBuffer(req,Buffer.concat([
86+
87+
constbuffer=Buffer.concat([
8688
sizeBuffer,
8789
serializedMessage,
88-
]),handle);
90+
]);
91+
constresult=channel.writeBuffer(req,buffer,handle);
92+
// Mirror what stream_base_commons.js does for Buffer retention.
93+
if(streamBaseState[kLastWriteWasAsync])
94+
req.buffer=buffer;
95+
returnresult;
8996
},
9097
};
9198

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constchild_process=require('child_process');
5+
6+
// Regression test for https://github.com/nodejs/node/issues/34797
7+
consteightMB=8*1024*1024;
8+
9+
if(process.argv[2]==='child'){
10+
for(leti=0;i<4;i++){
11+
process.send(newUint8Array(eightMB).fill(i));
12+
}
13+
}else{
14+
constchild=child_process.spawn(process.execPath,[__filename,'child'],{
15+
stdio: ['inherit','inherit','inherit','ipc'],
16+
serialization: 'advanced'
17+
});
18+
constreceived=[];
19+
child.on('message',common.mustCall((chunk)=>{
20+
assert.deepStrictEqual(chunk,newUint8Array(eightMB).fill(chunk[0]));
21+
22+
received.push(chunk[0]);
23+
if(received.length===4){
24+
assert.deepStrictEqual(received,[0,1,2,3]);
25+
}
26+
},4));
27+
}

0 commit comments

Comments
(0)