GeezerVison is showing github.com — fs: stop recursive mkdir from retrying ENOENT forever · nodejs/node@f8330e1
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 |
|---|
@@ -219,6 +219,7 @@ typedef void (*uv_fs_callback_t)(uv_fs_t*); | 219 | 219 |
| 220 | 220 | void FSContinuationData::MemoryInfo(MemoryTracker* tracker) const { | 221 | 221 | tracker->TrackField("paths", paths_); | | 222 | + tracker->TrackField("enoent_paths", enoent_paths_); | 222 | 223 | } | 223 | 224 |
| 224 | 225 | FileHandleReadWrap::~FileHandleReadWrap() = default; | @@ -1966,6 +1967,9 @@ int MKDirpSync(uv_loop_t* loop, | 1966 | 1967 | std::string dirname = | 1967 | 1968 | next_path.substr(0, next_path.find_last_of(kPathSeparator)); | 1968 | 1969 | if (dirname != next_path) { | | 1970 | + if (!req_wrap->continuation_data()->ShouldRetryENOENT(next_path)) { | | 1971 | + return err; | | 1972 | + } | 1969 | 1973 | req_wrap->continuation_data()->PushPath(std::move(next_path)); | 1970 | 1974 | req_wrap->continuation_data()->PushPath(std::move(dirname)); | 1971 | 1975 | } else if (req_wrap->continuation_data()->paths().empty()) { | @@ -2047,6 +2051,10 @@ int MKDirpAsync( | 2047 | 2051 | std::string dirname = | 2048 | 2052 | path.substr(0, path.find_last_of(kPathSeparator)); | 2049 | 2053 | if (dirname != path) { | | 2054 | + if (!req_wrap->continuation_data()->ShouldRetryENOENT(path)) { | | 2055 | + req_wrap->continuation_data()->Done(err); | | 2056 | + break; | | 2057 | + } | 2050 | 2058 | req_wrap->continuation_data()->PushPath(path); | 2051 | 2059 | req_wrap->continuation_data()->PushPath(std::move(dirname)); | 2052 | 2060 | } 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 +28Lines changed: 28 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 | +try { | | 15 | + fs.mkdirSync(dir); | | 16 | +} catch (err) { | | 17 | + if (err.code !== 'ENOENT') common.skip(`mkdir under /proc fails with ${err.code}`); | | 18 | +} | | 19 | + | | 20 | +assert.throws(() => fs.mkdirSync(dir, { recursive: true }), expected); | | 21 | + | | 22 | +fs.mkdir(dir, { recursive: true }, common.mustCall((err) => { | | 23 | + assert.strictEqual(err.code, expected.code); | | 24 | + assert.strictEqual(err.syscall, expected.syscall); | | 25 | +})); | | 26 | + | | 27 | +assert.rejects(fs.promises.mkdir(dir, { recursive: true }), expected) | | 28 | + .then(common.mustCall()); |
|