Repository navigation
Avoid converting whole Buffers when encoding debug messages - #5973
Merged
Merged
Conversation
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
|
|
Contributor
Author
|
/easycla |
Member
|
Thanks for this @Juice-de-Orange Have you completed the easycla process? |
Member
|
/easycla |
Contributor
Author
|
Yes, it's signed now and the EasyCLA check is green. Sorry for the delay. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
Fixes #5248
JSON.stringifycallsBuffer.prototype.toJSONbeforeencodeObject'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 todebugMaxLength. A file upload inmsg.req.filesis enough to run out of heap that way.While
encodeObjectstringifies, Buffers longer thanmaxLengthnow only convert the bytes that are shown. The originaltoJSONis put back in afinally, and on the next tick as well, because thefinallyis skipped when a Function node's timeout terminates execution in the middle ofnode.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 owntoJSON, callingBuffer.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
encodeObjectdirectly (maxLength1000), with the Buffer nested in the message unless noted:--max-old-space-size=512msg.payloadIn 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
npm run testto verify the unit tests pass