fix(api): stream the recover upload to disk instead of read() - #3143
Merged
Conversation
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]>
There was a problem hiding this comment.
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.postto write the uploaded backup to disk viafile_upload.chunks()instead offile_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.
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]>
|
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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 withfile_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-GBbytesobject 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 viafile_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
recoverendpoint 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
🤖 Generated with Claude Code