From 6fea27f6958c72f907094fb2e662cd60e682396b Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 21 Sep 2023 15:28:23 -0400 Subject: [PATCH] fixup! fs: improve `readFileSync` with file descriptors --- src/node_file.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index f318dad6d34059f..fe102fe504cb49f 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -2552,7 +2552,6 @@ static void ReadFileUtf8(const FunctionCallbackInfo& args) { uv_file file; uv_fs_t req; - auto defer_req_cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); }); bool is_fd = args[0]->IsInt32(); @@ -2568,17 +2567,19 @@ static void ReadFileUtf8(const FunctionCallbackInfo& args) { file = uv_fs_open(nullptr, &req, *path, flags, O_RDONLY, nullptr); FS_SYNC_TRACE_END(open); if (req.result < 0) { + uv_fs_req_cleanup(&req); // req will be cleaned up by scope leave. return env->ThrowUVException(req.result, "open", nullptr, path.out()); } } - auto defer_close = OnScopeLeave([file, is_fd]() { + auto defer_close = OnScopeLeave([file, is_fd, &req]() { if (!is_fd) { - uv_fs_t close_req; - CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr)); - uv_fs_req_cleanup(&close_req); + FS_SYNC_TRACE_BEGIN(close); + CHECK_EQ(0, uv_fs_close(nullptr, &req, file, nullptr)); + FS_SYNC_TRACE_END(close); } + uv_fs_req_cleanup(&req); }); std::string result{};