Skip to content

Commit ba5a668

Browse files
joyeecheungBridgeAR
authored andcommitted
doc: note caveats in process message serialization
The message sent using process.send() goes through JSON serialization and parsing, which could lead to surprising behaviors. This commit elaborate a bit more on this and add a link to the notes about these caveats in the ECMAScript specification. PR-URL: #12963 Refs: #12497 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 10bea3c commit ba5a668

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

doc/api/child_process.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ added: v0.5.9
891891
The `'message'` event is triggered when a child process uses [`process.send()`][]
892892
to send messages.
893893

894+
*Note*: The message goes through JSON serialization and parsing. The resulting
895+
message might not be the same as what is originally sent. See notes in
896+
[the `JSON.stringify()` specification][`JSON.stringify` spec].
897+
894898
### subprocess.channel
895899
<!-- YAML
896900
added: v7.1.0
@@ -1050,6 +1054,10 @@ be used to send messages to the child process. When the child process is a
10501054
Node.js instance, these messages can be received via the
10511055
[`process.on('message')`][] event.
10521056

1057+
*Note*: The message goes through JSON serialization and parsing. The resulting
1058+
message might not be the same as what is originally sent. See notes in
1059+
[the `JSON.stringify()` specification][`JSON.stringify` spec].
1060+
10531061
For example, in the parent script:
10541062

10551063
```js
@@ -1060,6 +1068,7 @@ n.on('message', (m) => {
10601068
console.log('PARENT got message:', m);
10611069
});
10621070

1071+
// Causes the child to print: CHILD got message: { hello: 'world' }
10631072
n.send({ hello: 'world' });
10641073
```
10651074

@@ -1070,7 +1079,8 @@ process.on('message', (m) => {
10701079
console.log('CHILD got message:', m);
10711080
});
10721081

1073-
process.send({ foo: 'bar' });
1082+
// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }
1083+
process.send({ foo: 'bar', baz: NaN });
10741084
```
10751085

10761086
Child Node.js processes will have a [`process.send()`][] method of their own that
@@ -1201,9 +1211,6 @@ It is also recommended that any `'message'` handlers in the child process
12011211
verify that `socket` exists, as the connection may have been closed during the
12021212
time it takes to send the connection to the child.
12031213

1204-
*Note*: This function uses [`JSON.stringify()`][] internally to serialize the
1205-
`message`.
1206-
12071214
### subprocess.stderr
12081215
<!-- YAML
12091216
added: v0.1.90
@@ -1319,6 +1326,7 @@ unavailable.
13191326
[`ChildProcess`]: #child_process_child_process
13201327
[`Error`]: errors.html#errors_class_error
13211328
[`EventEmitter`]: events.html#events_class_eventemitter
1329+
[`JSON.stringify` spec]: https://tc39.github.io/ecma262/#sec-json.stringify
13221330
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
13231331
[`subprocess.connected`]: #child_process_subprocess_connected
13241332
[`subprocess.disconnect()`]: #child_process_subprocess_disconnect

doc/api/process.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ message sent by a parent process using [`childprocess.send()`][] is received by
9090
the child process.
9191

9292
The listener callback is invoked with the following arguments:
93-
* `message` {Object} a parsed JSON object or primitive value
93+
* `message` {Object} a parsed JSON object or primitive value.
9494
* `sendHandle` {Handle object} a [`net.Socket`][] or [`net.Server`][] object, or
9595
undefined.
9696

97+
*Note*: The message goes through JSON serialization and parsing. The resulting
98+
message might not be the same as what is originally sent. See notes in
99+
[the `JSON.stringify()` specification][`JSON.stringify` spec].
97100

98101
### Event: 'rejectionHandled'
99102
<!-- YAML
@@ -1472,8 +1475,9 @@ used to send messages to the parent process. Messages will be received as a
14721475
If Node.js was not spawned with an IPC channel, `process.send()` will be
14731476
`undefined`.
14741477

1475-
*Note*: This function uses [`JSON.stringify()`][] internally to serialize the
1476-
`message`.
1478+
*Note*: The message goes through JSON serialization and parsing. The resulting
1479+
message might not be the same as what is originally sent. See notes in
1480+
[the `JSON.stringify()` specification][`JSON.stringify` spec].
14771481

14781482
## process.setegid(id)
14791483
<!-- YAML
@@ -1877,6 +1881,7 @@ cases:
18771881
[`ChildProcess`]: child_process.html#child_process_class_childprocess
18781882
[`Error`]: errors.html#errors_class_error
18791883
[`EventEmitter`]: events.html#events_class_eventemitter
1884+
[`JSON.stringify` spec]: https://tc39.github.io/ecma262/#sec-json.stringify
18801885
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
18811886
[`console.error()`]: console.html#console_console_error_data_args
18821887
[`console.log()`]: console.html#console_console_log_data_args

0 commit comments

Comments
 (0)