fs: cancel in-flight stat on abort

Signed-off-by: Mert Can Altin <mertgold60@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/63142
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
This commit is contained in:
Mert Can Altin authored and Antoine du Hamel committed 2026-09-15 10:49:17 +02:00
1 parent ed1b629901
commit ea372bf9c9
3 files changed
+48

No files matched your search

+1
View File
@@ -395,6 +395,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;
+8
View File
@@ -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),
@@ -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);
@@ -0,0 +1,39 @@
'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 { spawnSyncAndExitWithoutError } = require('../common/child_process');
if (process.argv[2] !== 'child') {
spawnSyncAndExitWithoutError(
process.execPath,
['--expose-internals', __filename, 'child'],
{ env: { ...process.env, UV_THREADPOOL_SIZE: '1' } },
);
return;
}
// A single worker is what keeps the stat queued behind the blocker below.
assert.strictEqual(process.env.UV_THREADPOOL_SIZE, '1');
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();