Skip to content

Commit 697fa89

Browse files
liu-congnirrozenbaum
authored andcommitted
Fix a regression in prefix plugin which can cause data race (#1188)
1 parent 7fa8fc0 commit 697fa89

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

pkg/epp/scheduling/framework/plugins/multi/prefix/indexer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ func (i *indexer) Get(hash BlockHash) podSet {
8585
i.mu.RLock()
8686
defer i.mu.RUnlock()
8787

88-
res := podSet{}
89-
pods, ok := i.hashToPods[hash]
90-
if !ok {
91-
return res
88+
pods := i.hashToPods[hash]
89+
res := make(podSet, len(pods))
90+
for pod := range pods {
91+
// Deep copy to avoid race condition.
92+
res[pod] = struct{}{}
9293
}
9394

94-
return pods
95+
return res
9596
}
9697

9798
// makeEvictionFn returns a per-pod LRU eviction callback that removes the pod from hashToPods on eviction.

pkg/epp/scheduling/framework/plugins/multi/prefix/indexer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ func TestIndexer_AddAndGet(t *testing.T) {
4141
// Add another entry to the cache, which should evict the first one due to max size.
4242
i.Add([]BlockHash{BlockHash(3)}, server)
4343
assert.Equal(t, 2, i.podToLRU[server].Len(), "Cache size should still be 2 after adding an entry")
44+
45+
servers = i.Get(BlockHash(4))
46+
assert.Empty(t, servers, "Cache should not contain non-existent hash")
4447
}

0 commit comments

Comments
 (0)