Skip to content

fix(cache): cancel-safe dedup of concurrent origin pull-through - #330

Merged
alpergundogdu merged 1 commit into
mainfrom
fix/small-correctness-bugs
Apr 27, 2026
Merged

alpergundogdu merged 1 commit into
mainfrom
fix/small-correctness-bugs

Conversation

@alpergundogdu

@alpergundogdu alpergundogdu commented Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Concurrent CacheEngine::get calls for the same uncached hash now coalesce into a single origin fetch via an inflight Notify map. The check-and-insert is a single lock acquisition, eliminating the race where multiple tasks could see an empty map and all proceed to pull.

Cleanup is wrapped in a Drop guard (InflightGuard) so the entry is removed and waiters are woken even when the owning future is cancelled mid-pull — without this, a cancelled pull would orphan the entry and hang every subsequent request for the same hash forever.

Closes #305

Notes

Test plan

  • cargo nextest run -p decdn-cache — 37 tests pass
  • New tests:
    • concurrent_gets_coalesce_into_single_origin_fetch — 5 concurrent gets → 1 origin fetch
    • inflight_map_is_empty_after_pull_completes — cleanup on success
    • cancelled_owner_does_not_orphan_inflight_entry — cleanup on cancellation
  • cargo clippy — clean
  • cargo fmt -- --check — clean

🤖 Generated with Claude Code

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request implements request coalescing in the cache engine to prevent redundant origin fetches and introduces a rate_per_mb field to the gossip protocol. It also adds validation for OTLP endpoints and improves gossip version checking. Feedback highlights a race condition in the coalescing logic that could still allow duplicate fetches and a wire-compatibility issue caused by the structural changes to the gossip protocol's announcement body.

Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/protocol/src/gossip.rs Resolved
@alpergundogdu
alpergundogdu force-pushed the fix/small-correctness-bugs branch from ca0824c to d0f3778 Compare April 27, 2026 14:00
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces request coalescing in the cache engine to prevent duplicate origin fetches and adds a rate_per_mb field to the NodeAnnounceBody struct. Feedback identifies a critical cancellation safety issue in the coalescing logic that could lead to task hangs if a future is dropped. Additionally, the modification of NodeAnnounceBody is flagged for breaking backward compatibility and violating architectural constraints regarding frozen wire formats.

Comment thread crates/cache/src/engine.rs Resolved
Comment thread crates/protocol/src/gossip.rs Resolved
…lescing

Concurrent CacheEngine::get calls for the same uncached hash now coalesce
into a single origin fetch via an inflight Notify map. The check-and-insert
is a single lock acquisition, eliminating the race where multiple tasks
could see an empty map and all proceed to pull.

Cleanup is wrapped in a Drop guard (InflightGuard) so the entry is
removed and waiters are woken even when the owning future is cancelled
mid-pull — without this, a cancelled pull would orphan the entry and
hang every subsequent request for the same hash forever.

Closes #305

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alpergundogdu
alpergundogdu force-pushed the fix/small-correctness-bugs branch from 2770763 to 4632210 Compare April 27, 2026 15:00
@alpergundogdu alpergundogdu changed the title feat(protocol,cache): rate_per_mb in NodeAnnounce + dedup concurrent pulls fix(cache): cancel-safe dedup of concurrent origin pull-through Apr 27, 2026
@alpergundogdu

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request implements request coalescing in the cache engine to prevent redundant origin fetches for the same hash. It introduces an inflight map tracked via tokio::sync::Notify and utilizes an RAII InflightGuard to ensure that map entries are cleared and waiting tasks are notified, even if the owning task is cancelled or panics. New tests verify that concurrent requests are correctly coalesced and that the internal state is properly cleaned up. I have no feedback to provide.

@alpergundogdu
alpergundogdu merged commit 8f014e3 into main Apr 27, 2026
10 checks passed
@alpergundogdu
alpergundogdu deleted the fix/small-correctness-bugs branch April 27, 2026 15:11
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.

cache: concurrent origin pull-through for the same hash can result in duplicate fetches and redundant insertions into iroh-blobs

1 participant