Remove only the expired cache entry TryGet observed, and saturate the TTL [patch] - #62
Merged
Merged
Conversation
… TTL TryGet removed whatever was stored under the key after seeing an expired entry, so a value another thread set in between was deleted. It now removes the entry by key and value. Set treated TimeSpan.MaxValue as an overflow and threw; a time-to-live past DateTime.MaxValue now never expires. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CzC1o7LFYHYaoWmVy1NLcp
|
This was referenced Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #47
What changed
Race in
TryGet. WhenTryGetsaw an expired entry, it removed whatever was stored under the key at that moment. If another thread calledSetin between, the new value was deleted. It now removes the entry by key and value, so it only removes the entry it read:TryRemove(KeyValuePair)on .NET 5+ICollection<KeyValuePair>.Removeon netstandard2.1Both compare the value, and
CacheEntryis compared by reference, so a replacement set by another thread stays in place.Overflow in
Set.Set(key, value, TimeSpan.MaxValue)used to throwArgumentOutOfRangeException. A newComputeExpirationsaturates instead. A time-to-live that reaches pastDateTime.MaxValuenever expires. One that reaches beforeDateTime.MinValuecounts as already expired.Tests
Cache_Expired_TryGet_Does_Not_Remove_A_Value_Set_Concurrentlymakes the race deterministic instead of relying on a stress loop.TryGethashes the key twice: once to read the entry and once to remove it. The test uses a key type that runsSet(key, 2)on the second hash, which puts the other thread'sSetexactly in that window. The test then checks that the fresh value survives.Cache_Set_With_MaxValue_Expiration_Never_ExpiresandCache_Set_With_MinValue_Expiration_Has_Already_Expiredcover the saturation.Verification
InMemoryCacheProvider.csreverted, all three new tests fail. The race test finds the value it just set is gone, and the MaxValue/MinValue tests throwArgumentOutOfRangeException.CLAUDE.md's description ofCacheProviderTests.csnow mentions the new tests.🤖 Generated with Claude Code
https://claude.ai/code/session_01CzC1o7LFYHYaoWmVy1NLcp
Generated by Claude Code