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

fs: add support for AbortSignal in readFile #35911

Closed
wants to merge 1 commit into from

Conversation

benjamingr
Copy link
Member

Add basic support for AbortSignal in readFile.

This adds support just to readFile and fs.promises.readFile and not anything else in FS (unless you include fileHandle.readFile).

This is because it's significantly easier to do for readFile since the cancellation doesn't have to happen in libuv land and since readFile is a really heavy and pretty common action.

cc @jasnell @mcollina @addaleax

@nodejs-github-bot nodejs-github-bot added the fs Issues and PRs related to the fs subsystem / file system. label Nov 1, 2020
@mscdex
Copy link
Contributor

mscdex commented Nov 1, 2020

It's probably worth noting what happens when a readFile() is aborted (an error is raised in one form or another) and the limitations of aborting a readFile() call (e.g. it does not abort queued requests at the libuv level, so it will always wait for the current request to finish first).

@benjamingr
Copy link
Member Author

@mscdex good suggestion, is this what you had in mind? d6100b1

@addaleax addaleax added the semver-minor PRs that contain new features and should be released in the next minor version. label Nov 1, 2020
if (DOMException === undefined)
DOMException = internalBinding('messaging').DOMException;
return new DOMException(message, name);
});
Copy link
Member

Choose a reason for hiding this comment

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

Why using a DOMException? As a user I would prefer to receive one of our error objects with a code.

Copy link
Member

Choose a reason for hiding this comment

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

This is recommended by the spec: https://dom.spec.whatwg.org/#aborting-ongoing-activities

Users are supposed to check for it with error.name === 'AbortError'

Copy link
Member

Choose a reason for hiding this comment

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

We are telling our users to check for an error code. I think that should be the way to go here as well.

Note that I'd like the error code to be specific for fs.readFile, so it's easy to debug.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mcollina this is what we use everywhere else that uses AbortSignals (like events.once and timers/promises) and everywhere in the web standard (like fetch). I feel strongly that this should be compatible with how exceptions work everywhere else.

A code is already set on DOMExceptions (as ABORT_ERR ) see internal/per_context/domexception - we can recommend the code as the way to check rather than the name in the docs if you think that's better.

Copy link
Member

Choose a reason for hiding this comment

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

I've likely missed it everywhere else. This seems really hard to debug: users will not know what this aborterror is about in a complex application.

Let's not hold this for it.. I'll open an issue when I have the time.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've likely missed it everywhere else. This seems really hard to debug: users will not know what this aborterror is about in a complex application.

Why? Would it help if this had a different .code? I am happy to set the code to something different here and reject with a subclass if you think that improves DX. I can also do that for timers (though like you said probably better in a different PX).

Note I'm not convinced DX is actually better with "different aborts cancel with different codes since by far the most common the use case I saw is checking for cancellation (in general) to ignore the error (since cancellation is not an error most times). Users can also figure where cancellation came from from the stack trace.

Let's not hold this for it.. I'll open an issue when I have the time.

Sure, note you are currently blocking - is that intentional?

@mcollina
Copy link
Member

mcollina commented Nov 2, 2020

(Sorry I made a mess from the mobile interface).

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@benjamingr benjamingr added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 4, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 4, 2020
@nodejs-github-bot
Copy link
Collaborator

@benjamingr benjamingr added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 5, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 5, 2020
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@benjamingr benjamingr added the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 5, 2020
@github-actions github-actions bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Nov 5, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2020

