Skip to content

fix(download): don't report a canceled or replaced download through onError - #1057

Draft
staszekscp wants to merge 1 commit into
wonday:masterfrom
staszekscp:fix/ignore-canceled-downloads
Draft

staszekscp wants to merge 1 commit into
wonday:masterfrom
staszekscp:fix/ignore-canceled-downloads

Conversation

@staszekscp

Copy link
Copy Markdown

When source changes while a download is still running (for example, when an auth token in the URL is refreshed), componentDidUpdate cancels the old download. Since #1019 and #1031, the stored task can be canceled, so cancel() now works. But react-native-blob-util rejects a canceled task with a CanceledFetchError, and the catch in _downloadFile passes it to onError. An app that shows an error state from onError then hides the PDF and shows a load error, even though the new source is loading fine.

The same gap lets an older download interfere with a newer one. If it finishes anyway, it can show the old file, or clear lastRNBFTask, which by then belongs to the newer download.

Changes

  • Each download gets an id. _cancelDownload() increments it and cancels the running task. A download whose id is no longer current ignores its result: it doesn't call onError, set state, or touch lastRNBFTask.
  • componentDidUpdate cancels and loads the new source right away, instead of inside the cancel callback. react-native-blob-util doesn't call that callback when the request has already finished, and with the id check the order no longer matters.
  • A cache stat() that resolves after the source changed is ignored, so an older source can't start loading after a newer one.
  • Unchanged: a download that is still current when the view unmounts still finishes and fills the cache, and real download errors still reach onError once.

Testing

The repo has no test setup, so I tested index.js with Jest and react-native-blob-util mocked. The mock's cancel() rejects the task with a canceled error, like the real one.

Case master This PR
Source changes while the download runs: the old download is canceled, the new one is shown, onError isn't called ❌ onError(Error: canceled) ✅
Source changes to a cached file, then the old download finishes: the cached file stays, onError isn't called ❌ onError(Error: canceled) ✅
Download of the current source fails: onError is called once ✅ ✅
fetch() throws: onError is called, no unhandled rejection ✅ ✅

Related: #796 (the lastRNBFTask.cancel is not a function crash, fixed by #1031), #870.

🤖 Generated with Claude Code

https://claude.ai/code/session_012KqBcHkPPX7gFtJ8cnSDmV

…nError

When the source changes while a download is running, the old download is
canceled. react-native-blob-util rejects the canceled task, and the catch in
_downloadFile passed that to onError, so apps showed a load error while the
new source was loading. A replaced download that still finished could also
show its file or clear lastRNBFTask for the newer download.

Give each download an id and ignore the result of one that is no longer
current. Load the new source right away in componentDidUpdate instead of in
the cancel callback, which react-native-blob-util doesn't call once the
request has finished. Ignore a cache stat() that resolves after the source
changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012KqBcHkPPX7gFtJ8cnSDmV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant