Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,12 @@ void NewFSReqCallback(const FunctionCallbackInfo<Value>& args) {
new FSReqCallback(binding_data, args.This(), args[0]->IsTrue());
}

void CancelFSReq(const FunctionCallbackInfo<Value>& 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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-fs-stat-cancel-inflight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

// Aborting a queued stat must cancel the libuv request instead of running it.
// The threadpool is saturated so the request is still queued when it is
// cancelled; the queue is FIFO, so the stat cannot start before the blocker.

const common = require('../common');

if (!common.hasCrypto) common.skip('missing crypto');

const assert = require('assert');
const crypto = require('crypto');
const { spawnSync } = require('child_process');

if (process.argv[2] !== 'child') {
const result = spawnSync(
process.execPath,
['--expose-internals', __filename, 'child'],
{ env: { ...process.env, UV_THREADPOOL_SIZE: '1' }, encoding: 'utf8' },
);
assert.strictEqual(result.status, 0, result.stderr);
return;
}

const { internalBinding } = require('internal/test/binding');
const binding = internalBinding('fs');

// Occupy the only worker thread so the stat below cannot start.
crypto.pbkdf2('secret', 'salt', 500_000, 32, 'sha512', common.mustCall());

const req = new binding.FSReqCallback(false);
req.oncomplete = common.mustCall((err) => {
// Without cancellation the stat would run and report success.
assert.strictEqual(err?.code, 'ECANCELED');
});
binding.stat(__filename, false, req, true);
req.cancel();
Loading