Skip to content

lib: fix fs.readdir recursive async#56041

Merged
aduh95 merged 3 commits into
nodejs:mainfrom
RafaelGSS:fix-readdir-async-throw
Dec 5, 2024
Merged

lib: fix fs.readdir recursive async#56041
aduh95 merged 3 commits into
nodejs:mainfrom
RafaelGSS:fix-readdir-async-throw

Conversation

@RafaelGSS

@RafaelGSS RafaelGSS commented Nov 27, 2024

Copy link
Copy Markdown
Member

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Nov 27, 2024
@codecov

codecov Bot commented Nov 27, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 92.98246% with 8 lines in your changes missing coverage. Please review.

Project coverage is 88.01%. Comparing base (ce346b6) to head (16a29cb).
Report is 71 commits behind head on main.

Files with missing lines Patch % Lines
lib/fs.js 92.98% 8 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #56041    +/-   ##
========================================
  Coverage   88.00%   88.01%            
========================================
  Files         653      656     +3     
  Lines      188093   189077   +984     
  Branches    35942    36006    +64     
========================================
+ Hits       165537   166412   +875     
- Misses      15734    15840   +106     
- Partials     6822     6825     +3     
Files with missing lines Coverage Δ
lib/fs.js 93.26% <92.98%> (-0.09%) ⬇️

... and 55 files with indirect coverage changes

@RafaelGSS RafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 27, 2024
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 27, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment thread lib/fs.js
}
}

function handleDirents({ result, currentPath, context }) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding jsdoc to all newly/updated functions?

@RafaelGSS RafaelGSS Nov 28, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't added jsdoc to these ones because they are not public, they are used inside readdirRecursive (which has JSDoc). It seems a pattern on this file.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment thread lib/fs.js Outdated

@LiviaMedeiros LiviaMedeiros left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node/lib/fs.js

Line 1533 in 982a50e

options = getOptions(options);

If options gets mutated (for example, to be reused subsequent readdir) after the function call during the directory processing, the results might become unpredictable. We should copyObject() it.

Comment thread lib/fs.js Outdated
Comment thread lib/fs.js Outdated
Comment thread lib/fs.js Outdated

@Ethan-Arrowood Ethan-Arrowood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@aduh95

aduh95 commented Nov 29, 2024

Copy link
Copy Markdown
Contributor

CI is failing

Path: parallel/test-fs-readdir-types-symlinks
Error: --- stderr ---
node:internal/errors:540
      throw error;
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Array
    at Object.join (node:path:1261:7)
    at handleFilePaths (node:fs:1458:35)
    at processReaddirResult (node:fs:1437:93)
    at read (node:fs:1497:5)
    at readdirSyncRecursive (node:fs:1505:5)
    at Object.readdirSync (node:fs:1576:12)
    at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-fs-readdir-types-symlinks.js:34:6)
    at Module._compile (node:internal/modules/cjs/loader:1566:14)
    at Object..js (node:internal/modules/cjs/loader:1718:10)
    at Module.load (node:internal/modules/cjs/loader:1305:32) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v24.0.0-pre
Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-fs-readdir-types-symlinks.js
=== release test-fs-readdir-recursive ===
Path: sequential/test-fs-readdir-recursive
Error: --- stderr ---
node:internal/errors:540
      throw error;
      ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Array
    at Object.join (node:path:1261:7)
    at handleFilePaths (node:fs:1458:35)
    at processReaddirResult (node:fs:1437:93)
    at read (node:fs:1497:5)
    at readdirSyncRecursive (node:fs:1505:5)
    at Object.readdirSync (node:fs:1576:12)
    at Object.<anonymous> (/home/runner/work/node/node/test/sequential/test-fs-readdir-recursive.js:156:21)
    at Module._compile (node:internal/modules/cjs/loader:1566:14)
    at Object..js (node:internal/modules/cjs/loader:1718:10)
    at Module.load (node:internal/modules/cjs/loader:1305:32) {
  code: 'ERR_INVALID_ARG_TYPE'
}

@RafaelGSS

Copy link
Copy Markdown
Member Author

Thanks @aduh95. I forgot the readdir tests runs on sequential. I'll check

@juanarbol juanarbol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

@RafaelGSS RafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 3, 2024
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 3, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment thread lib/fs.js
Comment on lines +1503 to 1505
for (let i = 0; i < context.pathsQueue.length; i++) {
read(context.pathsQueue[i]);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is somewhat out of scope of the PR, I just wondered if we want to limit the amount of reads triggered to an upper bound of available file descriptors? That way we would not allocate anything before it's needed and it would probably reduce memory and increase performance a tad.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that makes sense. But I wouldn't include it on this PR as it just fixes an async behavior. Currently, fs.readdir() - async performs one operation per time:

-> readdir -> cb -> readdir -> cb ...

But at some point, we could read from parallel folders within the same callback, which would improve performance significantly (memory would increase, though).

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@RafaelGSS RafaelGSS added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 4, 2024
@juanarbol juanarbol added commit-queue Add this label to land a pull request using GitHub Actions. and removed needs-ci PRs that need a full CI run. labels Dec 5, 2024
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Dec 5, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator
Commit Queue failed
- Loading data for nodejs/node/pull/56041
✔  Done loading data for nodejs/node/pull/56041
----------------------------------- PR info ------------------------------------
Title      lib: fix fs.readdir recursive async (#56041)
Author     Rafael Gonzaga <rafael.nunu@hotmail.com> (@RafaelGSS)
Branch     RafaelGSS:fix-readdir-async-throw -> nodejs:main
Labels     fs, author ready
Commits    3
 - lib: fix fs.readdir recursive async
 - fixup! lib: fix fs.readdir recursive async
 - fixup! fixup! lib: fix fs.readdir recursive async
Committers 1
 - RafaelGSS <rafael.nunu@hotmail.com>
PR-URL: https://github.com/nodejs/node/pull/56041
Fixes: https://github.com/nodejs/node/issues/56006
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/56041
Fixes: https://github.com/nodejs/node/issues/56006
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
--------------------------------------------------------------------------------
   ⚠  Commits were pushed since the last approving review:
   ⚠  - fixup! fixup! lib: fix fs.readdir recursive async
   ℹ  This PR was created on Wed, 27 Nov 2024 20:05:17 GMT
   ✔  Approvals: 2
   ✔  - Ethan Arrowood (@Ethan-Arrowood): https://github.com/nodejs/node/pull/56041#pullrequestreview-2470295506
   ✔  - Juan José Arboleda (@juanarbol): https://github.com/nodejs/node/pull/56041#pullrequestreview-2470693898
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2024-12-04T20:46:30Z: https://ci.nodejs.org/job/node-test-pull-request/63877/
- Querying data for job/node-test-pull-request/63877/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/12172531267

@juanarbol juanarbol added commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Dec 5, 2024
@RafaelGSS

RafaelGSS commented Dec 5, 2024

Copy link
Copy Markdown
Member Author

Could someone re-approve? I pushed 16a29cb after approvals.

@aduh95 aduh95 added the needs-ci PRs that need a full CI run. label Dec 5, 2024
@aduh95 aduh95 merged commit 53356c3 into nodejs:main Dec 5, 2024
@aduh95

aduh95 commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

Landed in 53356c3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.readdir(path, { recursive: true }) is sync

10 participants