Skip to content

Fix pubsub TTL cache eviction - #203

Open
flcl42 wants to merge 7 commits into
sipsorcery-security-updatefrom
pubsub-ttl-cache
Open

Fix pubsub TTL cache eviction#203
flcl42 wants to merge 7 commits into
sipsorcery-security-updatefrom
pubsub-ttl-cache

Conversation

@flcl42

@flcl42 flcl42 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • remove expired TTL-cache entries by expiration time instead of key ordering
  • make cache access and disposal safe while a sweeper is active
  • add regression coverage for out-of-order keys and lazy expiration

Pipeline repair

This branch also includes the shared SIPSorcery security dependency update required for CI restore. It aligns the WebRTC certificate integration and is tracked independently in #208.

Validation

  • Focused cache regression coverage passes.

@flcl42
flcl42 requested a review from rubo as a code owner August 24, 2026 06:01
@flcl42
flcl42 requested a lite review from Copilot August 24, 2026 07:01

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.

Pull request overview

This PR updates the pubsub TTL cache implementation to evict entries based on their expiration timestamps (rather than key ordering), improves thread-safety around cache access/sweeping/disposal, and adds regression tests for eviction and lazy expiration behavior.

Changes:

  • Replaced key-ordered eviction with expiration-time-based eviction (RemoveExpired) and switched the backing store to a Dictionary.
  • Made cache reads (Contains, TryGet, ToList) honor TTL even if the sweeper hasn’t run yet.
  • Added NUnit regression tests covering out-of-order keys and lazy expiration.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

File Description
src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs Reworks TTL cache storage/eviction and adds synchronized access + cancellation-based sweeper disposal.
src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs Adds regression tests for eviction correctness and lazy expiration behavior.

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

Comment thread src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs Outdated
Comment thread src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs
Comment thread src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Suppressed comments (2)

src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:30

  • This test name implies it runs before the background sweeper executes, but with the current implementation the sweeper interval is min(5s, ttl). With ttl=25ms, the sweeper is expected to run very quickly, so the name is misleading.
    public void ExpiredEntries_AreNotReturned()

src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:54

  • Similar to the first test, the 25ms TTL and 60ms sleep are very tight for a time-based test; on a slow agent the replacement add/assert can race with timing jitter. Increasing the TTL and sleep margin will reduce flakiness.
        using TtlCache<MessageId, string> cache = new(25);
        MessageId id = new([0x01]);
        cache.Add(id, "expired");

        Thread.Sleep(60);
        cache.Add(id, "replacement");

Comment thread src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs Outdated

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

@flcl42
flcl42 changed the base branch from main to sipsorcery-security-update August 24, 2026 09:20
@flcl42
flcl42 requested a lite review from Copilot August 24, 2026 09:29

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Suppressed comments (2)

src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:36

  • Thread.Sleep makes this test timing-dependent and can be flaky on slow CI agents. Use a bounded retrying assertion to wait for expiry instead of sleeping a fixed duration.
        Thread.Sleep(750);

src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs:54

  • Thread.Sleep makes this test timing-dependent and can be flaky under load. Prefer waiting for expiry with a bounded retrying assertion, then proceed to add the replacement entry.
        Thread.Sleep(750);
        cache.Add(id, "replacement");

Comment thread src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs Outdated
Comment thread src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs
Comment thread src/libp2p/Libp2p.Protocols.Pubsub.Tests/TtlCacheTests.cs

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/libp2p/Libp2p.Protocols.Pubsub/TtlCache.cs Outdated
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.

2 participants