Skip to content

fix: background upload discovery and queue blocking - #4525

Merged
marinofaggiana merged 6 commits into
masterfrom
fix/background-upload-review
Dec 5, 2026
Merged

marinofaggiana merged 6 commits into
masterfrom
fix/background-upload-review

Conversation

@marinofaggiana

@marinofaggiana marinofaggiana commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes from a review of the experimental Photos background upload extension (BackgroundUploadExtension, iOS 27+), plus a small filename label fix found while running the unit tests.

Background upload

  • Discovery no longer reads the history once per uploaded photo. With "all photos" enabled there is no date filter, so every background pass walks the library from its oldest asset. Each already uploaded asset cost one GRDB query and one Realm read, which on large libraries could use up the extension's background time before any new photo was queued. The uploaded components are now read once per pass (NCLocalDatabase.uploadedAutoUploadClasses) and completed assets are skipped without a query. Assets that are not complete still go through the existing per-asset check.

  • Pending transfers no longer block the queue. Job creation reads only the oldest pending transfers that fit the free PhotoKit slots. Transfers skipped on every pass stayed at the head of that list and kept newer photos waiting. Now:

    • a transfer cancelled before it got a PhotoKit job is deleted, as the host app already does;
    • a transfer whose history row is missing is registered again;
    • a transfer that still cannot be tracked, or cannot build a valid request, is handed to the host app, using the path that already routes files over 100 MB there.
  • Discarded uploads are not queued again. Discarding a background upload in Transfers removed only its metadata, while the upload history still listed the file as not uploaded, so the next discovery pass queued it again. A file that always fails came back after every discard. The discarded component is now marked as handled in the history. Force Reupload starts a new history session, so it still gives the file new attempts.

  • Smaller hardening (one commit).

    • A retry is counted before PhotoKit can start it, so an extension stopped right after the request can no longer allow a fourth upload.
    • X-OC-CTime / X-OC-MTime are omitted for dates of one day or less after the epoch. Nextcloud rejects them only after writing the file, which wasted an attempt for photos with a camera clock reset to 1970.
    • When a cancelled upload had already completed, its cancellation request is cleared, as the host app already does.
  • Less duplicated code (refactor commit). Recording a confirmed upload, classifying a failed upload, handling orphan jobs, reading the PhotoKit queues, naming job states and picking the asset resource were each written in two or more places; each now lives in one. Behavior is unchanged, except that a job becoming orphaned during a pass is acknowledged on the next pass.

The legacy auto upload is not affected: it returns early whenever the extension is enabled, and the new history read is used only by the extension.

Filename label (separate commit)

  • The natural width now includes ink drawn past the last glyph's advance. A trailing _ draws about 0.3 pt past its advance, so a label sized to its intrinsic width clipped the end of an extension like .EX_. This is what made the NCFilenameUILabel test "A name that fits is drawn in full" fail locally.

Testing

Built the BackgroundUploadExtension scheme and ran NextcloudUnitTests locally on the iPhone Duo simulator: all 234 tests pass. The background upload changes were not run on a device or against a live Nextcloud instance.

Suggested checks on an iPhone with iOS 27, on a live Nextcloud instance:

  1. Enable Auto Upload with "all photos" on a library where most photos are already uploaded. Take a new photo and keep the app closed. In the shared log (background upload tag) the pass should finish quickly and log "Created pending metadata…" for the new photo.
  2. Cancel a transfer that is still waiting in Transfers before it starts. It disappears and the remaining photos keep uploading.
  3. Discard a background upload that is in error in Transfers. It does not come back after the next background pass. Stop Auto Upload, enable Force Reupload and start it again: the file is uploaded again.
  4. Check that file names ending with _ (for example SETUP.EX_) show their full extension in the file list.

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI

🤖 Generated with Claude Code

marinofaggiana and others added 3 commits December 5, 2026 10:52
With "all photos" enabled the discovery has no date filter, so every
background pass walks the library from its oldest asset and ran one
GRDB query plus one Realm read for each already uploaded asset. On large
libraries this could exhaust the extension's background time before any
new photo was queued.

Assisted-by: Claude Code:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marino Faggiana <marino.faggiana@nextcloud.com>
Glyphs such as "_" draw ink past their advance, but the natural size
counted only the advance. A label sized to its intrinsic width clipped
the end of an extension like ".EX_", which also made the
NCFilenameUILabel "A name that fits is drawn in full" test fail.

Assisted-by: Claude Code:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marino Faggiana <marino.faggiana@nextcloud.com>
Job creation reads only the oldest pending transfers that fit the free
PhotoKit slots. Transfers that were skipped on every pass, because they
were cancelled before getting a job, had lost their history row or could
not build a valid request, stayed at the head of that list and kept
newer photos from being uploaded.

Assisted-by: Claude Code:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marino Faggiana <marino.faggiana@nextcloud.com>
marinofaggiana and others added 3 commits December 5, 2026 11:01
Discarding a background upload in Transfers removed only its metadata.
The upload history still listed the file as not uploaded, so the next
discovery pass queued it again. A file that always fails came back after
every discard and could not be dismissed. Force Reupload starts a new
history session, so it still gives a discarded file new attempts.

Assisted-by: Claude Code:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marino Faggiana <marino.faggiana@nextcloud.com>
- Count a retry before PhotoKit can start it. The count was saved after
  the request, so an extension stopped in between allowed one upload
  more than the three-attempt limit.
- Omit X-OC-CTime and X-OC-MTime for dates of one day or less after the
  epoch. Nextcloud rejects them after writing the file, which wasted an
  attempt for photos with a camera clock reset to 1970 and failed every
  attempt with Force Reupload.
- Clear the cancellation request when a cancelled upload had already
  completed, as the host app does, so the kept file carries no stale
  request.

Assisted-by: Claude Code:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marino Faggiana <marino.faggiana@nextcloud.com>
The same steps were written in several places: recording a confirmed
upload, deciding what a failed upload means for the account, handling
orphan jobs, reading the PhotoKit queues, naming job states and picking
the asset resource. Copies that drift apart are hard to read and easy to
fix in one place only. Behavior is unchanged, except that a job becoming
orphaned during a pass is now acknowledged on the next pass.

Assisted-by: Claude Code:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Marino Faggiana <marino.faggiana@nextcloud.com>
@marinofaggiana
marinofaggiana merged commit 5311402 into master Dec 5, 2026
6 checks passed
@marinofaggiana
marinofaggiana deleted the fix/background-upload-review branch December 5, 2026 11:26
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