From df70735295fa0c43394fcb63f551a46e6a3aea84 Mon Sep 17 00:00:00 2001 From: AhmedGrati Date: Wed, 25 Jan 2023 17:08:16 +0100 Subject: [PATCH] Fix: Handle concurrent threads using mutex on the rainbowColor function Signed-off-by: AhmedGrati --- cmd/formatter/colors.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/formatter/colors.go b/cmd/formatter/colors.go index 9123e9555..e050a6aef 100644 --- a/cmd/formatter/colors.go +++ b/cmd/formatter/colors.go @@ -19,6 +19,7 @@ package formatter import ( "fmt" "strconv" + "sync" "github.com/docker/compose/v2/pkg/api" ) @@ -88,8 +89,11 @@ func makeColorFunc(code string) colorFunc { var nextColor = rainbowColor var rainbow []colorFunc var currentIndex = 0 +var mutex sync.Mutex func rainbowColor() colorFunc { + mutex.Lock() + defer mutex.Unlock() result := rainbow[currentIndex] currentIndex = (currentIndex + 1) % len(rainbow) return result