Skip to content

Commit

Permalink
fixup! fs: fix FileHandle::ClosePromise to return persisted Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jul 14, 2021
1 parent 489b6c5 commit 3e8ba52
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,12 @@ MaybeLocal<Promise> FileHandle::ClosePromise() {
EscapableHandleScope scope(isolate);
Local<Context> context = env()->context();

Local<Promise::Resolver> close_resolver = close_promise_.Get(isolate);
if (!close_resolver.IsEmpty())
Local<Value> close_resolver =
object()->GetInternalField(FileHandle::kClosingPromiseSlot);
if (!close_resolver.IsEmpty() && !close_resolver->IsUndefined()) {
CHECK(close_resolver->IsPromise());
return close_resolver.As<Promise>();
}

CHECK(!closed_);
CHECK(!closing_);
Expand All @@ -365,7 +368,7 @@ MaybeLocal<Promise> FileHandle::ClosePromise() {
return MaybeLocal<Promise>();
}
closing_ = true;
close_promise_.Reset(isolate, resolver);
object()->SetInternalField(FileHandle::kClosingPromiseSlot, promise);

CloseReq* req = new CloseReq(env(), close_req_obj, promise, object());
auto AfterClose = uv_fs_callback_t{[](uv_fs_t* req) {
Expand Down Expand Up @@ -2542,7 +2545,7 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(fd, "close", FileHandle::Close);
env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD);
Local<ObjectTemplate> fdt = fd->InstanceTemplate();
fdt->SetInternalFieldCount(StreamBase::kInternalFieldCount);
fdt->SetInternalFieldCount(FileHandle::kInternalFieldCount);
StreamBase::AddMethods(env, fd);
env->SetConstructorFunction(target, "FileHandle", fd);
env->set_fd_constructor_template(fdt);
Expand Down
8 changes: 6 additions & 2 deletions src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ class FileHandleReadWrap final : public ReqWrap<uv_fs_t> {
// the object is garbage collected
class FileHandle final : public AsyncWrap, public StreamBase {
public:
enum InternalFields {
kModuleWrapBaseField = StreamBase::kInternalFieldCount,
kClosingPromiseSlot,
kInternalFieldCount
};

static FileHandle* New(BindingData* binding_data,
int fd,
v8::Local<v8::Object> obj = v8::Local<v8::Object>());
Expand Down Expand Up @@ -351,8 +357,6 @@ class FileHandle final : public AsyncWrap, public StreamBase {
BaseObjectPtr<FileHandleReadWrap> current_read_;

BaseObjectPtr<BindingData> binding_data_;

v8::Global<v8::Promise::Resolver> close_promise_{};
};

int MKDirpSync(uv_loop_t* loop,
Expand Down

0 comments on commit 3e8ba52

Please sign in to comment.