Skip to content

fix(backup): keep scratch writes off the SD card - #1369

Merged
frahlg merged 1 commit into
masterfrom
fix/backup-scratch-tmpfs
Sep 24, 2026
Merged

frahlg merged 1 commit into
masterfrom
fix/backup-scratch-tmpfs

Conversation

@frahlg

@frahlg frahlg commented Sep 24, 2026

Copy link
Copy Markdown
Member

Summary

  • Fixes Backup IO delays durable charging-goal saves on Raspberry Pi #1246. A live backup's raw SQLite export goes to /dev/shm when the temporary file fits. Scratch commits no longer share the SD card queue with a durable charging-goal save. The compressed archive is still written to the destination and synced.
  • mq-deadline on the Pi ignores IO priority. Cutting the on-disk batch from 256 KiB to 64 KiB left a goal save at 2.11 s, and 16 KiB was 2.65 s again. The 2 s limit was not raised.
  • If tmpfs cannot hold the export, the backup stays on disk as before.

Measured on homelab-rpi, synthetic data only, not the household database

FTW_STORAGE_ADMISSION=1 with the 3,000-row / 48 MiB fixture:

Run Max durable goal save RSS
No backup 442 ms 21 MiB
Backup, scratch on the SD card 2.65 s 35 MiB
Backup, scratch in tmpfs 18 ms 59 MiB

Restore checked 3,000 rows and the saved goal. All admitted telemetry committed.

Also already on master, and on the running v0.132.0-beta.1

Those three tests passed locally. This PR does not change them.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-24T04:34:10.622124Z 4c2cb31 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

A durable goal save on homelab-rpi waited 2.65s behind backup commits
on the same card. mq-deadline ignores IO priority, and smaller batches
did not get under the 2s limit. The raw export now uses tmpfs when the
file fits. On that Pi the same fixture then saved a goal in 18ms, and
the restored archive still matched.
@frahlg
frahlg force-pushed the fix/backup-scratch-tmpfs branch from 4c2cb31 to 0c7cb9f Compare September 24, 2026 04:31

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4c2cb31e2e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if err == nil {
name := f.Name()
_ = f.Close()
_ = os.Remove(name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep ownership of the temporary inode

On a multi-user Linux host, /dev/shm is world-writable, so removing the securely created file before SQLite opens the returned path creates a TOCTOU window. A local user watching the directory can replace the name with a world-writable regular file while the progress callback runs; Core then writes its raw state/configuration into an attacker-owned readable file, and the sticky bit can prevent cleanup. Keep the CreateTemp inode in place for SQLite to reopen, or use a private tmpfs directory.

Useful? React with 👍 / 👎.

Comment on lines +120 to +122
need := sourceBytes + backupScratchHeadroom
if avail, err := diskAvail("/dev/shm"); err == nil && avail >= need {
f, err := os.CreateTemp("/dev/shm", "ftw-backup-*.raw")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Bound tmpfs use by the service memory limit

The reference native deployment sets MemoryMax=512M in deploy/ftw.service:17, but this condition only compares the export estimate with host-wide /dev/shm space. On a 4/8 GB Raspberry Pi, an export larger than roughly 448 MiB can pass this check even though the tmpfs pages plus Core's existing memory are charged to the service cgroup, causing the backup to OOM-kill the control process instead of falling back to disk. Include cgroup/process memory headroom in this decision or cap tmpfs scratch below the unit limit.

Useful? React with 👍 / 👎.

Comment on lines +139 to +140
if _, err := os.Stat("/dev/shm"); err != nil {
t.Skip("no tmpfs")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip the test when tmpfs capacity is insufficient

This skip checks only that /dev/shm exists, while the helper requires at least 65 MiB free for this 1 MiB source. Standard containers commonly mount a 64 MiB /dev/shm, so the helper correctly falls back to disk and this test fails. Probe the available capacity and skip when sourceBytes + backupScratchHeadroom does not fit.

Useful? React with 👍 / 👎.

@frahlg
frahlg merged commit 363a8c0 into master Sep 24, 2026
15 checks passed
@frahlg
frahlg deleted the fix/backup-scratch-tmpfs branch November 11, 2026 09:18
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 IO delays durable charging-goal saves on Raspberry Pi

1 participant