GeezerVison is showing github.com — fs: stop recursive mkdir from retrying ENOENT forever · nodejs/node@13b4c23
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_file-inl.h +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | #include "node_file.h" | 7 | 7 | #include "req_wrap-inl.h" | 8 | 8 |
| | 9 | +#include <algorithm> | | 10 | + | 9 | 11 | namespace node { | 10 | 12 | namespace fs { | 11 | 13 |
| @@ -27,6 +29,15 @@ void FSContinuationData::MaybeSetFirstPath(const std::string& path) { | 27 | 29 | } | 28 | 30 | } | 29 | 31 |
| | 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 | + | 30 | 41 | std::string FSContinuationData::PopPath() { | 31 | 42 | CHECK(!paths_.empty()); | 32 | 43 | std::string path = std::move(paths_.back()); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -207,6 +207,7 @@ typedef void (*uv_fs_callback_t)(uv_fs_t*); | 207 | 207 |
| 208 | 208 | void FSContinuationData::MemoryInfo(MemoryTracker* tracker) const { | 209 | 209 | tracker->TrackField("paths", paths_); | | 210 | + tracker->TrackField("enoent_paths", enoent_paths_); | 210 | 211 | } | 211 | 212 |
| 212 | 213 | FileHandleReadWrap::~FileHandleReadWrap() = default; | @@ -1948,6 +1949,9 @@ int MKDirpSync(uv_loop_t* loop, | 1948 | 1949 | std::string dirname = | 1949 | 1950 | next_path.substr(0, next_path.find_last_of(kPathSeparator)); | 1950 | 1951 | if (dirname != next_path) { | | 1952 | + if (!req_wrap->continuation_data()->ShouldRetryENOENT(next_path)) { | | 1953 | + return err; | | 1954 | + } | 1951 | 1955 | req_wrap->continuation_data()->PushPath(std::move(next_path)); | 1952 | 1956 | req_wrap->continuation_data()->PushPath(std::move(dirname)); | 1953 | 1957 | } else if (req_wrap->continuation_data()->paths().empty()) { | @@ -2029,6 +2033,10 @@ int MKDirpAsync( | 2029 | 2033 | std::string dirname = | 2030 | 2034 | path.substr(0, path.find_last_of(kPathSeparator)); | 2031 | 2035 | if (dirname != path) { | | 2036 | + if (!req_wrap->continuation_data()->ShouldRetryENOENT(path)) { | | 2037 | + req_wrap->continuation_data()->Done(err); | | 2038 | + break; | | 2039 | + } | 2032 | 2040 | req_wrap->continuation_data()->PushPath(path); | 2033 | 2041 | req_wrap->continuation_data()->PushPath(std::move(dirname)); | 2034 | 2042 | } else if (req_wrap->continuation_data()->paths().empty()) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,6 +114,9 @@ class FSContinuationData : public MemoryRetainer { | 114 | 114 | inline std::string PopPath(); | 115 | 115 | // Used by mkdirp to track the first path created: | 116 | 116 | 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); | 117 | 120 | inline void Done(int result); | 118 | 121 |
| 119 | 122 | int mode() const { return mode_; } | @@ -129,6 +132,7 @@ class FSContinuationData : public MemoryRetainer { | 129 | 132 | uv_fs_t* req_; | 130 | 133 | int mode_; | 131 | 134 | std::vector<std::string> paths_; | | 135 | + std::vector<std::string> enoent_paths_; | 132 | 136 | std::string first_path_; | 133 | 137 | }; | 134 | 138 |
| |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|