diff --git a/docs/changelog/99480.yaml b/docs/changelog/99480.yaml new file mode 100644 index 0000000000000..08dcdceca60b0 --- /dev/null +++ b/docs/changelog/99480.yaml @@ -0,0 +1,6 @@ +pr: 99480 +summary: Fix deadlock between Cache.put and Cache.invalidateAll +area: Infra/Core +type: bug +issues: + - 99326 diff --git a/server/src/main/java/org/elasticsearch/common/cache/Cache.java b/server/src/main/java/org/elasticsearch/common/cache/Cache.java index bb4bd0062b8db..7cd6fa471040a 100644 --- a/server/src/main/java/org/elasticsearch/common/cache/Cache.java +++ b/server/src/main/java/org/elasticsearch/common/cache/Cache.java @@ -521,12 +521,12 @@ public void invalidateAll() { Entry h; boolean[] haveSegmentLock = new boolean[NUMBER_OF_SEGMENTS]; - try { - for (int i = 0; i < NUMBER_OF_SEGMENTS; i++) { - segments[i].segmentLock.writeLock().lock(); - haveSegmentLock[i] = true; - } - try (ReleasableLock ignored = lruLock.acquire()) { + try (ReleasableLock ignored = lruLock.acquire()) { + try { + for (int i = 0; i < NUMBER_OF_SEGMENTS; i++) { + segments[i].segmentLock.writeLock().lock(); + haveSegmentLock[i] = true; + } h = head; for (CacheSegment segment : segments) { segment.map = null; @@ -539,11 +539,11 @@ public void invalidateAll() { head = tail = null; count = 0; weight = 0; - } - } finally { - for (int i = NUMBER_OF_SEGMENTS - 1; i >= 0; i--) { - if (haveSegmentLock[i]) { - segments[i].segmentLock.writeLock().unlock(); + } finally { + for (int i = NUMBER_OF_SEGMENTS - 1; i >= 0; i--) { + if (haveSegmentLock[i]) { + segments[i].segmentLock.writeLock().unlock(); + } } } }