Skip to content

fix(api): stream the recover upload to disk instead of read() - #3143

Merged
vpetersson merged 2 commits into
masterfrom
fix/recover-stream-upload
Jul 8, 2026
Merged

fix(api): stream the recover upload to disk instead of read()#3143
vpetersson merged 2 commits into
masterfrom
fix/recover-stream-upload

Conversation

@vpetersson

Copy link
Copy Markdown
Contributor

Issues Fixed

Description

Prompted by a question on whether the recover failures relate to large backups — they do, and here's the concrete bug.

The API restore endpoint (RecoverViewMixin.post, /api/v1|v2/recover) staged the uploaded backup with file_upload.read(), pulling the entire archive into RAM before writing it to disk. A backup contains every image and video asset on the device, so a multi-GB restore allocates a multi-GB bytes object and OOM-kills the worker on a 1 GB Pi. Because it's an OOM (process killed), it surfaces as "restore doesn't work" rather than a clean Sentry stack.

The HTML recover view (settings_recover) was already fixed to stream via file_upload.chunks(); this brings the API path in line — a flat memory footprint regardless of backup size.

Two related restore weaknesses are documented in #3142 as follow-ups (not addressed here to keep this focused): the extraction is still synchronous in the request (can time out on a multi-GB library, the restore equivalent of the download-side #2987/#3073 work), and there's no ENOSPC handling if the SD card fills mid-restore.

The download/generate side is already hardened (astream_backup, #2987/#3073).

Validation

New test uploads a >64 KiB (multi-chunk) payload through the real recover endpoint and asserts the staged file the recover step sees is byte-identical to the upload — proving the streamed write reassembles correctly. Note: the specific Sentry event that started this (ANTHIAS-3W) was a 1.4 KB curl upload of a non-gzip file — unrelated operator input; this fix is for the large-backup OOM the question surfaced.

Checklist

  • I have performed a self-review of my own code.
  • New and existing unit tests pass locally and on CI with my changes.
  • I have done an end-to-end test for Raspberry Pi devices.
  • I have tested my changes for x86 devices.
  • I added a documentation for the changes I have made (when necessary).

🤖 Generated with Claude Code

The API restore endpoint staged the uploaded backup with
file_upload.read(), pulling the entire archive (every image + video
asset on the device) into RAM — a multi-GB restore OOM-kills the
worker on a 1 GB Pi. The HTML recover view already streams via
chunks(); this brings the API path in line.

Fixes #3142

Co-Authored-By: Claude Fable 5 <[email protected]>
@vpetersson
vpetersson requested a review from a team as a code owner July 8, 2026 05:42
@vpetersson vpetersson self-assigned this Jul 8, 2026
@vpetersson
vpetersson requested a review from Copilot July 8, 2026 05:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an OOM failure mode during large backup restores by changing the API recover endpoint to stream uploaded backups to disk in chunks rather than reading the entire upload into RAM, aligning the API behavior with the existing HTML recover flow. It also adds a regression test to ensure the staged file content is byte-identical for multi-chunk uploads.

Changes:

  • Update RecoverViewMixin.post to write the uploaded backup to disk via file_upload.chunks() instead of file_upload.read().
  • Add an API endpoint test that uploads a >64 KiB payload and asserts the recovery step sees the full, correct staged content.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/anthias_server/api/views/mixins.py Streams the uploaded recover archive to disk in chunks to avoid RAM spikes/OOM on large restores.
src/anthias_server/api/tests/test_v1_endpoints.py Adds a regression test ensuring the streamed staging write reassembles multi-chunk uploads correctly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/anthias_server/api/tests/test_v1_endpoints.py
Copilot review: the test created a relative static/ dir in the
checkout and left it behind. Run from a monkeypatch.chdir(tmp_path)
so pytest cleans it up; drop the redundant local os import.

Co-Authored-By: Claude Fable 5 <[email protected]>
@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@vpetersson
vpetersson merged commit 8e6c5c2 into master Jul 8, 2026
10 checks passed
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.

Backup restore chokes on large backups: API recover loads whole upload into RAM (+ synchronous-extraction timeout)

2 participants