Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abort reason to AbortSignal #1027

Merged
merged 20 commits into from
Nov 8, 2021
Merged

Add abort reason to AbortSignal #1027

merged 20 commits into from
Nov 8, 2021

Conversation

nidhijaju
Copy link
Contributor

@nidhijaju nidhijaju commented Oct 18, 2021

Based on the discussion in whatwg/streams#1165, it was suggested that we add an abort reason property to AbortSignal. This PR makes the initial changes to the base primitives in the DOM spec, before we make changes to all other related specs that use the AbortSignal.

@domenic Is it okay if you please help take a look?
/cc @yutakahirano


Preview | Diff

@yutakahirano
Copy link
Member

With setting nits aside, I have a question - when no reason is given to AbortController.abort(), should AbortController.reason be undefined, or an AbortError? I think undefined is simpler and I prefer the option if there is no reason to create a new AbortError.

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With setting nits aside, I have a question - when no reason is given to AbortController.abort(), should AbortController.reason be undefined, or an AbortError? I think undefined is simpler and I prefer the option if there is no reason to create a new AbortError.

I think it's important to create a new AbortError so that author code can do something like

async function doSomething({ signal }) {
  if (signal.aborted) {
    throw signal.reason;
  }
}

to propagate the error as expected. This is also how I would expect us to write the other specs, e.g. Fetch would say something like "If signal is aborted, then reject promise with signal's abort reason."

dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
@domenic
Copy link
Member

domenic commented Oct 18, 2021

Hmm, I wonder if the property should be signal.reason or signal.abortReason... Given that AbortSignal is being used as a base class for things like TaskSignal, and that the variable is usually named signal instead of abortSignal, spelling it out as abortReason might be a good idea.

@nidhijaju
Copy link
Contributor Author

I don't have strong opinions about signal.reason vs signal.abortReason, but I was thinking that if someone uses an AbortSignal object, despite it being called signal, they would still know it is related to "aborting" which is why I thought signal.reason might be okay. However, if you think signal.abortReason is clearer, then I can change it to that too 😊

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main logic looks great now.

I'm still interested to gather more opinions on reason vs. abortReason... maybe @jasnell can help represent other-ecosystems perspectives.

dom.bs Outdated Show resolved Hide resolved
dom.bs Show resolved Hide resolved
Copy link
Contributor

@jasnell jasnell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. It's a good idea. Having the reason default to an AbortError makes sense but I don't think it's entirely necessary.

I'll add this to both Node.js and Workers.

@nidhijaju
Copy link
Contributor Author

Thank you @jasnell! Do you have any opinions on the naming of the abort reason by the way (i.e. signal.reason vs. signal.abortReason)?

dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
@jasnell
Copy link
Contributor

jasnell commented Oct 21, 2021

I think just 'reason' for the name works

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks good except for some nits.

I think it would be good to have some of the follow-up PRs against other specifications, e.g., Fetch, ready as well (at least in draft state) to ensure it works out and the complete flow can be reviewed.

dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
nidhijaju and others added 2 commits October 21, 2021 17:12
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
dom.bs Show resolved Hide resolved
domenic pushed a commit to whatwg/streams that referenced this pull request Oct 25, 2021
Part of #1165, which discusses moving it instead to the AbortSignal's reason property per whatwg/dom#1027.
Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Excited for this as this will enable timeouts as well. As I mentioned above I think we should have the PRs against the other specifications as well (CI will complain, but that's okay) to get the full picture.

dom.bs Outdated Show resolved Hide resolved
dom.bs Outdated Show resolved Hide resolved
nidhijaju and others added 2 commits October 26, 2021 18:22
@nidhijaju
Copy link
Contributor Author

Thank you @annevk! I am working on the Streams and Fetch PRs so hopefully I can have that up soon.

miketaylr added a commit to miketaylr/web-bluetooth that referenced this pull request Nov 12, 2021
domenic pushed a commit to jsdom/jsdom that referenced this pull request Nov 12, 2021
reillyeon pushed a commit to WebBluetoothCG/web-bluetooth that referenced this pull request Nov 12, 2021
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Nov 13, 2021
…=testonly

Automatic update from web-platform-tests
DOM: tests for AbortSignal's reason

See whatwg/dom#1027 for context.
--

wpt-commits: 9c7dfd2809b6c3a209a2c4df43c1d6816e6bfc1b
wpt-pr: 31291
jamienicol pushed a commit to jamienicol/gecko that referenced this pull request Nov 15, 2021
…=testonly

Automatic update from web-platform-tests
DOM: tests for AbortSignal's reason

See whatwg/dom#1027 for context.
--

wpt-commits: 9c7dfd2809b6c3a209a2c4df43c1d6816e6bfc1b
wpt-pr: 31291
Gabisampaio pushed a commit to Gabisampaio/wpt that referenced this pull request Nov 18, 2021
kanongil added a commit to kanongil/node-1 that referenced this pull request Nov 19, 2021
Testing the rejected error is not really safe, especially if adopted
for third-party code. This also makes for a smoother transition if
node APIs adopt signal.reason.

Refs: nodejs#40692
Refs: whatwg/dom#1027
kanongil added a commit to kanongil/node-1 that referenced this pull request Nov 19, 2021
This is done to prepare user code for signal.reason, which will allow
custom errors to be thrown on aborts. Custom errors means that it
will not be possible to conclusively determine if an error is from an
`ac.abort()`, just by looking at it. By not declaring what error is
thrown, node is also free to change it to
`new DOMException(message, 'AbortError')` in a future release. This
also avoids the possible addition of a public `AbortError` to node.

The thrown errors will remain instances of the internal `AbortError`
for now, to avoid effecting existing user code.

While signal.aborted can be used to detect aborted errors in most
cases, it does fully not work for the stream pipeline API, where
individual streams are destroyed with
`stream.destroy(new AbortError())`. Here the stream can no longer
fully detect aborts. However, I don't think this is ever relevant, as
streams should always perform the same cleanup logic, regardless of
what error is passed. If it doesn't support a signal option, it does
not make sense to include logic to handle signal aborts.

Refs: nodejs#40692
Refs: nodejs#38361
Refs: whatwg/dom#1027
domenic pushed a commit to whatwg/streams that referenced this pull request Nov 19, 2021
Following on from whatwg/dom#1027, with introduces custom abort reasons, this uses the AbortSignal's abort reason in the ReadableStreamPipeTo function. It also updates WritableStreamAbort to use the reason to signal abort.

Closes #1165. Part of whatwg/dom#1030.
blueboxd pushed a commit to blueboxd/chromium-legacy that referenced this pull request Nov 25, 2021
This CL follows whatwg/dom#1027, which adds
an abort reason property[1] to DOM's AbortSignal API (i.e. signal.reason).
This change also removes the aborted flag from the AbortSignal, and
instead uses the "aborted" predicate[2] to do similar checks.

[1] https://dom.spec.whatwg.org/#abortsignal-abort-reason
[2] https://dom.spec.whatwg.org/#abortsignal-aborted

Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/T6B5czAke1I

Bug: 1263410
Change-Id: I25beb697d8e73dc73fdce2bc0d24b5e1bd52b78e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3293551
Reviewed-by: Kent Tamura <tkent@chromium.org>
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Nidhi Jaju <nidhijaju@chromium.org>
Cr-Commit-Position: refs/heads/main@{#945295}
@domenic domenic added the topic: aborting AbortController and AbortSignal label May 19, 2022
annevk pushed a commit to whatwg/fetch that referenced this pull request Oct 5, 2022
This is a follow-up to whatwg/dom#1027 ensuring that the fetch algorithm forwards the abort reason appropriately.

Tests: web-platform-tests/wpt#35374.
mjfroman pushed a commit to mjfroman/moz-libwebrtc-third-party that referenced this pull request Oct 14, 2022
This CL follows whatwg/dom#1027, which adds
an abort reason property[1] to DOM's AbortSignal API (i.e. signal.reason).
This change also removes the aborted flag from the AbortSignal, and
instead uses the "aborted" predicate[2] to do similar checks.

[1] https://dom.spec.whatwg.org/#abortsignal-abort-reason
[2] https://dom.spec.whatwg.org/#abortsignal-aborted

Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/T6B5czAke1I

Bug: 1263410
Change-Id: I25beb697d8e73dc73fdce2bc0d24b5e1bd52b78e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3293551
Reviewed-by: Kent Tamura <tkent@chromium.org>
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Nidhi Jaju <nidhijaju@chromium.org>
Cr-Commit-Position: refs/heads/main@{#945295}
NOKEYCHECK=True
GitOrigin-RevId: 155b2353a12c9e0173b8d33b32b115a2406a8131
@shaseley shaseley mentioned this pull request Apr 12, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: aborting AbortController and AbortSignal
Development

Successfully merging this pull request may close these issues.

None yet

7 participants