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 MylesBorins committed Feb 18, 2023
1 parent f91260b commit 35570e9
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
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
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 35570e9

Please sign in to comment.