Commit Queue failed
- Loading data for nodejs/node/pull/35911
✔  Done loading data for nodejs/node/pull/35911
----------------------------------- PR info ------------------------------------
Title      fs: add support for AbortSignal in readFile (#35911)
   ⚠  Could not retrieve the email or name of the PR author's from user's GitHub profile!
Branch     benjamingr:readfile-abort-signal -> nodejs:master
Labels     fs, semver-minor
Commits    1
 - fs: add support for AbortSignal in readFile
Committers 1
 - Benjamin Gruenbaum 
PR-URL: https://github.com/nodejs/node/pull/35911
Reviewed-By: James M Snell 
Reviewed-By: Anna Henningsen 
Reviewed-By: Matteo Collina 
Reviewed-By: Rich Trott 
Reviewed-By: Michaël Zasso 
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/35911
Reviewed-By: James M Snell 
Reviewed-By: Anna Henningsen 
Reviewed-By: Matteo Collina 
Reviewed-By: Rich Trott 
Reviewed-By: Michaël Zasso 
--------------------------------------------------------------------------------
   ⚠  Commits were pushed since the last review:
   ⚠  - fs: add support for AbortSignal in readFile
   ✖  Last GitHub CI failed
   ℹ  Last Full PR CI on 2020-11-05T18:03:50Z: https://ci.nodejs.org/job/node-test-pull-request/34099/
- Querying data for job/node-test-pull-request/34099/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
   ℹ  This PR was created on Sun, 01 Nov 2020 16:25:00 GMT
   ✔  Approvals: 5
   ✔  - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/35911#pullrequestreview-521239795
   ✔  - Anna Henningsen (@addaleax): https://github.com/nodejs/node/pull/35911#pullrequestreview-521251093
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/35911#pullrequestreview-521540648
   ✔  - Rich Trott (@Trott) (TSC): https://github.com/nodejs/node/pull/35911#pullrequestreview-521671149
   ✔  - Michaël Zasso (@targos) (TSC): https://github.com/nodejs/node/pull/35911#pullrequestreview-521863433
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu

Commit Queue action: https://github.com/nodejs/node/actions/runs/348132668

@github-actions github-actions bot added the commit-queue-failed An error occurred while landing this pull request using GitHub Actions. label Nov 5, 2020
benjamingr added a commit that referenced this pull request Nov 6, 2020
PR-URL: #35911
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@benjamingr
Copy link
Member Author

Landed manually in b5a136c

@benjamingr benjamingr closed this Nov 6, 2020
@benjamingr benjamingr deleted the readfile-abort-signal branch November 6, 2020 11:16
danielleadams pushed a commit that referenced this pull request Nov 9, 2020
PR-URL: #35911
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@danielleadams danielleadams mentioned this pull request Nov 9, 2020
danielleadams added a commit that referenced this pull request Nov 9, 2020
Notable changes:

* events: getEventListeners static (Benjamin Gruenbaum) (#35991)
* fs: add support for AbortSignal in readFile (Benjamin Gruenbaum) (#35911)

PR URL: #36055
danielleadams added a commit that referenced this pull request Nov 10, 2020
Notable changes:

* events: getEventListeners static (Benjamin Gruenbaum) (#35991)
* fs: add support for AbortSignal in readFile (Benjamin Gruenbaum) (#35911)

PR URL: #36055
danielleadams added a commit that referenced this pull request Nov 10, 2020
Notable changes:

* events:
  * getEventListeners static (Benjamin Gruenbaum) (#35991)
* fs:
  * support abortsignal in writeFile (Benjamin Gruenbaum) (#35993)
  * add support for AbortSignal in readFile (Benjamin Gruenbaum) (#35911)
* stream:
  * fix thrown object reference (Gil Pedersen) (#36065)

PR URL: #36055
danielleadams added a commit that referenced this pull request Nov 10, 2020
Notable changes:

* events:
  * getEventListeners static (Benjamin Gruenbaum) (#35991)
* fs:
  * support abortsignal in writeFile (Benjamin Gruenbaum) (#35993)
  * add support for AbortSignal in readFile (Benjamin Gruenbaum) (#35911)
* stream:
  * fix thrown object reference (Gil Pedersen) (#36065)

PR URL: #36055
danielleadams added a commit that referenced this pull request Nov 10, 2020
Notable changes:

* events:
  * getEventListeners static (Benjamin Gruenbaum) (#35991)
* fs:
  * support abortsignal in writeFile (Benjamin Gruenbaum) (#35993)
  * add support for AbortSignal in readFile (Benjamin Gruenbaum) (#35911)
* stream:
  * fix thrown object reference (Gil Pedersen) (#36065)

PR URL: #36055
@targos targos added backport-open-v14.x and removed commit-queue-failed An error occurred while landing this pull request using GitHub Actions. labels Apr 24, 2021
targos pushed a commit to targos/node that referenced this pull request Apr 24, 2021
PR-URL: nodejs#35911
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
targos pushed a commit to targos/node that referenced this pull request Apr 26, 2021
PR-URL: nodejs#35911
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
targos pushed a commit to targos/node that referenced this pull request Apr 30, 2021
PR-URL: nodejs#35911
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
targos pushed a commit that referenced this pull request Apr 30, 2021
PR-URL: #35911
Backport-PR-URL: #38386
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@danielleadams danielleadams mentioned this pull request May 3, 2021
aduh95 added a commit to aduh95/node that referenced this pull request Sep 1, 2021
nodejs-github-bot pushed a commit that referenced this pull request Sep 6, 2021
Refs: #33716
Refs: #35993
Refs: #35911

PR-URL: #39972
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
nodejs-github-bot pushed a commit that referenced this pull request Sep 6, 2021
PR-URL: #39972
Refs: #33716
Refs: #35993
Refs: #35911
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
BethGriggs pushed a commit that referenced this pull request Sep 21, 2021
Refs: #33716
Refs: #35993
Refs: #35911

PR-URL: #39972
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants