fix(8822): the backup converts each table as it reads it and streams each section to disk
CI & Build / TypeScript typecheck (push) Successful in 10s
CI & Build / Python lint (push) Successful in 2s
CI & Build / Plugin hooks (push) Successful in 16s
CI & Build / Python tests (push) Successful in 1m54s
CI & Build / integration (push) Successful in 1m57s
CI & Build / Build & push image (push) Successful in 28s

The export held every table's ORM objects and every table's rows at once,
and the archive writer then built each section as one byte string (a peak of
twice the section). The nightly run raised the app's memory about 200 MB that
it kept. Each table is now converted before the next is read, and each section
is encoded into a spool file that moves to disk past 1 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-06 10:50:23 -05:00
co-authored by Claude Opus 5.5
parent 6052a66aca
commit f19ba0c798
3 changed files with 169 additions and 134 deletions
+19
View File
@@ -8,6 +8,7 @@ shape this file replaced.
"""
import io
import tarfile
import tracemalloc
from datetime import datetime, timezone
from unittest.mock import patch
@@ -162,6 +163,24 @@ def test_a_failed_write_leaves_nothing_behind(tmp_path):
assert list(tmp_path.iterdir()) == []
def test_a_large_section_is_never_held_in_memory_whole(tmp_path):
"""The app keeps the memory of its largest moment, so writing a section
must not build its bytes in memory: the peak while writing stays a small
fraction of the section's size."""
body = "x" * 1000
payload = _payload(notes=[{"id": n, "body": body} for n in range(20_000)])
tracemalloc.start()
try:
with open(tmp_path / "out.tar", "wb") as f:
write_archive_to(payload, f, app_version="v")
_, peak = tracemalloc.get_traced_memory()
finally:
tracemalloc.stop()
written = (tmp_path / "out.tar").stat().st_size
assert written > 20_000_000
assert peak < written // 5, f"peak {peak} bytes while writing {written}"
def _rewrite(members: dict[str, bytes]) -> io.BytesIO:
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w", format=tarfile.USTAR_FORMAT) as tar: