From 5c15bf406da3a40d19ba89c7cd90080047d3793e Mon Sep 17 00:00:00 2001 From: "Kirill A. Korinsky" Date: Thu, 15 Feb 2024 11:11:29 +0100 Subject: [PATCH] Prevent GC from schedule itself with 0 period. On startup `gcTimeSum` might work fast and return `0`, so on this case the algorithm turns in infinity loop which simple consume CPU on timer which fires without any interval. Use `5ms` as fallback to have interval `245ms` for that case. Closes: https://github.com/containerd/containerd/issues/5089 Signed-off-by: Kirill A. Korinsky --- gc/scheduler/scheduler.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gc/scheduler/scheduler.go b/gc/scheduler/scheduler.go index af563dd96c..04b96c8f86 100644 --- a/gc/scheduler/scheduler.go +++ b/gc/scheduler/scheduler.go @@ -246,6 +246,7 @@ func schedule(d time.Duration) (<-chan time.Time, *time.Time) { } func (s *gcScheduler) run(ctx context.Context) { + const minimumGCTime = float64(5 * time.Millisecond) var ( schedC <-chan time.Time @@ -343,6 +344,11 @@ func (s *gcScheduler) run(ctx context.Context) { // runtime in between gc to reach the pause threshold. // Pause threshold is always 0.0 < threshold <= 0.5 avg := float64(gcTimeSum) / float64(collections) + // Enforce that avg is no less than minimumGCTime + // to prevent immediate rescheduling + if avg < minimumGCTime { + avg = minimumGCTime + } interval = time.Duration(avg/s.pauseThreshold - avg) }