Skip to content

Set FileReader to LOADING state while a read is in progress - #57375

Closed
durvesh1992 wants to merge 1 commit into
react:mainfrom
durvesh1992:fix-filereader-loading-abort
Closed

Set FileReader to LOADING state while a read is in progress#57375
durvesh1992 wants to merge 1 commit into
react:mainfrom
durvesh1992:fix-filereader-loading-abort

Conversation

@durvesh1992

Copy link
Copy Markdown
Contributor

Summary

FileReader's readAsText / readAsDataURL / readAsArrayBuffer go straight from EMPTY to DONE — they never set readyState to LOADING. Two consequences:

  1. readyState is never observably LOADING during a read (it should be, per the W3C FileReader spec).

  2. abort() is dead during a read. Its body only emits events when the reader is mid-read:

    abort() {
      this._aborted = true;
      // only call onreadystatechange if there is something to abort, as per spec
      if (this._readyState !== EMPTY && this._readyState !== DONE) {
        this._reset();
        this._setReadyState(DONE);
      }
      this._reset();
    }

    Because a read never sets LOADING, readyState is still EMPTY when abort() runs, so the guard is false and no abort/loadend events are dispatched — even though the method is written specifically to handle that case.

Fix

Set the LOADING state when each read starts (via the existing _setReadyState helper, which also fires readystatechange). This makes readyState correct during a read and makes abort() during a read dispatch abort/loadend as its own code already anticipates.

Test plan

Added tests to Libraries/Blob/__tests__/FileReader-test.js:

  • readyState is LOADING synchronously after a read starts.
  • abort() during a read dispatches abort and loadend.

Both fail before this change and pass after; the existing read tests and the full Blob test suite still pass (20/20). ESLint clean on the changed files.

Changelog:

[General] [Fixed] - Set FileReader readyState to LOADING during a read so abort() correctly emits abort/loadend

FileReader's readAs* methods went straight from EMPTY to DONE: they
never set readyState to LOADING. As a result readyState was never
observably LOADING, and abort() — which only emits events when
readyState is neither EMPTY nor DONE — silently did nothing when called
during an in-progress read, so no 'abort'/'loadend' events fired.

Set the LOADING state when each read starts (via _setReadyState, which
also fires 'readystatechange'), matching the FileReader spec and making
abort() during a read work as its own code already anticipated.

Add tests for the LOADING state and for abort() dispatching abort/loadend
during a read.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 30, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jun 30, 2026
@meta-codesync

meta-codesync Bot commented Jun 30, 2026

Copy link
Copy Markdown

@Abbondanzo has imported this pull request. If you are a Meta employee, you can view this in D110191434.

@meta-codesync meta-codesync Bot closed this in c00813c Jun 30, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Jun 30, 2026
@meta-codesync

meta-codesync Bot commented Jun 30, 2026

Copy link
Copy Markdown

@Abbondanzo merged this pull request in c00813c.

meta-codesync Bot pushed a commit that referenced this pull request Jul 28, 2026
Summary:
Before #57375, reads never entered `LOADING`, so the active abort path was effectively unreachable. Now that reads use `LOADING`, that path calls `_reset()` before and after transitioning to `DONE`, leaving the reader in `EMPTY` after `abort()` returns.

This differs from the [File API abort algorithm](https://w3c.github.io/FileAPI/#dfn-abort), which keeps an active reader in `DONE`. The final reset also overwrites a replacement read started by an `abort` handler, and that new read clears the shared `_aborted` flag before the canceled native promise settles.

This change:

- keeps an aborted active reader in `DONE` with a null result
- skips the old `loadend` if the abort handler starts another read
- gives each read an ID so a canceled native promise cannot complete over a newer read

## Changelog:

[GENERAL] [FIXED] - Keep `FileReader` in the correct state after aborting a read.

Pull Request resolved: #57692

Test Plan:
- `yarn jest packages/react-native/Libraries/Blob/__tests__/FileReader-test.js --runInBand`
- `./node_modules/.bin/prettier --check packages/react-native/Libraries/Blob/FileReader.js packages/react-native/Libraries/Blob/__tests__/FileReader-test.js`
- `./node_modules/.bin/eslint --max-warnings 0 packages/react-native/Libraries/Blob/FileReader.js packages/react-native/Libraries/Blob/__tests__/FileReader-test.js`
- `yarn flow-check`

Reviewed By: christophpurrer

Differential Revision: D113802292

Pulled By: Abbondanzo

fbshipit-source-id: d5c8beeaf2539f3784e7f61ba500e67da575164c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant