Skip to content

fix(downloads): keep a download going after the app is force-stopped - #675

Merged
Quick104 merged 4 commits into
Silo-Server:mainfrom
Rhainland:fix/download-survives-background-restart-stacked
Oct 24, 2026
Merged

Quick104 merged 4 commits into
Silo-Server:mainfrom
Rhainland:fix/download-survives-background-restart-stacked

Conversation

@Rhainland

@Rhainland Rhainland commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

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's downloading. 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

  • Both foreground promotions go through promoteToForeground: a refusal is logged and the transfer continues as ordinary work, trying again at the next progress step. A stop still propagates.
  • The Downloads list lets a local "Failed" win over the server status, and re-reads rows when the set of failed downloads changes, so a worker that ends in failure is never shown as downloading.

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 server downloading.
  • Lint (all five lint tasks from CONTRIBUTING) and assembleDebug for both apps: pass. This branch alone: :android-shared:testDebugUnitTest and :androidApp:assembleDebug pass.
  • On a local Silo server (main) on Pixel 7 and Pixel Tablet emulators (Android 16), force-stopping the app mid-download and opening it again:
    • Before: doWork fatal … BackgroundServiceStartNotAllowedException, partial deleted, row stuck on "Downloading" (twice).
    • After: "setForeground refused; continuing in the background", the worker resumed from the partial (Range) and completed with a byte-identical file (twice on the phone, once on the tablet).
  • The Room observer that re-reads failed rows has no unit test; the on-device runs cover it.

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.

  • Tablet: the same list on a Pixel Tablet completed the same way; no layout change.
  • Foldable: Pixel Fold emulator (Android 16), open and folded, on the evidence page; the force-stopped download resumed from the partial there too.
  • Android TV: not affected; the TV app has no downloads.

Risks

Item detail still reads the server-refreshed status, so it can offer Cancel for a download the Downloads tab shows as Failed.

Checklist

  • I read and can explain the complete diff.
  • This pull request addresses one concern.
  • The Evidence section shows every change a user can see, or says there is none.

AI Disclosure

  • Harness: Claude Code
  • Tool(s): Claude Code, Codex CLI (second reviewer)
  • Model(s): claude-opus-5-5 (implementation and review), gpt-6.1-sol (review)
  • Involvement: Fully AI-generated, human verified
  • Adversarial review: dual review of the final diff (Claude Opus 5.5 and GPT 6.1 Sol, medium effort), three rounds. Fixed: dropping the failed-ids flow's first emission could lose a failure written during startup; once a refusal is swallowed, an OS time-limit stop deleted the partial (moved into fix(downloads): delete unfinished download files with their download #674's stop-reason rule); a misplaced comment; an orphaned partial on detail-screen cancel; an extra reload on each Downloads open; an unused return value. Last round: no findings.

…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.
@coderabbitai

coderabbitai Bot commented Oct 23, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b390616b-860f-4f33-99aa-a08f249e0f53
📥 Commits

Reviewing files that changed from the base of the PR and between 5e19a58 and ab19fcd.

📒 Files selected for processing: 9
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/data/db/dao/DownloadDao.kt
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/downloads/DownloadEnqueuer.kt
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/downloads/DownloadMetadataStore.kt
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/downloads/DownloadStorage.kt
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/downloads/DownloadWorker.kt
  • android-shared/src/androidUnitTest/kotlin/org/siloserver/silo/common/downloads/DownloadWorkerProgressTest.kt
  • android-shared/src/androidUnitTest/kotlin/org/siloserver/silo/common/downloads/MediaStorePublicDownloadStoreCollisionTest.kt
  • androidApp/src/androidMain/kotlin/org/siloserver/silo/android/ui/screens/downloads/DownloadsViewModel.kt
  • androidApp/src/androidUnitTest/kotlin/org/siloserver/silo/android/ui/screens/downloads/DownloadStatusLabelTest.kt
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@silo-triage silo-triage Bot added the size/L 200–499 added + deleted lines (all files); change volume, not priority. label Oct 23, 2026
@silo-kody

silo-kody Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Silo Kody — review complete

Review finished. Check the inline comments for findings and verify each suggestion against the code and tests.

Reviewing changes in Silo
  • Include the related issue, expected behavior, and validation steps in the PR description.
  • For API changes, describe the effect on Apple and Android clients and Jellyfin compatibility.
  • For plugin changes, identify the affected SDK contract, plugin, and catalog entry.
  • Follow this repository's AGENTS.md and CONTRIBUTING.md.
  • Request another review with @kody start-review in a PR comment.
  • React with 👍 or 👎 to give feedback on individual suggestions.
Review settings
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ❌
⚠️ 4 Kody Rule(s) were not evaluated

These rules declare that they need repository context beyond this diff, and the review could not retrieve it, so they were not judged on this pull request:

  • Validate download ownership before publishing subtitle bytes
  • Bind playback markers to the actual playing file
  • Share route-aware audio decisions and preserve original-format decoder fallback
  • Apply playback route eligibility consistently at admission and media loading

@macroscopeapp

macroscopeapp Bot commented Oct 23, 2026

Copy link
Copy Markdown

The background-restart transfer behavior in DownloadWorker.kt lacks evidence: the described Pixel 7 and tablet runs are not accompanied by a recording or evidence link, and the foldable is missing in both folded and open states; provide a recording of the force-stop, reopen, and resume flow on a phone, tablet, and foldable folded and open (Android TV is not affected). The Downloads-tab failed-status change in DownloadsViewModel.kt lacks attached or linked before-and-after screenshots of the same screen and data for a phone, tablet, and foldable folded and open. Suggested fix: publish the captures and add the evidence URL to the PR's Evidence section.

Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness.

Posted via Macroscope — Visible change evidence

@silo-triage silo-triage Bot added bug Something isn't working impact: usability Core flow broken or severely blocked priority: P1 Core-path bug; painful workaround — next after P0 stacked Stacked on another open PR or branch labels Oct 23, 2026
@Rhainland

Copy link
Copy Markdown
Contributor Author

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.

@Quick104
Quick104 merged commit 338f5e0 into Silo-Server:main Oct 24, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working impact: usability Core flow broken or severely blocked priority: P1 Core-path bug; painful workaround — next after P0 size/L 200–499 added + deleted lines (all files); change volume, not priority. stacked Stacked on another open PR or branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants