-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc,v8: clarify v8.writeHeapSnapshot
may return undefined
#41365
doc,v8: clarify v8.writeHeapSnapshot
may return undefined
#41365
Conversation
There was a problem hiding this comment.
Choose a reason for 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} |
We also needs a test in https://github.com/nodejs/node/blob/HEAD/test/sequential/test-heapdump.js that asserts that the function indeed returns undefined when it fails to write the file.
@@ -275,7 +275,8 @@ added: v11.13.0 | |||
generated, where `{pid}` will be the PID of the Node.js process, | |||
`{thread_id}` will be `0` when `writeHeapSnapshot()` is called from | |||
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.
Choose a reason for 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. |
Why are we documenting this rather than making it throw an error? |
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)
|
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
|
Thank you @RaisinTen, I opened #41373 following your indications. |
We can close this now that #41373 has landed |
Fixes: #41346
Or at least, it's one of the possible approaches. There are two we can do:
undefined
.v8.writeHeapSnapshot()
calls the following code:node/src/heap_utils.cc
Lines 369 to 390 in 0de6a63
As we can see, the lack of
args.GetReturnValue().Set(...)
whenWriteSnapshot(isolate, *name)
returns false (lines 377 and 387) makes the function always returnundefined
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 anundefined
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 againstundefined
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:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/2aae4db6bbe2e078c0cbc84cc9cd43cb1e01d8fd/types/node/v14/test/v8.ts#L13
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.