[PR #1851] Fix download corrupt network switch #1726

Open
opened 2026-04-25 00:00:52 +02:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/advplyr/audiobookshelf-app/pull/1851
Author: @ckbaker10
Created: 4/18/2026
Status: 🔄 Open

Base: masterHead: fix-download-corrupt-network-switch


📝 Commits (2)

  • d195f2c Network switch fix
  • 0243526 Network onFailure not beeing handled correctly

📊 Changes

4 files changed (+168 additions, -40 deletions)

View changed files

📝 android/app/src/main/java/com/audiobookshelf/app/data/PlaybackSession.kt (+21 -11)
📝 android/app/src/main/java/com/audiobookshelf/app/managers/DownloadItemManager.kt (+101 -7)
📝 android/app/src/main/java/com/audiobookshelf/app/managers/InternalDownloadManager.kt (+43 -21)
📝 android/app/src/main/java/com/audiobookshelf/app/models/DownloadItemPart.kt (+3 -1)

📄 Description

Brief summary

Fixes downloads getting silently marked as complete when switching networks mid-download, resulting in missing files with no error shown.

Which issue is fixed?

No issue number, noticed this myself while commuting.

Pull Request Type

Android only. Backend of the app, no frontend changes.

In-depth Description

When you switch networks — say, disabling WiFi and falling back to mobile — any files currently downloading would get quietly marked as complete even though they were never actually written to disk. The download would "finish" and show as done, but the files just weren't there.

The cause is a race condition between OkHttp and Android's ConnectivityManager. OkHttp fires its onFailure callback the moment the socket dies, which sets completed=true and failed=true on the download part immediately. Android's NetworkCallback.onLost fires later, and the old filter !it.completed && !it.failed meant those already-failed parts were completely ignored — never paused, never retried, just abandoned. Since all parts ended up with completed=true, isDownloadFinished returned true and the whole item got processed as a finished download with missing files.

Three things changed to fix this:

  • handleInternalDownloadPart now checks isNetworkAvailable() when it sees a failed part. If there's no network, instead of treating it as a permanent failure it resets the part and puts it in pausedDownloadItemParts. When connectivity returns, onAvailable re-queues it and the download resumes from the .tmp byte offset as normal.
  • The onLost filter was changed from !it.completed && !it.failed to !it.completed || it.failed so parts that OkHttp already failed during the drop also get caught and marked paused.
  • Added isNetworkAvailable() helper that checks NET_CAPABILITY_INTERNET and NET_CAPABILITY_VALIDATED, consistent with what the NetworkRequest was already registering for.

The existing resume-from-offset logic and .tmp file handling was already there and works fine, this just makes sure parts don't get discarded before they get a chance to use it.

How have you tested this?

Tested on my own device via the local-build branch during daily audiobook listening. Reproduced by starting a download over WiFi, disabling WiFi mid-download to force a switch to mobile, and verifying the download pauses and then resumes and completes correctly once the new network is established. Previously this would silently complete with missing files. Which could crash the App

https://github.com/ckbaker10/audiobookshelf-app/tree/local-build

Screenshots

N/A, no UI changes.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/advplyr/audiobookshelf-app/pull/1851 **Author:** [@ckbaker10](https://github.com/ckbaker10) **Created:** 4/18/2026 **Status:** 🔄 Open **Base:** `master` ← **Head:** `fix-download-corrupt-network-switch` --- ### 📝 Commits (2) - [`d195f2c`](https://github.com/advplyr/audiobookshelf-app/commit/d195f2c6a86b3280d47ccd4f59f20a47c8922c0e) Network switch fix - [`0243526`](https://github.com/advplyr/audiobookshelf-app/commit/024352692521663d6676ed6ea597aea803378abe) Network onFailure not beeing handled correctly ### 📊 Changes **4 files changed** (+168 additions, -40 deletions) <details> <summary>View changed files</summary> 📝 `android/app/src/main/java/com/audiobookshelf/app/data/PlaybackSession.kt` (+21 -11) 📝 `android/app/src/main/java/com/audiobookshelf/app/managers/DownloadItemManager.kt` (+101 -7) 📝 `android/app/src/main/java/com/audiobookshelf/app/managers/InternalDownloadManager.kt` (+43 -21) 📝 `android/app/src/main/java/com/audiobookshelf/app/models/DownloadItemPart.kt` (+3 -1) </details> ### 📄 Description ## Brief summary Fixes downloads getting silently marked as complete when switching networks mid-download, resulting in missing files with no error shown. ## Which issue is fixed? No issue number, noticed this myself while commuting. ## Pull Request Type Android only. Backend of the app, no frontend changes. ## In-depth Description When you switch networks — say, disabling WiFi and falling back to mobile — any files currently downloading would get quietly marked as complete even though they were never actually written to disk. The download would "finish" and show as done, but the files just weren't there. The cause is a race condition between OkHttp and Android's ConnectivityManager. OkHttp fires its `onFailure` callback the moment the socket dies, which sets `completed=true` and `failed=true` on the download part immediately. Android's `NetworkCallback.onLost` fires later, and the old filter `!it.completed && !it.failed` meant those already-failed parts were completely ignored — never paused, never retried, just abandoned. Since all parts ended up with `completed=true`, `isDownloadFinished` returned true and the whole item got processed as a finished download with missing files. Three things changed to fix this: - `handleInternalDownloadPart` now checks `isNetworkAvailable()` when it sees a failed part. If there's no network, instead of treating it as a permanent failure it resets the part and puts it in `pausedDownloadItemParts`. When connectivity returns, `onAvailable` re-queues it and the download resumes from the `.tmp` byte offset as normal. - The `onLost` filter was changed from `!it.completed && !it.failed` to `!it.completed || it.failed` so parts that OkHttp already failed during the drop also get caught and marked paused. - Added `isNetworkAvailable()` helper that checks `NET_CAPABILITY_INTERNET` and `NET_CAPABILITY_VALIDATED`, consistent with what the `NetworkRequest` was already registering for. The existing resume-from-offset logic and `.tmp` file handling was already there and works fine, this just makes sure parts don't get discarded before they get a chance to use it. ## How have you tested this? Tested on my own device via the local-build branch during daily audiobook listening. Reproduced by starting a download over WiFi, disabling WiFi mid-download to force a switch to mobile, and verifying the download pauses and then resumes and completes correctly once the new network is established. Previously this would silently complete with missing files. Which could crash the App https://github.com/ckbaker10/audiobookshelf-app/tree/local-build ## Screenshots N/A, no UI changes. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-04-25 00:00:52 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf-app#1726