Skip to content

Commit aaef19f

Browse files
committed
remotecache: avoid ultra deep recursion for complex cache layouts
1 parent 070d993 commit aaef19f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

cache/remotecache/v1/utils.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,21 @@ func (s *normalizeState) removeLoops(ctx context.Context) {
134134
}
135135

136136
visited := map[digest.Digest]struct{}{}
137+
processed := map[digest.Digest]struct{}{}
137138

138139
for _, d := range roots {
139-
s.checkLoops(ctx, d, visited)
140+
s.checkLoops(ctx, d, visited, processed)
140141
}
141142
}
142143

143-
func (s *normalizeState) checkLoops(ctx context.Context, d digest.Digest, visited map[digest.Digest]struct{}) {
144+
func (s *normalizeState) checkLoops(ctx context.Context, d digest.Digest, visited map[digest.Digest]struct{}, processed map[digest.Digest]struct{}) {
145+
if _, ok := processed[d]; ok {
146+
return
147+
}
148+
defer func() {
149+
processed[d] = struct{}{}
150+
}()
151+
144152
it, ok := s.byKey[d]
145153
if !ok {
146154
return
@@ -166,7 +174,7 @@ func (s *normalizeState) checkLoops(ctx context.Context, d digest.Digest, visite
166174
}
167175
delete(links[l], id)
168176
} else {
169-
s.checkLoops(ctx, id, visited)
177+
s.checkLoops(ctx, id, visited, processed)
170178
}
171179
}
172180
}

0 commit comments

Comments
 (0)