Skip to content

fix(downloads): delete unfinished download files with their download - #674

Closed
Rhainland wants to merge 1 commit into
Silo-Server:mainfrom
Rhainland:fix/download-delete-pending-partials
Closed

Rhainland wants to merge 1 commit into
Silo-Server:mainfrom
Rhainland:fix/download-delete-pending-partials

Conversation

@Rhainland

@Rhainland Rhainland commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Related issue: N/A
Validation tasks: unblocks #329 C2, C3

A download that does not finish leaves its partial file in shared storage. When a download fails (for example the phone runs out of space), or the user removes one that is still downloading, the app's delete finds nothing to remove: the partial is a MediaStore item still marked pending, and the slot delete's folder query skips pending items unless it opts in. MediaProvider logs "Deleted 0 … items" for each attempt. On a test phone, three interrupted downloads of one 181 MB movie left 236 MB of hidden .pending-* files in Movies/Silo/…. Android only expires pending items after about a week, so a user who is already short of space silently loses more.

This change deletes a slot's pending partials together with the slot.

Approach

  • MediaStorePublicDownloadStore routes its folder deletes (the slot delete, the slot pre-clean before a new download, and the profile/server purges) through one helper that includes pending items: QUERY_ARG_MATCH_PENDING = MATCH_INCLUDE on Android 11+, setIncludePending on Android 10. Deletes stay limited to this app's items in that slot's folder. Every caller already stops or waits for the slot's worker first.
  • Once deletes can see pending items, the worker's stop handler would delete the partial on any stop, so a lost connection would restart the download from zero. The worker now deletes the partial only on an app cancel; a system stop (time limit, lost network, quota) keeps it for the Range resume, on every Android version.
  • Cancelling from the item detail screen also deletes the slot's bytes, so a partial kept for a retry is not orphaned.

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 fake MediaStore that hides pending rows unless the caller opts in shows a slot delete removing its pending partials and leaving a sibling slot's files alone; a stop-reason table decides when the partial is kept.
  • Lint (all five lint tasks from CONTRIBUTING) and assembleDebug for both apps: pass.
  • On a local Silo server (main) on a Pixel 7 emulator (Android 16), on a build with this change and the other two Android download fixes:
    • Removing a download cleared the three leftover .pending-* files from earlier runs along with the finished file.
    • With 100 MB free, a 181 MB download failed with ENOSPC, showed "Failed", and left the slot folder empty; the 100 MB was free again.
    • A network cut and an app force-stop mid-download both resumed from the partial (Range) and finished with a byte-identical file.
  • The Android 10 code path is not covered by a test or a device run.

Evidence

Evidence: https://evidence.siloserver.org/r/silo-android/pr-674/

Risks

Deletes now remove pending items in a slot's folder. They stay limited to this app's items and to that one slot, and run only after the slot's worker has stopped.

Merge this before #675; that pull request contains this one's commit.

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: including pending items made a lost connection restart downloads from zero (worker now keeps the partial on any system stop, on every Android version); a cancel from item detail could orphan a kept partial; an overstated comment. Dismissed: a remove-then-re-add race (Remove already waits for the worker) and a lint concern that did not reproduce.

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.
@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: 9ee48e33-eff8-4caf-a1a3-2fe2c12d83cb
📥 Commits

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

📒 Files selected for processing: 5
  • android-shared/src/androidMain/kotlin/org/siloserver/silo/common/downloads/DownloadEnqueuer.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
  • 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 size/M 50–199 added + deleted lines (all files); change volume, not priority. bug Something isn't working priority: P2 Limited scope, workaround exists, or polish labels Oct 23, 2026
@macroscopeapp

macroscopeapp Bot commented Oct 23, 2026

Copy link
Copy Markdown

Preserving partial bytes after a system stop changes the mobile Downloads row’s progress during retry from restarting at zero to resuming at the retained amount (DownloadWorker.kt, surfaced by DownloadsViewModel.kt/DownloadEntryRows.kt); this reaches Android phones, tablets, and foldables folded and open. Evidence is missing for those devices: add before-and-after captures of the same Downloads screen and a recording of the stop-to-retry flow, identifying each device and build/commit. Android TV is not affected because its navigation omits the touch-only Downloads screen.

Suggested fix: No diff; replace the “no user-visible change” evidence line with captures covering the listed devices and flow.

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

Posted via Macroscope — Visible change evidence

@Rhainland

Copy link
Copy Markdown
Contributor Author

Evidence is published at https://evidence.siloserver.org/r/silo-android/pr-674/. On the retry progress point: on main a system stop already resumed from the partial, because the delete could not see pending items, so the retry behaviour does not change; this branch keeps that while making deletes work. The visible difference is the failed row's size (119.5 MB before, 0 B after) and the freed storage, shown on the phone where the low-storage run happened.

@Quick104

Copy link
Copy Markdown
Contributor

This PR is superseded: #675 merged its commit in 338f5e0 on main. That squash includes "fix(downloads): delete pending MediaStore partials with their slot". Main already has everything this branch changes: the pending-inclusive folder deletes in MediaStorePublicDownloadStore, downloadStopKeepsPartial in the worker, and the slot delete on a detail-screen cancel in DownloadEnqueuer, along with both new tests. I recommend closing this PR.

No issues are linked, so closing it closes nothing else. Thanks for the work, @Rhainland.

Posted by Claude Code (claude-opus-5-5) on behalf of the maintainer.

@Quick104 Quick104 closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working priority: P2 Limited scope, workaround exists, or polish size/M 50–199 added + deleted lines (all files); change volume, not priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants