Skip to content

fix(torrent): keep seeding counters across restarts and report discarded payload - #574

Merged
linroid merged 5 commits into
fix/torrent-field-testfrom
fix/torrent-payload-counters
Oct 17, 2026
Merged

linroid merged 5 commits into
fix/torrent-field-testfrom
fix/torrent-payload-counters

Conversation

@linroid

@linroid linroid commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes two counter bugs that field tests of #570 found when restarting ketch server against real swarms. Based on fix/torrent-field-test (#570), so this diff holds only these changes.

Seeding tasks lost their upload counts on restart

A checkpoint reached the task record only through store() while a download ran. Two kinds of seeding session never wrote theirs back:

  • sessions lent their slot to keep seeding after a run;
  • sessions TorrentControls restored through startSeeding.

So after a restart, a seeding task's uploaded payload went back to its value at completion, often 0.

  • New SourceHost.saveSourceState(task, state) (core) saves a completed task's source state for a session that runs outside a download. It never takes the task's control lock: a Stop command stops seeding from afterSave, which already holds that lock.
  • Seeders save their checkpoint into the record:
  • Restores keep the larger count. Restoring raises each counter to at least the saved one, for v1 and v2.
  • Snapshots without a running session show the saved totals. countersOf reads them from the record's checkpoint through checkpointPayload, which reads only the counters, not the metainfo.

Discarded payload was counted as received and never reported

TorrentCounters.discardedPayloadBytes is now reported. It is the part of the received payload thrown away instead of verified:

  • v1: blocks that come twice, late or unrequested, plus:
    • pieces that fail their hash check, or whose commit does not finish;
    • partial pieces closed when they go idle, are deselected, are verified by a web seed, lose their last peer, or the swarm stops.
  • v2: stale and losing duplicate blocks, plus:
    • pieces that fail their hash check, fail to store, or are no longer wanted;
    • evicted, unwanted or closed partial pieces.
  • Web seeds: at the end of each run, what the run received minus what its verified pieces kept. This covers corrupt pieces and failed or dropped runs.

Both checkpoint formats save the count as discarded. Older builds ignore the key, and older checkpoints read as 0.

Test plan

  • ./gradlew :library:core:jvmTest :library:torrent:jvmTest
  • New tests:
    • checkpoint round trips and older checkpoints;
    • v1 and v2 scheduler discards;
    • web seed received = verified + discarded on a corrupt and a dropped run, and on a run stopped while throttled;
    • lent and restored seeders saving periodically, on stop, and in stop() before the engine stops;
    • snapshot fallback to saved counters;
    • saveSourceState guards, and a Stop command that saves without deadlocking. That test hangs with the lock taken.
  • compileTestKotlinIosSimulatorArm64 for core and torrent

…ded payload

Seeding sessions never saved their checkpoint into the task record, so a
seeding task's uploaded payload went back to its value at completion after
a restart. They now save it through the new SourceHost.saveSourceState
about every minute, as they stop and as the source closes, and snapshots
without a session report the saved counters.

Payload thrown away rather than verified (duplicate, late or stale blocks,
corrupt pieces, abandoned partial pieces, failed web seed runs) is now
counted in v1, v2 and web seeds, saved in both checkpoint formats and
reported as TorrentCounters.discardedPayloadBytes.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 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-10-17T11:23:15.648829Z 4c32522 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.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

🤖 Generated with Claude Code

@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: 4c32522c4b

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

@linroid
linroid added this pull request to stack #575 October 17, 2026 12:42
…etch into fix/torrent-payload-counters

# Conflicts:
#	docs/torrent.md
#	library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/v1/TorrentSwarm.kt
#	library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/webseed/TorrentV2WebSeeds.kt
#	library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/webseed/TorrentWebSeed.kt
…etch into fix/torrent-payload-counters

# Conflicts:
#	library/torrent/src/commonMain/kotlin/com/linroid/ketch/torrent/TorrentDownloadSource.kt
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 2 042 files  ± 0   2 070 suites  ±0   33m 5s ⏱️ + 1m 42s
13 678 tests +34  13 416 ✅ +34  262 💤 ±0  0 ❌ ±0 
16 651 runs  +54  16 389 ✅ +54  262 💤 ±0  0 ❌ ±0 

Results for commit d2099b2. ± Comparison against base commit 7c3aad8.

♻️ This comment has been updated with latest results.

…t waits for

lonelyStall_stillForgetsAPeerItGaveUpOn assumed the peer was given up at
60 s and found it again at a fixed 400 s. On a loaded machine (the macOS
and iOS CI runners) the swarm handles each refused dial a step or more
late, so the give-up came late and the peer was still remembered at
400 s: dialed once or twice instead of four times. The test now waits
for the fourth dial, finds the peer again 340 s after it, and counts the
dials of the next 90 s, letting the last step's dial land first. Under
full CPU load it failed 2 to 3 runs in 10 before and none after.
@linroid
linroid merged commit 8a76b72 into main Oct 17, 2026
14 checks passed
@linroid
linroid deleted the fix/torrent-payload-counters branch October 17, 2026 14:54
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