Skip to content

Conversation

@nickita-khylkouski
Copy link

Summary

When compute() returns null for an entry whose value has been garbage collected, the removal cause should be COLLECTED, not EXPLICIT.

Changes

  • Fix: In LocalCache.Segment.compute(), before calling removeEntry(), check if the original value was null and the reference is still active. If so, use RemovalCause.COLLECTED instead of RemovalCause.EXPLICIT.

  • Test: Added testComputeRemovalCause_collected() that simulates garbage collection of a cached value and verifies that compute() returning null reports COLLECTED cause.

Motivation

Reported by @ben-manes in #7985:

After the compute locks it runs preWriteCleanup() which performs an amortized drain of the pending work. However it does not perform all that might have built up since it is on a caller's thread. After this is performed, an existing entry may be found that is pending eviction but still present. In this test it returns null, so which leads to the mapping removal, and the listener reports this as an EXPLICIT removal cause with a null value.

The null value in the removal notification should only occur for a COLLECTED cause.

Testing

The new test:

  1. Creates a cache with soft values
  2. Puts an entry
  3. Clears the value reference to simulate garbage collection
  4. Calls compute() which returns null
  5. Verifies the removal notification has COLLECTED cause

Fixes #7985

…entries

When compute() returns null for an entry whose value has been garbage
collected, the removal cause should be COLLECTED, not EXPLICIT.

The fix checks if the original value was null and the reference is still
active (not loading), which indicates the value was garbage collected
rather than explicitly removed by the user.

Fixes google#7985
@nickita-khylkouski nickita-khylkouski force-pushed the fix-compute-collected-removal-cause branch from bc82868 to 217afb9 Compare January 26, 2026 19:20
@eamonnmcmanus eamonnmcmanus self-requested a review January 28, 2026 16:08
@eamonnmcmanus eamonnmcmanus added the P3 no SLO label Jan 28, 2026
Error Prone requires compute() return value to be used. Using
Object unused to satisfy the check (var not available with source 1.8).
@nickita-khylkouski nickita-khylkouski force-pushed the fix-compute-collected-removal-cause branch from 41c441e to 3386500 Compare January 28, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P3 no SLO

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache's compute misreports the removal cause of a collected entry

2 participants