diff --git a/lib/fs.js b/lib/fs.js index 3c0bef6cf506..92934139dc20 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -392,6 +392,7 @@ function bindSignalToReq(req, signal, callback) { let aborted = false; const onAbort = () => { aborted = true; + req.cancel(); callback(new AbortError(undefined, { cause: signal.reason })); }; kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation; diff --git a/src/node_file.cc b/src/node_file.cc index b2e765346cf1..699110f7b965 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -758,6 +758,12 @@ void NewFSReqCallback(const FunctionCallbackInfo& args) { new FSReqCallback(binding_data, args.This(), args[0]->IsTrue()); } +void CancelFSReq(const FunctionCallbackInfo& args) { + FSReqBase* req_wrap; + ASSIGN_OR_RETURN_UNWRAP(&req_wrap, args.This()); + req_wrap->Cancel(); +} + FSReqAfterScope::FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req) : wrap_(wrap), req_(req), @@ -4618,6 +4624,7 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, fst->InstanceTemplate()->SetInternalFieldCount( FSReqBase::kInternalFieldCount); fst->Inherit(AsyncWrap::GetConstructorTemplate(isolate_data)); + SetProtoMethod(isolate, fst, "cancel", CancelFSReq); SetConstructorFunction(isolate, target, "FSReqCallback", fst); // Create FunctionTemplate for FileHandleReadWrap. There’s no need @@ -4734,6 +4741,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(HandleToFd); #endif registry->Register(NewFSReqCallback); + registry->Register(CancelFSReq); registry->Register(FileHandle::New); registry->Register(FileHandle::Close);