Skip to content

Commit 2b80d19

Browse files
aduh95danielleadams
authored andcommitted
lib: tighten AbortSignal.prototype.throwIfAborted implementation
PR-URL: #46521 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 17b4e9b commit 2b80d19

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/internal/abort_controller.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ class AbortSignal extends EventTarget {
150150
}
151151

152152
throwIfAborted() {
153-
if (this.aborted) {
154-
throw this.reason;
153+
validateThisAbortSignal(this);
154+
if (this[kAborted]) {
155+
throw this[kReason];
155156
}
156157
}
157158

test/parallel/test-abortcontroller.js

+17
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,20 @@ const { setTimeout: sleep } = require('timers/promises');
254254
const ac = new AbortController();
255255
ac.signal.throwIfAborted();
256256
}
257+
258+
{
259+
const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted');
260+
const actualReason = new Error();
261+
Reflect.defineProperty(AbortSignal.prototype, 'aborted', { value: false });
262+
throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason);
263+
Reflect.defineProperty(AbortSignal.prototype, 'aborted', originalDesc);
264+
}
265+
266+
{
267+
const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'reason');
268+
const actualReason = new Error();
269+
const fakeExcuse = new Error();
270+
Reflect.defineProperty(AbortSignal.prototype, 'reason', { value: fakeExcuse });
271+
throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason);
272+
Reflect.defineProperty(AbortSignal.prototype, 'reason', originalDesc);
273+
}

0 commit comments

Comments
 (0)