Skip to content

recursive option of fs.readdir results in blocking synchronous I/O instead of async readdir #51749

Closed as not planned
@Rob--W

Description

@Rob--W

Affected URL(s)

https://nodejs.org/api/fs.html#fspromisesreaddirpath-options

Description of the problem

The readdir method specifies a recursive option that "reads the contents of a directory recursively". This can be a potentially expensive/long-running filesystem query.

The current implementation is synchronous (see below), which means that passing recursive:true would block the calling thread until the I/O completes. Ideally, the implementation would not block the calling thread, but until that is implemented (I didn't find any open issue), the least that can be done is to update the documentation to warn about this risk.

Another aspect worth documenting is to explicitly call out that symlinks are NOT followed. If symlinks were to be followed, then that could potentially result in security issues, such as DoS (infinite readdir loop) or directory traversal outside of the specified directory.

Relevant issues / PRs that introduced this feature:

Relevant source:

node/lib/fs.js

Lines 1458 to 1467 in 544cfc5

function readdir(path, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options);
path = getValidatedPath(path);
if (options.recursive != null) {
validateBoolean(options.recursive, 'options.recursive');
}
if (options.recursive) {
callback(null, readdirSyncRecursive(path, options));

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions