Skip to content

Avoid converting whole Buffers when encoding debug messages - #5973

Merged
knolleary merged 1 commit into
node-red:mainfrom
Juice-de-Orange:fix/debug-large-buffer
Oct 7, 2026
Merged

knolleary merged 1 commit into
node-red:mainfrom
Juice-de-Orange:fix/debug-large-buffer

Conversation

@Juice-de-Orange

Copy link
Copy Markdown
Contributor
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Proposed changes

Fixes #5248

JSON.stringify calls Buffer.prototype.toJSON before encodeObject's replacer runs, so every Buffer in a debug message was first copied into an array with one element per byte and only then cut down to debugMaxLength. A file upload in msg.req.files is enough to run out of heap that way.

While encodeObject stringifies, Buffers longer than maxLength now only convert the bytes that are shown. The original toJSON is put back in a finally, and on the next tick as well, because the finally is skipped when a Function node's timeout terminates execution in the middle of node.warn. Doing it in the replacer at the parent object instead would mean returning copies of the parent, which breaks json-stringify-safe's cycle detection. Only code running during the stringify itself can notice the swap (a getter, or a Buffer's own toJSON, calling Buffer.prototype.toJSON), plus, after a terminated call, whatever runs before the next tick. A top-level Buffer is also only hex encoded as far as it's shown.

Calling encodeObject directly (maxLength 1000), with the Buffer nested in the message unless noted:

Buffer main this PR
50 MB 8 to 50 s depending on load, 0.5 to 1.2 GB RSS ~10 ms, 109 MB RSS
100 MB, --max-old-space-size=512 heap out of memory ~30 ms
300 MB as msg.payload encodeObject error (hex string too long) ~1 ms

In the runtime with a 512 MB heap, a 100 MB upload to the flow from the issue (http in with file uploads → debug showing the complete msg, plus an http response) makes main abort with "heap out of memory", while this branch answers with 200 within a few seconds and keeps running. Same with the debug node set to msg.req.files.

The encoded output doesn't change: 192 cases (nested, arrays, cycles, maps, different maxLength) are byte-identical on main and this branch.

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run npm run test to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

JSON.stringify calls Buffer's toJSON, which copies every byte into an
array, before encodeObject's replacer gets to truncate it. A large Buffer
anywhere in a message sent to the debug sidebar (e.g. msg.req.files from
an HTTP In node with file uploads) could take tens of seconds and run the
runtime out of heap.

While encodeObject stringifies, Buffers longer than maxLength now only
convert the bytes that are shown. The original toJSON is restored in a
finally block and, in case execution is terminated (e.g. by a Function
node timeout), on the next tick. A top-level Buffer is likewise only hex
encoded as far as needed.

Fixes node-red#5248
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Juice-de-Orange / name: Juice-de-Orange (bce18a2)

@Juice-de-Orange

Copy link
Copy Markdown
Contributor Author

/easycla

@knolleary

Copy link
Copy Markdown
Member

Thanks for this @Juice-de-Orange

Have you completed the easycla process?

@knolleary

Copy link
Copy Markdown
Member

/easycla

@Juice-de-Orange

Copy link
Copy Markdown
Contributor Author

Yes, it's signed now and the EasyCLA check is green. Sorry for the delay.

@knolleary
knolleary merged commit cbf311f into node-red:main Oct 7, 2026
3 checks passed
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.

Http in Post with file uploads crashes node-red with large files

2 participants