Skip to content

Commit 2b80d19

Browse files
aduh95danielleadams
authored andcommitted
lib: tighten AbortSignal.prototype.throwIfAborted implementation
PR-URL: #46521 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 17b4e9b commit 2b80d19

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

‎lib/internal/abort_controller.js‎

Lines changed: 3 additions & 2 deletions
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-
throwthis.reason;
153+
validateThisAbortSignal(this);
154+
if(this[kAborted]){
155+
throwthis[kReason];
155156
}
156157
}
157158

‎test/parallel/test-abortcontroller.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,20 @@ const { setTimeout: sleep } = require('timers/promises');
254254
constac=newAbortController();
255255
ac.signal.throwIfAborted();
256256
}
257+
258+
{
259+
constoriginalDesc=Reflect.getOwnPropertyDescriptor(AbortSignal.prototype,'aborted');
260+
constactualReason=newError();
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+
constoriginalDesc=Reflect.getOwnPropertyDescriptor(AbortSignal.prototype,'reason');
268+
constactualReason=newError();
269+
constfakeExcuse=newError();
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)