Repository navigation
fs: restore stack traces of fs/promises readFile and writeFile errors #66757
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
Open
vedchaudhari
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
vedchaudhari:fs-promises-stack-trace
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { spawnSync } = require('child_process'); | ||
| const tmpdir = require('../common/tmpdir'); | ||
|
|
||
| tmpdir.refresh(); | ||
|
|
||
| // The permission model denies access before the first await, so the stack | ||
| // still has the frames of a caller that does not await the promise. | ||
| const { status, stderr } = spawnSync( | ||
| process.execPath, | ||
| [ | ||
| '--permission', | ||
| '-e', | ||
| ` | ||
| const assert = require('assert'); | ||
| const { readFile, writeFile, appendFile } = require('fs/promises'); | ||
| const target = ${JSON.stringify(tmpdir.resolve('file'))}; | ||
|
|
||
| function readCaller() { return readFile(target); } | ||
| function writeCaller() { return writeFile(target, 'data'); } | ||
| function appendCaller() { return appendFile(target, 'data'); } | ||
| async function awaitAppendCaller() { await appendFile(target, 'data'); } | ||
|
|
||
| const cases = [ | ||
| [readCaller, /at readCaller /], | ||
| [writeCaller, /at writeCaller /], | ||
| [appendCaller, /at appendCaller /], | ||
| [awaitAppendCaller, /at awaitAppendCaller /], | ||
| ]; | ||
| let checked = 0; | ||
| for (const [caller, stack] of cases) { | ||
| assert.rejects(caller(), { code: 'ERR_ACCESS_DENIED', stack }) | ||
| .then(() => checked++); | ||
| } | ||
| process.on('exit', () => assert.strictEqual(checked, cases.length)); | ||
| `, | ||
| ], | ||
| ); | ||
| assert.strictEqual(status, 0, stderr.toString()); |
Oops, something went wrong.
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.
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.
This loses the caller's frames for permission errors:
With
node --permission --allow-fs-read=. repro.cjs, main showsat loadConfig. With this PR, the stack only hasreadFileandprocessTicksAndRejections.job.run()returns the permission error synchronously, so capturing its stack after theawaitloses those frames. Could we keep the existing capture foraccessErrorin the helper and only recapture errors from the async completion?writeFile()has the same issue, andappendFile()loses the caller's frame even when awaited. Please add a permission test with a non-async caller that checks its frame is preserved in the stack. The existing fixtures don't check the stack.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.
You're right, I only tested ENOENT and didn't think about the permission error. I changed the helpers to throw it directly instead of rejecting, so the caller shows up in the stack again, appendFile() too.
I’ve also added a test that fails without this change. Fixed in 42e6ebd. If you’d prefer to keep the capture in the helper, I can change it.
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.
Looks good to me, no need to move the capture back into the helper. The throw happens before anything is awaited, so the catch still sees the caller's frames.
One non-blocking nit on the new test. The child doesn't check that all four
assert.rejects()calls finished. If a denied call ever stayed pending, the process would exit with 0 without checking any stack. A counter checked on exit would cover it. The existing fixtures would still catch a hang, so this is only about this test.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.
Good point, I've added a counter that's checked on exit, so the test now fails if any of the calls never finishes.