From 6eda874be0e11719ba985ce327b23f2c5655f40f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 28 Nov 2021 12:53:43 -0800 Subject: [PATCH] fs: propagate abortsignal reason in new AbortSignal constructors Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/41008 Reviewed-By: Ruben Bridgewater Reviewed-By: Robert Nagy Reviewed-By: Benjamin Gruenbaum --- lib/fs.js | 4 ++-- lib/internal/fs/promises.js | 2 +- lib/internal/fs/read_file_context.js | 3 ++- lib/internal/fs/watchers.js | 6 +++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 86fb163f2a428c..aa69abb38a29d0 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -350,7 +350,7 @@ function readFileAfterStat(err, stats) { function checkAborted(signal, callback) { if (signal?.aborted) { - callback(new AbortError()); + callback(new AbortError(undefined, { cause: signal?.reason })); return true; } return false; @@ -2050,7 +2050,7 @@ function lutimesSync(path, atime, mtime) { function writeAll(fd, isUserFd, buffer, offset, length, signal, callback) { if (signal?.aborted) { - const abortError = new AbortError(); + const abortError = new AbortError(undefined, { cause: signal?.reason }); if (isUserFd) { callback(abortError); } else { diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 5b3482b99b26ee..b92f189c54c1aa 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -370,7 +370,7 @@ async function fsCall(fn, handle, ...args) { function checkAborted(signal) { if (signal?.aborted) - throw new AbortError(); + throw new AbortError(undefined, { cause: signal?.reason }); } async function writeFileHandle(filehandle, data, signal, encoding) { diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index afa4b7852f6790..55ad6d767ecf7d 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -88,7 +88,8 @@ class ReadFileContext { let length; if (this.signal?.aborted) { - return this.close(new AbortError()); + return this.close( + new AbortError(undefined, { cause: this.signal?.reason })); } if (this.size === 0) { buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength); diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index b45af42d12ff7d..f86860f8fc0aed 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -316,13 +316,13 @@ async function* watch(filename, options = {}) { } if (signal?.aborted) - throw new AbortError(); + throw new AbortError(undefined, { cause: signal?.reason }); const handle = new FSEvent(); let { promise, resolve, reject } = createDeferredPromise(); const oncancel = () => { handle.close(); - reject(new AbortError()); + reject(new AbortError(undefined, { cause: signal?.reason })); }; try { @@ -361,7 +361,7 @@ async function* watch(filename, options = {}) { yield await promise; ({ promise, resolve, reject } = createDeferredPromise()); } - throw new AbortError(); + throw new AbortError(undefined, { cause: signal?.reason }); } finally { handle.close(); signal?.removeEventListener('abort', oncancel);