Skip to content

child_process: harden native IPC framer error and memory handling - #43

Merged
anonrig merged 1 commit into
anonrig:child-process-native-ipc-framingfrom
lemire:ipc-framer-hardening
Nov 16, 2026
Merged

anonrig merged 1 commit into
anonrig:child-process-native-ipc-framingfrom
lemire:ipc-framer-hardening

Conversation

@lemire

@lemire lemire commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

This is a follow-up to nodejs#64072. It targets that PR's branch, so it can be folded into it.

Error handling

In nodejs#64072, the framer only advances its state after a frame deserializes successfully. A frame that fails to deserialize therefore:

  • Started in an earlier read: stays in the buffer, so every later read() throws again and the channel is stuck.
  • Is complete within the current read: discards the partial frame that follows it, so the stream desynchronizes.
  • Comes after other messages in the same read: discards those messages, which were already decoded.

Now each frame is consumed before it is deserialized. The first error is held in the framer, and the remaining frames in the read are still decoded. parseChannelMessages() delivers the decoded messages and then throws the held error. A small generator handles this, and it is only created on the error path. Termination exceptions are rethrown immediately. The old buffering() method becomes status() (a bitmask) plus takeError(), so the normal path still makes only one extra binding call.

Memory and copies for frames that span reads

  • A frame larger than 64 KiB is accumulated directly into a BackingStore, sized from its length prefix, instead of a std::vector<char>. When the frame is complete, the store becomes an ArrayBuffer. ArrayBufferView host objects borrow from it, the same as for frames within one read, instead of being copied a second time. The memory is released by the GC with those views, instead of staying in the vector's capacity for the life of the channel.
  • Bytes are copied with memcpy instead of one push_back per byte.
  • If the allocation fails, the framer throws ERR_MEMORY_ALLOCATION_FAILED and discards the frame's bytes, so the stream stays in sync.
  • Smaller frames use a buffer the framer reuses (see Benchmarks).

Other changes

  • The length check is written as available - 4 >= size, so it cannot overflow when size_t is 32 bits.
  • MemoryInfo reports the buffered payload size instead of visiting every byte.

Tests

  • New cctests: a Buffer host object in a frame split across four reads (one split inside the length prefix), and an invalid frame between valid ones with a partial frame after it.
  • New test/parallel/test-child-process-advanced-serialization-spanning-frames.js: messages from 1 byte to 1 MiB carrying Buffer, Float64Array and Uint16Array data, with their contents checked.
  • parallel/test-child-process-*, parallel/test-cluster-*, sequential/test-child-process-* and sequential/test-cluster-* pass (Linux x86-64).

Benchmarks

Linux x86-64 (128 cores).

benchmark/child_process/child-process-ipc-roundtrip (msgs/s), comparing nodejs#64072 with this PR on top of it, 10 runs each:

serialization len nodejs#64072 this PR delta
advanced 1 KiB 206,235 200,980 -2.5% (t=-1.4, not significant)
advanced 4 KiB 136,413 136,196 -0.2%
advanced 16 KiB 57,568 58,669 +1.9%
advanced 64 KiB 8,444 13,103 +55.2% (t=18.2)
json 1–64 KiB within ±1.5%

Round trips of { b: Buffer.alloc(len), f: new Float64Array(len / 16) } with serialization: 'advanced' (msgs/s, mean of 3 runs). This compares the current main (the JS framer), nodejs#64072, and nodejs#64072 with this PR on top:

len main nodejs#64072 this PR
64 KiB 8,367 5,143 8,658
256 KiB 2,068 386 2,050
1 MiB 514 152 560
8 MiB 62 18 73

On these large messages, nodejs#64072 alone is 3–5× slower than main because it copies one byte at a time and copies large frames twice. This PR restores main's throughput or improves on it.

To keep small frames that span reads cheap (one BackingStore and ArrayBuffer per frame cost about 8% at 16 KiB), payloads up to 64 KiB are accumulated into a buffer the framer reuses. Its size is bounded, so it never retains more than 64 KiB.

Consume each frame before deserializing it so that an invalid frame
neither wedges nor desynchronizes the channel, and deliver the
messages decoded from the same read before throwing its error.
Accumulate large frames that span reads into a BackingStore that
host objects borrow, and small ones into a bounded reused buffer.
Copy in bulk and avoid 32-bit overflow in the length check.

Signed-off-by: Daniel Lemire <daniel@lemire.me>
@lemire
lemire requested a review from anonrig November 15, 2026 21:21
@anonrig
anonrig merged commit 67a2a60 into anonrig:child-process-native-ipc-framing Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants