Skip to content

Alt borrowing fetches each donor article once (fetched-article store) - #738

Closed
xbmc4lyfe wants to merge 7 commits into
mainfrom
alt-borrow-fetch-once
Closed

xbmc4lyfe wants to merge 7 commits into
mainfrom
alt-borrow-fetch-once

Conversation

@xbmc4lyfe

@xbmc4lyfe xbmc4lyfe commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Alt borrowing fetches each donor article once

Audit finding 6 (4 GiB alt failover run, 2026-11-26): borrowing for a dead 97.7 MB volume from an
85-volume alt took two fetch passes (186.9 MB and 87.8 MB). Each pass fetched the first and last
article of all 85 donor volumes, so 172 message-ids were requested twice (88–276 MB fetched again).

Why there were two passes

Nothing failed or timed out and restarted. The download had two holes: part13 was dead, and part26
had 2 missing articles. Each hole got its own stream-repair run, and each run has its own
Fetch cost line (the second one shows 1 batch of 3 articles). Every run builds the donor's
payload map again through ContentMapper::build_map_with_tls, which reads each volume's first
article and, for the end-of-archive block, its last one.

Two caches should have saved the second walk, and both were too small:

  • RECENT_SINGLES holds 64 articles / 48 MB, but one walk of AltB is 170 articles / 88 MB.
    FIFO eviction had emptied it before the next run started.
  • nntp-rs's shared body cache holds 64 MB, and the download's own traffic between the two runs
    pushed the edge articles out of it.

A second run for a second hole is correct. The bug was that it went back to the network.

Change

  • New rust/src/donorstore.rs, a store of fetched donor articles on disk, keyed by message-id
    (angle brackets normalised). Only the index is held in memory.
    • Location: <TempDir>/rustyget-donor-articles-<pid>-<nonce>/, a new directory created with
      mode 0700. It never reuses an existing path and never follows a symlink.
    • Each file starts with its message-id. A file is served only when its key matches and its
      size is exact.
    • Only articles that arrived whole are kept.
    • Entries are owned by the downloads they were fetched for. forget(nzb_id) drops them when
      the download leaves the queue or starts over (postproc::forget_dead_volume_verdicts).
    • Caps: 512 MB and 4096 articles in total (oldest out first), 8 MB per article, and 1 KB per
      message-id.
    • Stores left by earlier runs are removed once, outside the lock, before the first article is
      kept.
  • Stream repair (streamrepair_coord.rs):
    • Single fetches (the map walk, member sizes) are stored. Batched fetches are stored when they
      are a volume's first or last article.
    • Every lookup checks the memos first, then the store. This covers single fetches and batch
      plans, so the borrow itself too.
    • The fetch cost line now adds N from the fetched-article store.
  • TwinCheck sampling (twin.rs) and fleet intake / probe fetches (dupesearch_coord.rs) read
    from and write to the same store, so a header that was sampled is never fetched again by the
    map or the borrow.

Twins and alts rules: no container bytes are compared, the one payload map is unchanged, and
re-RAR header handling is untouched. The store only replaces network fetches: a message-id names
the same article bytes on every server, and offset and file size are kept with the bytes.

OPEN ITEM, not done: mapping only the donor volumes that cover the hole

On a first map with no earlier one, every volume is still walked once. A RAR volume records only
its own piece size (packed_size), not where that piece sits in the payload. The offset of
volume k's piece is the sum of every earlier piece, so getting it means reading every earlier
header. Inferring it from equal posted volume sizes would be an assumption, and the rules forbid
that. Once a donor has been walked (by the fleet intake, TwinCheck or a repair run), every later
walk and borrow costs no network.

Tests

  • donorstore.rs unit tests:
    • store, return, and forget by owner
    • a file whose size changed is not served
    • a file of the same size holding another article's message-id is not served
    • message-ids with or without brackets are one article
    • empty and oversized articles are not stored
  • New harness scenario altfetchonce:
    • Setup: a 90 MB stored RAR loses one 3 MB volume whole and 2 articles of a later volume. Its
      alt (1 MB volumes, 90 of them) waits in history.
    • It asserts that no donor message-id is requested twice.
    • It asserts that donor bytes stay within the lost range, plus one first and one last article
      per donor volume, plus the batch margin around each hole.
    • It asserts that at least 2 repair runs happened and at least one was served from the store.
  • Farm x86_64, same scenario, with and without this change:
    • Baseline (main + this scenario, 6a570ead): FAIL. 146 donor message-ids were asked twice.
      The second run fetched 4.7 MB in 177 single fetches.
    • This branch (5c5e0de): PASS. Nothing was asked twice. The later runs made 0 and 1
      single fetches; the rest came from memory (64) and the store (116).

Validation: merged squash 7bef261 (GitHub Actions unavailable; local runs replace it)

7bef261 is a signed squash with sole parent main 4b3f599. Its tree matches branch head d7097b0. That head is 18958d0 rebased onto the new main, plus one fix: the store is now forgotten only when a download is finished with its borrows, is deleted, or goes back from history to the queue. It is no longer forgotten on the path that keeps borrows for post-processing.

Linux, rg-farm farm-val-7bef261b-1795746942ir2j:

== x86_64  commit=7bef261ba1e3ba2c4f799f8f0b715b8bda60d1a7
cargo_exit=0   (cargo test --tests -- --test-threads=12 on rustyget-x86-d, 116s)
daemon_exit=0   (just test-daemon on rustyget-x86-d, 45s)
438 / 441 scenarios passed on target=local
[FAIL] liveparrartrust  rerun alone: PASS => FLAKY under load
[FAIL] queuebatchstart  rerun alone: PASS => FLAKY under load
[FAIL] xpacknestedsettarget  rerun alone: PASS => FLAKY under load
== arm64  commit=7bef261ba1e3ba2c4f799f8f0b715b8bda60d1a7
cargo_exit=0   (cargo test --tests -- --test-threads=12 on rustyget-arm-c, 152s)
daemon_exit=0   (just test-daemon on rustyget-arm-c, 45s)
440 / 441 scenarios passed on target=local
[FAIL] fleetbigqueue  rerun alone: PASS => FLAKY under load

macOS on 7bef261 (m5air): still running when this was merged; I will add the result when it lands. The coordinator does not treat macOS as a gate here, because the change has no platform-specific code.

Earlier head 18958d0:

  • farm x86_64: cargo 0, daemon 0, 431/433 scenarios; the 2 failures passed when rerun alone.
  • farm arm64: cargo 0, daemon 0, 432/433 scenarios; the 1 failure passed when rerun alone.
  • m5air macOS: cargo 0, daemon 0, 429/433 scenarios. fleetprobebusy and streammaxrun fail the same way on main ba93ae1 (0/2), so they were already failing before this change.

Review: the twin-alt-reviewer gave a clean-room G2G, followed by delta G2Gs on 18958d0 and 683c11fe, and on 7bef261 (the final squash, tree and parent checked).

Deployment

6790 runs 9a2323f3. That build is deploy-6790 eac9eba6, plus the rar-limit fix d6a39194 that 6790
was already running, plus this branch at 18958d0.

  • deb sha256 4caad4d80addea89c7c8dbe4e8001fcf6167b6bb84f07885efd8fa7f2f06942e
  • The conf was backed up to /var/backups/rustyget-altfetch-20261127T021334Z; it compares
    identical after the install, and the Server* block is kept.
  • The service is active, with NRestarts=0 two minutes after the restart.
  • Rollback deb: server:~/rgtest/rar-limit.deb.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T02:37:56.201382Z d7097b0 New commits
ℹ️ 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.

@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: 5c5e0de849

ℹ️ 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".

Comment thread rust/src/donorstore.rs
Comment on lines +122 to +126
#[cfg(unix)]
fn process_alive(pid: i32) -> bool {
// SAFETY: signal 0 only checks that the process exists.
let r = unsafe { libc::kill(pid, 0) };
r == 0 || std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)

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 Provide the required exact-head macOS validation

When Actions is unavailable, AGENTS.md requires local validation on macOS, Linux x86_64, and Linux arm64 and explicitly instructs reviewers to request missing results. This commit's validation notes instead declare macOS unnecessary and reference functional results from an earlier head. Please provide the required test results for the reviewed head on all three platforms; the new Unix-specific implementation does not exempt this change from that review gate.

AGENTS.md reference: AGENTS.md:L70-L77

Useful? React with 👍 / 👎.

Comment thread rust/src/donorstore.rs Outdated
Comment on lines +140 to +145
let dir = base.join(format!("{DIR_PREFIX}{}", std::process::id()));
if std::fs::symlink_metadata(&dir).is_ok_and(|m| m.is_dir()) {
// left by this process id before (a reused id after a restart): start empty
let _ = std::fs::remove_dir_all(&dir);
}
std::fs::create_dir_all(&dir).ok()?;

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 Reject symlinks when creating the article-store directory

