Skip to content

Commit 6cd64f3

Browse files
joyeecheungjasnell
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 <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 9fc8edd commit 6cd64f3

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

doc/api/child_process.md

100644100755
+12-1
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
<a name="child_process_child_channel"></a>
895899
### subprocess.channel
896900
<!-- YAML
@@ -1056,6 +1060,10 @@ be used to send messages to the child process. When the child process is a
10561060
Node.js instance, these messages can be received via the
10571061
[`process.on('message')`][] event.
10581062

1063+
*Note*: The message goes through JSON serialization and parsing. The resulting
1064+
message might not be the same as what is originally sent. See notes in
1065+
[the `JSON.stringify()` specification][`JSON.stringify` spec].
1066+
10591067
For example, in the parent script:
10601068

10611069
```js
@@ -1066,6 +1074,7 @@ n.on('message', (m) => {
10661074
console.log('PARENT got message:', m);
10671075
});
10681076

1077+
// Causes the child to print: CHILD got message: { hello: 'world' }
10691078
n.send({ hello: 'world' });
10701079
```
10711080

@@ -1076,7 +1085,8 @@ process.on('message', (m) => {
10761085
console.log('CHILD got message:', m);
10771086
});
10781087

1079-
process.send({ foo: 'bar' });
1088+
// Causes the parent to print: PARENT got message: { foo: 'bar', baz: null }
1089+
process.send({ foo: 'bar', baz: NaN });
10801090
```
10811091

10821092
Child Node.js processes will have a [`process.send()`][] method of their own that
@@ -1329,6 +1339,7 @@ unavailable.
13291339
[`ChildProcess`]: #child_process_child_process
13301340
[`Error`]: errors.html#errors_class_error
13311341
[`EventEmitter`]: events.html#events_class_eventemitter
1342+
[`JSON.stringify` spec]: https://tc39.github.io/ecma262/#sec-json.stringify
13321343
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
13331344
[`subprocess.connected`]: #child_process_subprocess_connected
13341345
[`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
@@ -1430,8 +1433,9 @@ used to send messages to the parent process. Messages will be received as a
14301433
If Node.js was not spawned with an IPC channel, `process.send()` will be
14311434
`undefined`.
14321435

1433-
*Note*: This function uses [`JSON.stringify()`][] internally to serialize the
1434-
`message`.
1436+
*Note*: The message goes through JSON serialization and parsing. The resulting
1437+
message might not be the same as what is originally sent. See notes in
1438+
[the `JSON.stringify()` specification][`JSON.stringify` spec].
14351439

14361440
## process.setegid(id)
14371441
<!-- YAML
@@ -1833,6 +1837,7 @@ cases:
18331837
[`ChildProcess`]: child_process.html#child_process_class_childprocess
18341838
[`Error`]: errors.html#errors_class_error
18351839
[`EventEmitter`]: events.html#events_class_eventemitter
1840+
[`JSON.stringify` spec]: https://tc39.github.io/ecma262/#sec-json.stringify
18361841
[`JSON.stringify()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
18371842
[`console.error()`]: console.html#console_console_error_data_args
18381843
[`console.log()`]: console.html#console_console_log_data_args

0 commit comments

Comments
 (0)