Skip to content

Keep the last good state files when a save fails (ENOSPC); retry it; no clean stop while saves fail - #748

Draft
xbmc4lyfe wants to merge 3 commits into
mainfrom
fix/enospc-save-progress
Draft

xbmc4lyfe wants to merge 3 commits into
mainfrom
fix/enospc-save-progress

Conversation

@xbmc4lyfe

@xbmc4lyfe xbmc4lyfe commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Why

On 6790 (2026-11-27 02:56Z) the NVMe holding QueueDir and InterDir filled up. At the 20:56 CST stop the partial state of the downloading file, 334597s (Uncut Gems UHD #2466, one 90 GB file of 126,649 articles), was written in place. The failed write cut it short. After the restart the load logged Error reading diskstate for file 334597 and Discarding damaged download state 334597s, so every article it recorded was dropped: RemainingSizeMB went from 27434 to 85139, and about 57 GB was downloaded again. The queue, files, progress and stats saves failed too, and the queue save still deleted progress.

What restores per-file progress is <id>s (each article's status, offset, size and crc). progress holds only NZB-level counters. The current NZB counters (remaining size and the rest) are recomputed from the file states at load (UpdateCurrentStats), so a stale progress costs only counters, never a download. The state RustyGet already keeps is therefore enough, as long as a failed write can never destroy it.

What changes

  • The last good file is kept. Every state file, now also <id>s/<id>c, is written to <file>.new and renamed. If the write fails, or a sync fails with ENOSPC, EIO or EDQUOT, the .new is deleted, the old file stays and the save fails. The file is no longer deleted before the rename: rename(2) replaces it in one step. Partial states stay unsynced, as before. Syncing each one, plus its directory, queued the per-second saves of every downloading file behind each other (queuesyncslow regressed).
  • A failed save is retried (statesaver). Each attempt is logged at ERROR. A failed save stays queued and is retried with backoff (1 s, doubling to 30 s). A newer save of the same file takes its place and its backoff. No caller waits for a failing save, so the API and the coordinator are not stalled by a full disk. A success after failures logs Saved diskstate file X after N failed attempts.
  • progress is deleted only after the queue save is on disk. This uses a delete that waits for another file's write (Delete { after }). The delete never runs while the queue save fails, and it is dropped if that save is given up.
  • A stop is not clean while saves fail. At teardown each failing save gets one last attempt. Any that still fail are named in Stopping is not clean: N state file(s) could not be saved ... (ERROR), and the process exits 1. systemd then reports a failure instead of "Deactivated successfully".
  • The loader skips <id>s.new entries; such a file is read through its <id>s. remove_queue_file also deletes the .new.

Scenario diskfullrestart

QueueDir and InterDir sit on a small file system of their own: a 64 MB ext4 image, loop-mounted with sudo -n on Linux, or an HFS+ image through hdiutil on macOS. ext4 frees a truncated file's blocks only at the next journal commit, as the 6790 NVMe did. With ContinuePartial=yes, FlushQueue=yes and DiskSpace=0:

  1. A 40 MB file downloads (400 articles).
  2. At 40 % the file system is filled and kept full.
  3. The daemon is stopped.
  4. Space is freed and the daemon is started again.

The scenario asserts that no article fetched before the fill (less the last 4 s) is fetched again, counted by message-id from nserv's request log. It also asserts that the output is byte-exact, that save errors were logged, and that the stop exited 1 with the unclean log.

  • Control, origin/main plus the scenario only (23716162), x86_64: FAIL (timeout waiting for RelDF in history; rerun alone: FAIL).
  • Fix (2f3d697), x86_64: PASS status=SUCCESS integrity=True recorded_before_fill=131 fetched_again=0 ... save_errors=15 stop_rc=1 unclean_logs=1 discarded_states=0. arm64: the same, with 130 recorded and 0 fetched again.

Not in this PR (follow-up)

The 6790 journal also shows 2628 Could not commit cached article ... No space left on device. article_writer_flush_cache_guarded counts and discards a cached article whose write failed, while the article stays done in the state, so the output gets a hole. Keeping the item cached would block the stop: the cache thread exits only when the cache is empty, and the acache flag would drop every partial state. That needs its own design, recorded in rustyget-next-prs.md.

Validation

Farm gate on head 115cb37 (job farm-val-115cb37e-179575184336p8; GitHub Actions unavailable, farm runs replace it):

== x86_64  commit=115cb37eceed94cb88e5616d40cd3d83218efce5
cargo_exit=0   (cargo test --tests, 12 test threads on the farm host, 82s)
daemon_exit=0   (just test-daemon, 44s)
442 / 442 scenarios passed
== arm64  commit=115cb37eceed94cb88e5616d40cd3d83218efce5
cargo_exit=0   (cargo test --tests, 8 test threads on the farm host, 168s)
daemon_exit=0   (just test-daemon, 45s)
442 / 442 scenarios passed

diskfullrestart on the head (x86_64 and arm64 PASS):

RESULT diskfullrestart PASS status=SUCCESS/HEALTH integrity=True recorded_before_fill=108 fetched_again=0 of them fetched_after_restart=252 save_errors=19 stop_rc=1 unclean_logs=1 discarded_states=0
RESULT diskfullrestart PASS status=SUCCESS/HEALTH integrity=True recorded_before_fill=115 fetched_again=0 of them fetched_after_restart=245 save_errors=19 stop_rc=1 unclean_logs=1 discarded_states=0

Control: the same scenario on main 7bef261 plus the scenario only (23716162) FAILS on x86_64 (timeout waiting for RelDF; rerun alone FAIL).

GitHub Actions is unavailable; the farm runs replace it. macOS: not required (no platform-specific code; the scenario's macOS branch uses hdiutil).

🤖 Generated with Claude Code

https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM

xbmc4lyfe and others added 3 commits November 26, 2026 19:39
…stop while saves fail

On 6790 the queue disk filled (ENOSPC). The partial state of the downloading
file (<id>s) was written in place, the failed write cut it short, the restart
found it damaged and discarded every article it recorded: 57 GB downloaded
again.

- Every state file, also <id>s/<id>c, is written to <file>.new and renamed;
  a write or a sync that fails (ENOSPC, EIO, EDQUOT) deletes the .new and keeps
  the last good file. The file is no longer deleted before the rename.
- statesaver: a failed save stays queued and is retried with backoff (1 s
  doubling to 30 s); a newer save of the file takes its place and backoff; no
  caller waits for a failing save. At the stop each gets a last attempt; a
  still failing one is given up, logged at ERROR, and the process exits 1.
- A queue save deletes progress only after the queue file reached the disk
  (a delete that waits for another file's write), never while it fails.
- Scenario diskfullrestart: QueueDir/InterDir on a small ext4 (Linux, sudo
  loop mount) or HFS+ image (macOS), filled at 40 %, stop, free, restart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM
Syncing every <id>s save (and its directory) queued the per-second saves of
all downloading files behind one another on the saver thread; a thread that
waits for its save under the queue lock waited seconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM
…k, .new handling

- drain gives failed saves their last attempt one at a time, recorded in
  last_attempt in the same hold, so a delete waiting for that file never
  runs before the attempt is decided.
- Without a saver thread, drain and wait_for run the due jobs inline; a
  failed inline job no longer stops the others and merges into a newer one.
- The worker sleeps only for retries it could run.
- Tickets only under the queue lock.
- remove_queue_file deletes <file>.new too; the state loader skips names
  with a dot (a <id>s.new is read through its <id>s).
- The unclean-stop flag is decided by each teardown.
- diskfullrestart: stop must exit 1 with the unclean log, 4 s margin,
  lazy unmount fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM

This branch has not been deployed

No deployments
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