If TempDir is shared and writable by another local user, that user can precreate rustyget-donor-articles-<pid> as a symlink to a directory they control. The preceding symlink_metadata check leaves that symlink intact, and create_dir_all follows it successfully. Article files are then written into the attacker's directory, where they can be read or modified; read_bounded accepts same-length modifications and callers return them as successful articles without repeating the network decoder's CRC validation. Create a private directory exclusively and reject existing symlinks rather than accepting this predictable path.

Useful? React with 👍 / 👎.

@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: 932113a68f

ℹ️ 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".

Comment thread rust/src/donorstore.rs Outdated
Comment on lines +113 to +114
if pid <= 0 || pid as u32 == std::process::id() || process_alive(pid) {
continue;

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 Clean up stale stores when the daemon reuses its PID

When the daemon restarts with the same PID and a persistent TempDir—for example, when it runs as PID 1 in a container—this condition permanently skips the previous process's store directories. The new process creates another nonce directory, but neither shutdown nor startup removes the old one, so repeated restarts can accumulate up to 512 MB per restart outside the current store's size cap and eventually exhaust the download disk. Distinguish the current store from stale directories with the same PID rather than treating every matching PID as a live store.

Useful? React with 👍 / 👎.

xbmc4lyfe and others added 6 commits November 26, 2026 18:32
…es in a job store

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM
…ge-id, cleanup outside the lock; altfetchonce requires the store to serve

Review findings: a store directory is created new (mode 0700, never an existing or planted
one), each file starts with its message-id and is served only under it, a failed read drops
its id from the eviction order, stale stores are removed once outside the store's lock, and
the scenario fails unless two runs happened and one was served from the store.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM
…s id; test a file of another article

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LG44gYUGuwLMB2hw7HQGbM
@xbmc4lyfe
xbmc4lyfe force-pushed the alt-borrow-fetch-once branch from 18958d0 to 9ff4f6a Compare November 27, 2026 02:33
… still repairs it

direct_unpack_forget_holds_keep_borrows hands the download to post-processing, whose stream
repair maps the donor again: the store is forgotten only when the download is done with its
borrows (direct_unpack_forget_holds), deleted, or returned from history to start over.

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

@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: d7097b0234

ℹ️ 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".

Comment on lines +550 to +553
if let Some(hit) = recent_single(msg_id) {
return Some((hit, false));
}
let stored = crate::donorstore::get(owner, msg_id)?;

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 Register store ownership when serving a memory-cache hit

When two downloads use the same donor, a memory-cache hit returns before the second download is registered as an owner of the stored article. If the first download finishes, donorstore::forget deletes that article even though the second download still needs it. Once subsequent mapping or borrowing activity evicts the memory copy, the second download fetches it from the network again, defeating the cross-run retention this change introduces. Register the requesting owner on memory hits too, retaining the cached bytes in the store if its entry has already disappeared.

Useful? React with 👍 / 👎.

xbmc4lyfe added a commit that referenced this pull request Nov 27, 2026
#738)

Each repair run of a download maps its alt again (ContentMapper::build_map_with_tls reads every
donor volume's first and last article), and the 64-article memo and nntp-rs's 64 MB body cache
held less than one walk of a large alt: on the 4 GiB alt failover audit run, two runs (one per
hole: part13 dead, part26 two articles missing) each fetched all 170 edge articles of the
85-volume alt, 88-276 MB again.

rust/src/donorstore.rs keeps donor articles fetched to map or sample a donor copy on disk
(<TempDir>/rustyget-donor-articles-<pid>-<nonce>/, a new 0700 directory; each file carries its
message-id and is served only under it), owned by the downloads they were fetched for and
dropped when one leaves the queue, capped at 512 MB / 4096 articles. Stream repair's single
fetches and volume-edge batch articles, TwinCheck samples and fleet-intake fetches go through
it; the fetch cost line counts what it served.

Harness scenario altfetchonce (90 MB set, one dead volume plus a later two-article hole, a
90-volume alt): no donor message-id fetched twice, donor bytes within the lost range plus one
first and last article per donor volume plus the batch margin; it fails on main (146 ids asked
twice) and passes here.

Mapping only the donor volumes covering a hole on a first map stays open: a RAR volume's piece
offset is the sum of every earlier piece, known only by reading their headers.

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

Copy link
Copy Markdown
Collaborator Author

Merged into main as the locally signed squash 7bef261 (sole parent 4b3f599, tree equal to branch head d7097b0). GitHub reports it verified. Closing: the merged commit differs from the branch head only by being squashed.

@xbmc4lyfe xbmc4lyfe closed this Nov 27, 2026
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