test(lru): demonstrate TOCTOU race in lru implementation#4865
test(lru): demonstrate TOCTOU race in lru implementation#4865kislaykishore wants to merge 2 commits into
Conversation
…trictly positive delta
There was a problem hiding this comment.
Code Review
This pull request adds a new test, TestMapCache_UpdateSize_TOCTOU_Underflow, to demonstrate a TOCTOU race and size accounting underflow in the LRU cache. The reviewer pointed out that the cache size of 100 in the test is too small and can lead to false-positive test failures when cumulative updates exceed the cache limit. They suggested increasing the cache size to 2000 to ensure the test only fails on actual underflow issues.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4865 +/- ##
==========================================
+ Coverage 83.68% 83.72% +0.04%
==========================================
Files 170 170
Lines 20948 20948
==========================================
+ Hits 17530 17539 +9
+ Misses 2762 2757 -5
+ Partials 656 652 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Description
When downloading sparse files, cached objects grow incrementally from 0 bytes as chunks arrive. Callers invoke
cache.UpdateSize(key, delta)to dynamically update LRU size accounting (currentSize). (Note: Normal sequential file-cache is immune to this issue because its full file size is allocated upfront at insertion time).Erase(key)after its in-memory size changes but beforeUpdateSizefinishes,Erasesubtracts the new size instead of the size known to the cache.currentSizeunderflows (wrapping around unsigneduint64), leading to invariant check panics or aggressive, endless cache eviction. Additionally, if the same key is rapidly re-inserted, stale deltas from the old object can corrupt the new entry's accounting.Link to the issue in case of a bug fix.
N/A
Testing details
go test -v ./internal/cache/lru/...onTestMapCache_UpdateSize_TOCTOU_UnderflowwithmaxSize=2000which successfully reproduced and demonstrated the underflow (CurrentSize 18446744073709551586 over maxSize 2000).Any backward incompatible change? If so, please explain.
N/A