diff --git a/src/node_dir.cc b/src/node_dir.cc index c4aaf4bcd3e8ba..b0904c3d39ddd0 100644 --- a/src/node_dir.cc +++ b/src/node_dir.cc @@ -197,8 +197,8 @@ static MaybeLocal DirentListToArray( } static void AfterDirRead(uv_fs_t* req) { - FSReqBase* req_wrap = FSReqBase::from_req(req); - FSReqAfterScope after(req_wrap, req); + BaseObjectPtr req_wrap { FSReqBase::from_req(req) }; + FSReqAfterScope after(req_wrap.get(), req); if (!after.Proceed()) { return; @@ -210,12 +210,12 @@ static void AfterDirRead(uv_fs_t* req) { if (req->result == 0) { // Done Local done = Null(isolate); + after.Clear(); req_wrap->Resolve(done); return; } uv_dir_t* dir = static_cast(req->ptr); - req->ptr = nullptr; Local error; Local js_array; @@ -224,9 +224,13 @@ static void AfterDirRead(uv_fs_t* req) { req->result, req_wrap->encoding(), &error).ToLocal(&js_array)) { + // Clear libuv resources *before* delivering results to JS land because + // that can schedule another operation on the same uv_dir_t. Ditto below. + after.Clear(); return req_wrap->Reject(error); } + after.Clear(); req_wrap->Resolve(js_array); } diff --git a/src/node_file.cc b/src/node_file.cc index be18b18fddd413..96adbc9e756688 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -556,8 +556,15 @@ FSReqAfterScope::FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req) } FSReqAfterScope::~FSReqAfterScope() { + Clear(); +} + +void FSReqAfterScope::Clear() { + if (!wrap_) return; + uv_fs_req_cleanup(wrap_->req()); - delete wrap_; + wrap_->Detach(); + wrap_.reset(); } // TODO(joyeecheung): create a normal context object, and @@ -570,12 +577,16 @@ FSReqAfterScope::~FSReqAfterScope() { // which is also why the errors should have been constructed // in JS for more flexibility. void FSReqAfterScope::Reject(uv_fs_t* req) { - wrap_->Reject(UVException(wrap_->env()->isolate(), - req->result, - wrap_->syscall(), - nullptr, - req->path, - wrap_->data())); + BaseObjectPtr wrap { wrap_ }; + Local exception = + UVException(wrap_->env()->isolate(), + req->result, + wrap_->syscall(), + nullptr, + req->path, + wrap_->data()); + Clear(); + wrap->Reject(exception); } bool FSReqAfterScope::Proceed() { diff --git a/src/node_file.h b/src/node_file.h index 7690f272848425..39c9034f669ea7 100644 --- a/src/node_file.h +++ b/src/node_file.h @@ -184,6 +184,7 @@ class FSReqAfterScope final { public: FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req); ~FSReqAfterScope(); + void Clear(); bool Proceed(); @@ -195,7 +196,7 @@ class FSReqAfterScope final { FSReqAfterScope& operator=(const FSReqAfterScope&&) = delete; private: - FSReqBase* wrap_ = nullptr; + BaseObjectPtr wrap_; uv_fs_t* req_ = nullptr; v8::HandleScope handle_scope_; v8::Context::Scope context_scope_;