GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: stop recursive mkdir from retrying ENOENT forever · nodejs/node@13b4c23

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_file-inl.h
    • node_file.cc
    • node_file.h
  • test/parallel
    • test-fs-mkdir-recursive-enoent.js
    • test-heapdump-fs-mkdir-recursive.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_file-inl.h‎

Copy file name to clipboardExpand all lines: src/node_file-inl.h
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "node_file.h"
77
#include "req_wrap-inl.h"
88

9+
#include <algorithm>
10+
911
namespace node {
1012
namespace fs {
1113

@@ -27,6 +29,15 @@ void FSContinuationData::MaybeSetFirstPath(const std::string& path) {
2729
}
2830
}
2931

32+
bool FSContinuationData::ShouldRetryENOENT(const std::string& path) {
33+
if (std::find(enoent_paths_.begin(), enoent_paths_.end(), path) !=
34+
enoent_paths_.end()) {
35+
return false;
36+
}
37+
enoent_paths_.push_back(path);
38+
return true;
39+
}
40+
3041
std::string FSContinuationData::PopPath() {
3142
CHECK(!paths_.empty());
3243
std::string path = std::move(paths_.back());
Collapse file

‎src/node_file.cc‎

Copy file name to clipboardExpand all lines: src/node_file.cc
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ typedef void (*uv_fs_callback_t)(uv_fs_t*);
207207

208208
void FSContinuationData::MemoryInfo(MemoryTracker* tracker) const {
209209
tracker->TrackField("paths", paths_);
210+
tracker->TrackField("enoent_paths", enoent_paths_);
210211
}
211212

212213
FileHandleReadWrap::~FileHandleReadWrap() = default;
@@ -1948,6 +1949,9 @@ int MKDirpSync(uv_loop_t* loop,
19481949
std::string dirname =
19491950
next_path.substr(0, next_path.find_last_of(kPathSeparator));
19501951
if (dirname != next_path) {
1952+
if (!req_wrap->continuation_data()->ShouldRetryENOENT(next_path)) {
1953+
return err;
1954+
}
19511955
req_wrap->continuation_data()->PushPath(std::move(next_path));
19521956
req_wrap->continuation_data()->PushPath(std::move(dirname));
19531957
} else if (req_wrap->continuation_data()->paths().empty()) {
@@ -2029,6 +2033,10 @@ int MKDirpAsync(
20292033
std::string dirname =
20302034
path.substr(0, path.find_last_of(kPathSeparator));
20312035
if (dirname != path) {
2036+
if (!req_wrap->continuation_data()->ShouldRetryENOENT(path)) {
2037+
req_wrap->continuation_data()->Done(err);
2038+
break;
2039+
}
20322040
req_wrap->continuation_data()->PushPath(path);
20332041
req_wrap->continuation_data()->PushPath(std::move(dirname));
20342042
} else if (req_wrap->continuation_data()->paths().empty()) {
Collapse file

‎src/node_file.h‎

Copy file name to clipboardExpand all lines: src/node_file.h
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class FSContinuationData : public MemoryRetainer {
114114
inline std::string PopPath();
115115
// Used by mkdirp to track the first path created:
116116
inline void MaybeSetFirstPath(const std::string& path);
117+
// Used by mkdirp to retry a path that failed with ENOENT only once: its
118+
// parent has been created or checked by then, so retrying again cannot help.
119+
inline bool ShouldRetryENOENT(const std::string& path);
117120
inline void Done(int result);
118121

119122
int mode() const { return mode_; }
@@ -129,6 +132,7 @@ class FSContinuationData : public MemoryRetainer {
129132
uv_fs_t* req_;
130133
int mode_;
131134
std::vector<std::string> paths_;
135+
std::vector<std::string> enoent_paths_;
132136
std::string first_path_;
133137
};
134138

Collapse file

‎test/parallel/test-fs-mkdir-recursive-enoent.js‎

Copy file name to clipboard
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
// Refs: https://github.com/nodejs/node/issues/66268
3+
// On procfs, mkdir fails with ENOENT although the parent exists. A recursive
4+
// mkdir must report the error instead of retrying forever.
5+
const common = require('../common');
6+
const fs = require('fs');
7+
8+
if (!common.isLinux) common.skip('procfs is Linux only');
9+
10+
const assert = require('assert');
11+
const dir = `/proc/node-test-${process.pid}`;
12+
const expected = { code: 'ENOENT', syscall: 'mkdir' };
13+
14+
assert.throws(() => fs.mkdirSync(dir, { recursive: true }), expected);
15+
16+
fs.mkdir(dir, { recursive: true }, common.mustCall((err) => {
17+
assert.strictEqual(err.code, expected.code);
18+
assert.strictEqual(err.syscall, expected.syscall);
19+
}));
20+
21+
assert.rejects(fs.promises.mkdir(dir, { recursive: true }), expected)
22+
.then(common.mustCall());
Collapse file

‎test/parallel/test-heapdump-fs-mkdir-recursive.js‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
// This tests heap snapshot integration of a pending recursive mkdir.
4+
5+
const common = require('../common');
6+
const tmpdir = require('../common/tmpdir');
7+
const { validateByRetainingPath } = require('../common/heap');
8+
const assert = require('assert');
9+
const fs = require('fs');
10+
11+
tmpdir.refresh();
12+
13+
{
14+
const nodes = validateByRetainingPath('Node / FSContinuationData', []);
15+
assert.strictEqual(nodes.length, 0);
16+
}
17+
18+
fs.mkdir(tmpdir.resolve('a', 'b'), { recursive: true }, common.mustSucceed());
19+
20+
{
21+
const nodes = validateByRetainingPath('Node / FSContinuationData', []);
22+
assert.strictEqual(nodes.length, 1);
23+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.