Skip to content

Fix compare-and-swap check for mutable put - #81

Closed
dangowrt wants to merge 2 commits into
anacrolix:masterfrom
dangowrt:fix-cas-check
Closed

dangowrt wants to merge 2 commits into
anacrolix:masterfrom
dangowrt:fix-cas-check

Conversation

@dangowrt

@dangowrt dangowrt commented Aug 3, 2026

Copy link
Copy Markdown

CheckIncoming compared the stored item's Cas field against the incoming
item's Cas, and returned early (accept) whenever stored.Cas was 0. Items are
stored without a meaningful Cas, so stored.Cas is effectively always 0 and
the compare-and-swap check never fired — a stale update that should be rejected
was accepted, so the lost-update protection cas provides did not hold.

Per BEP 44 the incoming put's cas is the sequence number the writer expects to
be replacing, and must equal the stored item's current Seq. This compares
incoming.Cas against stored.Seq, treating an absent cas (0) as "no
compare-and-swap requested".

Found with a black-box BEP 44 conformance suite; verified by rebuilding the DHT
server and re-running its cas and cas_type_confusion checks, which now pass.

CheckIncoming compared the stored item's Cas field against the incoming Cas and
returned early whenever the stored Cas was 0. Items are stored without a
meaningful Cas, so the compare-and-swap never fired and a stale update could
overwrite the value.

Compare the incoming put's cas against the stored item's seq, as BEP 44
specifies, treating an absent cas (0) as no compare-and-swap requested.
@anacrolix

Copy link
Copy Markdown
Owner

Can you include a unit test that demonstrates it? Or even the black box test suite?

@anacrolix
anacrolix marked this pull request as ready for review August 31, 2026 08:51
@anacrolix
anacrolix requested a lite review from Copilot August 31, 2026 08:51

Copilot AI 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.

🟡 Changes recommended

The CAS behavior is critical for lost-update protection and should be covered by targeted unit tests to prevent regressions.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes BEP 44 compare-and-swap (CAS) enforcement for mutable puts by validating the incoming request’s cas against the stored item’s current sequence number (seq), ensuring stale updates are rejected as intended.

Changes:

  • Replace the ineffective stored.Cas-based early-accept logic with a BEP 44–aligned check: incoming.Cas != 0 && incoming.Cas != stored.Seq.
  • Clarify CAS semantics in CheckIncoming via updated inline documentation.
File summaries
File Description
bep44/item.go Corrects CAS validation logic in CheckIncoming to compare incoming.Cas against stored.Seq per BEP 44.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread bep44/item.go
Comment on lines +167 to 171
// cas (compare-and-swap) is the sequence number the writer expects to be
// replacing; per BEP 44 it must equal the *stored* item's current seq,
// otherwise the put is rejected. An absent cas (0) means "no cas".
if incoming.Cas != 0 && incoming.Cas != stored.Seq {
return ErrCasHashMismatched
Cover the cas acceptance and rejection matrix directly: an absent cas is
ignored, a cas equal to the stored seq is accepted, and a cas that differs from
the stored seq is rejected with ErrCasHashMismatched. The stored item's own Cas
field is not consulted, and the sequence number check still runs ahead of the
cas check.

TestWrapperCas walks the same path through Wrapper.Put with signed mutable
items, so a writer that still believes an older seq is current cannot overwrite
the newer value.

Copilot AI 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.

🟢 Approval recommended

The CAS validation bug is corrected in the right location and is backed by focused tests that exercise both direct validation and end-to-end Wrapper.Put behavior.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@dangowrt

Copy link
Copy Markdown
Author

Superseded by a9c967f, which compares cas against the stored sequence number in CheckIncoming() and adds tests for it.

@dangowrt dangowrt closed this Nov 14, 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.

3 participants