Repository navigation
fix(downloads): keep a download going after the app is force-stopped - #675
Conversation
…fused When WorkManager restarts a download as the app process starts in the background, Android 12+ refuses the foreground service start. The refusal from the progress-loop setForeground reached the permanent-failure branch, which deleted the partial and failed the work, and the Downloads tab kept showing the row as Downloading because the server row stays `downloading` and outranked the local Failed status. - Treat a refused foreground promotion as non-fatal; the next percent change asks again. - Keep the partial when the system stops the work (time limit, lost constraint, quota) so the retry resumes it; an app cancel still drops it, and a detail-screen cancel of waiting work now removes the kept bytes. - Show a local permanent failure over the server's `downloading` status, and re-read download rows when the set of failed rows changes.
An interrupted download's partial is a MediaStore item with IS_PENDING=1. Collection-wide MediaStore matches leave pending items out unless the caller opts in, so every slot delete (permanent failure, Remove, revoke cleanup, a fresh restart's pre-clean) and every profile/server purge deleted nothing, and each attempt left a hidden `.pending-…` file that the OS only expires after about a week. - Path deletes opt in to pending items (QUERY_ARG_MATCH_PENDING on Android 11+, setIncludePending on Android 10). - The worker's stop handler used to rely on that delete missing the partial: a Wi-Fi or other system stop now keeps the partial for the retry and only an app cancel deletes it. - Cancelling waiting work from the item detail screen deletes the kept bytes along with the row.
WorkManager records the stop reason itself on every API level, and an app cancel always reads STOP_REASON_CANCELLED_BY_APP. Read it everywhere and keep the partial for any other stop, including the reasonless JobScheduler stops before Android 12, so a lost Wi-Fi constraint resumes instead of restarting from zero once slot deletes can see pending partials.
…te-pending-partials
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
The background-restart transfer behavior in Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
|
Evidence is published at https://evidence.siloserver.org/r/silo-android/pr-675/: Pixel 7 and Pixel Fold (open and folded) before and after a force-stop mid-download. On the tablet the same run resumed and completed (see Validation); the list layout does not change. Android TV has no Downloads. |
Problem
Related issue: N/A
Validation tasks: unblocks #329 C2
A download can get stuck on "Downloading" forever after the app is force-stopped. On Android 12 and later, when WorkManager restarts the download while the app is still starting in the background, Android refuses the worker's foreground notification (
BackgroundServiceStartNotAllowedException). The refusal from the progress update was not caught, so the worker treated it as a permanent failure and deleted the partial. The Downloads tab then kept showing "Downloading" with no progress, because a failed local status was overwritten by the server'sdownloading. Reproduced twice on a Pixel 7 emulator: the row stayed on "Downloading 37.3 MB" for more than five minutes with no worker running, and the user was never told to remove and re-add it.This change keeps the transfer running when the foreground start is refused, and shows a download that does fail as "Failed".
Stacked on #674, which must merge first; review the commits after "fix(downloads): delete pending MediaStore partials with their slot".
Approach
promoteToForeground: a refusal is logged and the transfer continues as ordinary work, trying again at the next progress step. A stop still propagates.Validation
./scripts/test-check-build-supply-chain.sh,./scripts/check-build-supply-chain.sh: pass../gradlew testDebugUnitTest: 908 tests, 1 failure,ReflowStyleTest > line height flows from settings, which also fails on main (confirmed on main at 5e19a58). New tests: a refused promotion returns instead of throwing, a stop during promotion still stops, and a local Failed shows as Failed against a serverdownloading.assembleDebugfor both apps: pass. This branch alone::android-shared:testDebugUnitTestand:androidApp:assembleDebugpass.doWork fatal … BackgroundServiceStartNotAllowedException, partial deleted, row stuck on "Downloading" (twice).Evidence
Evidence: https://evidence.siloserver.org/r/silo-android/pr-675/
Surface and build: Pixel 7 emulator, Android 16. Before: main at 5e19a58. After: this branch. Both show the Downloads tab after a force-stop mid-download and reopening the app.
Risks
Item detail still reads the server-refreshed status, so it can offer Cancel for a download the Downloads tab shows as Failed.
Checklist
AI Disclosure