| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 182aaf5 commit 12391c7
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -197,8 +197,8 @@ static MaybeLocal<Array> DirentListToArray( | |||
| 197 | 197 | } | |
| 198 | 198 | ||
| 199 | 199 | static void AfterDirRead(uv_fs_t* req) { | |
| 200 | - FSReqBase* req_wrap = FSReqBase::from_req(req); | ||
| 201 | - FSReqAfterScope after(req_wrap, req); | ||
| 200 | + BaseObjectPtr<FSReqBase> req_wrap { FSReqBase::from_req(req) }; | ||
| 201 | + FSReqAfterScope after(req_wrap.get(), req); | ||
| 202 | 202 | ||
| 203 | 203 | if (!after.Proceed()) { | |
| 204 | 204 | return; | |
@@ -210,12 +210,12 @@ static void AfterDirRead(uv_fs_t* req) { | |||
| 210 | 210 | if (req->result == 0) { | |
| 211 | 211 | // Done | |
| 212 | 212 | Local<Value> done = Null(isolate); | |
| 213 | + after.Clear(); | ||
| 213 | 214 | req_wrap->Resolve(done); | |
| 214 | 215 | return; | |
| 215 | 216 | } | |
| 216 | 217 | ||
| 217 | 218 | uv_dir_t* dir = static_cast<uv_dir_t*>(req->ptr); | |
| 218 | - req->ptr = nullptr; | ||
| 219 | 219 | ||
| 220 | 220 | Local<Value> error; | |
| 221 | 221 | Local<Array> js_array; | |
@@ -224,9 +224,13 @@ static void AfterDirRead(uv_fs_t* req) { | |||
| 224 | 224 | req->result, | |
| 225 | 225 | req_wrap->encoding(), | |
| 226 | 226 | &error).ToLocal(&js_array)) { | |
| 227 | + // Clear libuv resources *before* delivering results to JS land because | ||
| 228 | + // that can schedule another operation on the same uv_dir_t. Ditto below. | ||
| 229 | + after.Clear(); | ||
| 227 | 230 | return req_wrap->Reject(error); | |
| 228 | 231 | } | |
| 229 | 232 | ||
| 233 | + after.Clear(); | ||
| 230 | 234 | req_wrap->Resolve(js_array); | |
| 231 | 235 | } | |
| 232 | 236 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -556,8 +556,15 @@ FSReqAfterScope::FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req) | |||
| 556 | 556 | } | |
| 557 | 557 | ||
| 558 | 558 | FSReqAfterScope::~FSReqAfterScope() { | |
| 559 | + Clear(); | ||
| 560 | + } | ||
| 561 | + | ||
| 562 | + void FSReqAfterScope::Clear() { | ||
| 563 | + if (!wrap_) return; | ||
| 564 | + | ||
| 559 | 565 | uv_fs_req_cleanup(wrap_->req()); | |
| 560 | - delete wrap_; | ||
| 566 | + wrap_->Detach(); | ||
| 567 | + wrap_.reset(); | ||
| 561 | 568 | } | |
| 562 | 569 | ||
| 563 | 570 | // TODO(joyeecheung): create a normal context object, and | |
@@ -570,12 +577,16 @@ FSReqAfterScope::~FSReqAfterScope() { | |||
| 570 | 577 | // which is also why the errors should have been constructed | |
| 571 | 578 | // in JS for more flexibility. | |
| 572 | 579 | void FSReqAfterScope::Reject(uv_fs_t* req) { | |
| 573 | - wrap_->Reject(UVException(wrap_->env()->isolate(), | ||
| 574 | - req->result, | ||
| 575 | - wrap_->syscall(), | ||
| 576 | - nullptr, | ||
| 577 | - req->path, | ||
| 578 | - wrap_->data())); | ||
| 580 | + BaseObjectPtr<FSReqBase> wrap { wrap_ }; | ||
| 581 | + Local<Value> exception = | ||
| 582 | + UVException(wrap_->env()->isolate(), | ||
| 583 | + req->result, | ||
| 584 | + wrap_->syscall(), | ||
| 585 | + nullptr, | ||
| 586 | + req->path, | ||
| 587 | + wrap_->data()); | ||
| 588 | + Clear(); | ||
| 589 | + wrap->Reject(exception); | ||
| 579 | 590 | } | |
| 580 | 591 | ||
| 581 | 592 | bool FSReqAfterScope::Proceed() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -184,6 +184,7 @@ class FSReqAfterScope final { | |||
| 184 | 184 | public: | |
| 185 | 185 | FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req); | |
| 186 | 186 | ~FSReqAfterScope(); | |
| 187 | + void Clear(); | ||
| 187 | 188 | ||
| 188 | 189 | bool Proceed(); | |
| 189 | 190 | ||
@@ -195,7 +196,7 @@ class FSReqAfterScope final { | |||
| 195 | 196 | FSReqAfterScope& operator=(const FSReqAfterScope&&) = delete; | |
| 196 | 197 | ||
| 197 | 198 | private: | |
| 198 | - FSReqBase* wrap_ = nullptr; | ||
| 199 | + BaseObjectPtr<FSReqBase> wrap_; | ||
| 199 | 200 | uv_fs_t* req_ = nullptr; | |
| 200 | 201 | v8::HandleScope handle_scope_; | |
| 201 | 202 | v8::Context::Scope context_scope_; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments