From c8766123d95403301b166cb1f0fda97bdf1e4ac4 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 --- plugins/gc/scheduler.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/gc/scheduler.go b/plugins/gc/scheduler.go index 3213ba3dd..ff78edee1 100644 --- a/plugins/gc/scheduler.go +++ b/plugins/gc/scheduler.go @@ -234,6 +234,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 @@ -331,6 +332,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) }