Skip to content

Commit

Permalink
fix: race condition by ensuring goroutine completes writing to stdin …
Browse files Browse the repository at this point in the history
…before going further

Signed-off-by: Prateek <[email protected]>
  • Loading branch information
Prateeknandle committed Nov 15, 2024
1 parent acc5ed8 commit 103b22a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions KubeArmor/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

kc "github.com/kubearmor/KubeArmor/KubeArmor/config"
Expand Down Expand Up @@ -291,7 +292,11 @@ func GetCommandOutputWithoutErr(cmd string, args []string) string {
return ""
}

var wg sync.WaitGroup
wg.Add(1)

go func() {
defer wg.Done()
defer func() {
if err = stdin.Close(); err != nil {
kg.Warnf("Error closing stdin %s\n", err)
Expand All @@ -300,6 +305,9 @@ func GetCommandOutputWithoutErr(cmd string, args []string) string {
_, _ = io.WriteString(stdin, "values written to stdin are passed to cmd's standard input")
}()

// Wait for the stdin writing to complete
wg.Wait()

out, err := res.CombinedOutput()
if err != nil {
return ""
Expand Down

0 comments on commit 103b22a

Please sign in to comment.