Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix evictable cache invalidation race condition #22302

Conversation

findepi
Copy link
Member

@findepi findepi commented Jun 6, 2024

Before the change the following race was possible between threads A, B, C:

  • A calls invalidate(K)
  • A increments: invalidations++
  • B changes state to be cached, and therefore calls invalidate(K) too
  • B increments: invalidations++
  • C calls get(K)
  • C reads invalidations counter
  • C retrieves current token T for key K
  • C reads value V for T from cache
  • A reads and removes token T (same) for key K
  • B attempts to read and remove token for key K, not found
  • B exits invalidate(K)
  • C checks invalidations counter (didn't check)
  • C revives, i.e. re-inserts token T for key K
  • B calls get(K)
  • B retrieves token T (same) for key K
  • B reads value V for T from cache -- despite B having called invalidate(K)

At least in this situation the problem is transient. Thread A will momentarily invalidate dataCache for token T, completing the invalidation.

This commit fixes this. The bug was introduced by token reviving (commit 17faae3). This commit reverts that one and provides a different solution to the problem that commit was solving.

Fixes #22285

Release notes

(x) Release notes are required -- this fixes correctness problems in a couple different connectors.

@findepi findepi requested review from dain and losipiuk June 6, 2024 12:28
@cla-bot cla-bot bot added the cla-signed label Jun 6, 2024
@findepi findepi marked this pull request as draft June 6, 2024 16:22
@findepi findepi force-pushed the findepi/fix-evictable-cache-invalidation-race-condition-f18e21 branch from d4115a0 to e3f218d Compare June 7, 2024 08:19
@findepi findepi marked this pull request as ready for review June 7, 2024 08:19
@findepi
Copy link
Member Author

findepi commented Jun 11, 2024

@findepi findepi force-pushed the findepi/fix-evictable-cache-invalidation-race-condition-f18e21 branch from e3f218d to edd6e34 Compare June 11, 2024 10:26
Before the change the following race was possible between threads A, B, C:

- A calls invalidate(K)
- A increments: invalidations++
- B changes state to be cached, and therefore calls invalidate(K) too
- B increments: invalidations++
- C calls get(K)
- C reads invalidations counter
- C retrieves current token T for key K
- C reads value V for T from cache
- A reads and removes token T (same) for key K
- B attempts to read and remove token for key K, not found
- B exits invalidate(K)
- C checks invalidations counter (didn't check)
- C revives, i.e. re-inserts token T for key K
- B calls get(K)
- B retrieves token T (same) for key K
- B reads value V for T from cache -- despite B having called
  invalidate(K)

At least in this situation the problem is transient. Thread A will momentarily
invalidate dataCache for token T, completing the invalidation.

This commit fixes this. The bug was introduced by token reviving (commit
17faae3). This commit reverts that one
and provides a different solution to the problem that commit was
solving.
@findepi findepi force-pushed the findepi/fix-evictable-cache-invalidation-race-condition-f18e21 branch from 97ead3c to 9d535cc Compare June 13, 2024 09:04
@findepi findepi merged commit e9062c0 into trinodb:master Jun 13, 2024
95 checks passed
@findepi findepi deleted the findepi/fix-evictable-cache-invalidation-race-condition-f18e21 branch June 13, 2024 10:23
@github-actions github-actions bot added this to the 450 milestone Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

EvictableCache race condition can temporarily revive old entry
2 participants