GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — doc,v8: clarify `v8.writeHeapSnapshot` may return `undefined` by kyranet · Pull Request #41365 · nodejs/node Conversation
|
| void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) { | |
| Environment* env = Environment::GetCurrent(args); | |
| Isolate* isolate = args.GetIsolate(); | |
| Local<Value> filename_v = args[0]; | |
| if (filename_v->IsUndefined()) { | |
| DiagnosticFilename name(env, "Heap", "heapsnapshot"); | |
| if (!WriteSnapshot(isolate, *name)) | |
| return; | |
| if (String::NewFromUtf8(isolate, *name).ToLocal(&filename_v)) { | |
| args.GetReturnValue().Set(filename_v); | |
| } | |
| return; | |
| } | |
| BufferValue path(isolate, filename_v); | |
| CHECK_NOT_NULL(*path); | |
| if (!WriteSnapshot(isolate, *path)) | |
| return; | |
| return args.GetReturnValue().Set(filename_v); | |
| } |
As we can see, the lack of args.GetReturnValue().Set(...) when WriteSnapshot(isolate, *name) returns false (lines 377 and 387) makes the function always return undefined when the file fails to write.
It does seem that the upstream module sets an error, which can be a lot more useful: https://github.com/bnoordhuis/node-heapdump/blob/3537b67cebdbd602d8572fcc533f223ed25e5cbc/src/heapdump.cc#L93-L96
I believe node-heapdump's approach is more sensible, as there are several reasons why a file write can fail (out of space, missing permissions...) and said error helps us determine the cause, where an undefined only tells us that the write failed.
The usage of v8.writeHeapSnapshot() is quite small in the ecosystem (according to GitHub's Code Search, 16 usages in JavaScript and 5 in TypeScript outside of Node.js forks) all of them rely on it returning a string or to write a file successfully, none of the matches seem to compare against undefined or a falsy/truthy value.
I don't know what would be the exact implication of defining the error state as an error being thrown rather than silently erroring with undefined, however, one of the matches was from DefinitelyTyped, a file used in CI:
I believe we are still able to change this behavior to throw an exception because:
*: According to cs.github.com's data.
As recommended by @RaisinTen in the aforementioned issue, I'm opening a docs PR. I would have opted in for making a PR for Approach 2 (throw an exception) instead if I had more availability and I was more confident to contribute in the code.
I'll be more than happy if somebody else can make the PR if the team decides to take the second approach, otherwise this PR should have everything for the first one. I checked all other heap snapshot methods, and none seems to have this behavior-documentation mismatch.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thank you for sending the PR!
Could you please update the return type in the JSDoc declaration of the function?
Line 66 in 0de6a63
| * @returns {string} |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| the main Node.js thread or the id of a worker thread. | ||
| * Returns: {string} The filename where the snapshot was saved. | ||
| * Returns: {string|undefined} The filename where the snapshot was saved, or | ||
| `undefined` if the file failed to write. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| `undefined` if the file failed to write. | |
| `undefined` if the process failed to write the file. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Why are we documenting this rather than making it throw an error? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
In the linked PR, I was advised to do a docs PR in case throwing an exception would eventually become blocked, as seen in #41346 (comment)
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
In that case, I think it would be fine if we throw an exception. Passing env instead of the isolate to WriteSnapshot() and calling Lines 319 to 327 in d0c1176
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thank you @RaisinTen, I opened #41373 following your indications. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
We can close this now that #41373 has landed |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
v8.writeHeapSnapshot may return undefined if the file fails to write
There was an error while loading. Please reload this page.