Skip to content

Commit

Permalink
lib: tighten AbortSignal.prototype.throwIfAborted implementation
Browse files Browse the repository at this point in the history
PR-URL: #46521
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Apr 3, 2023
1 parent 17b4e9b commit 2b80d19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ class AbortSignal extends EventTarget {
}

throwIfAborted() {
if (this.aborted) {
throw this.reason;
validateThisAbortSignal(this);
if (this[kAborted]) {
throw this[kReason];
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-abortcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,20 @@ const { setTimeout: sleep } = require('timers/promises');
const ac = new AbortController();
ac.signal.throwIfAborted();
}

{
const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'aborted');
const actualReason = new Error();
Reflect.defineProperty(AbortSignal.prototype, 'aborted', { value: false });
throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason);
Reflect.defineProperty(AbortSignal.prototype, 'aborted', originalDesc);
}

{
const originalDesc = Reflect.getOwnPropertyDescriptor(AbortSignal.prototype, 'reason');
const actualReason = new Error();
const fakeExcuse = new Error();
Reflect.defineProperty(AbortSignal.prototype, 'reason', { value: fakeExcuse });
throws(() => AbortSignal.abort(actualReason).throwIfAborted(), actualReason);
Reflect.defineProperty(AbortSignal.prototype, 'reason', originalDesc);
}

0 comments on commit 2b80d19

Please sign in to comment.