Skip to content

Commit f7bc0ac

Browse files
committed
fix: goroutine leak
1 parent 779f153 commit f7bc0ac

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
88
github.com/imdario/mergo v0.3.13
99
github.com/stretchr/testify v1.8.1
10+
go.uber.org/goleak v1.3.0
1011
gopkg.in/evanphx/json-patch.v5 v5.6.0
1112
gopkg.in/yaml.v2 v2.4.0
1213
k8s.io/kube-openapi v0.0.0-20230601164746-7562a1006961

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=
6161
github.com/xlab/treeprint v1.2.0/go.mod h1:gj5Gd3gPdKtR1ikdDK6fnFLdmIS0X30kTTuNd/WEJu0=
6262
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee331t6JAXeK2bcyhLOOc=
6363
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o=
64+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
65+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
6466
golang.org/x/sys v0.0.0-20191002063906-3421d5a6bb1c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6567
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
6668
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

api/internal/utils/timedcall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// TimedCall runs fn, failing if it doesn't complete in the given duration.
1111
// The description is used in the timeout error message.
1212
func TimedCall(description string, d time.Duration, fn func() error) error {
13-
done := make(chan error)
13+
done := make(chan error, 1)
1414
timer := time.NewTimer(d)
1515
defer timer.Stop()
1616
go func() { done <- fn() }()

api/internal/utils/timedcall_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/stretchr/testify/assert"
12+
"go.uber.org/goleak"
1213
. "sigs.k8s.io/kustomize/api/internal/utils"
1314
)
1415

@@ -62,3 +63,20 @@ func TestTimedCallSlowWithError(t *testing.T) {
6263
t.Fail()
6364
}
6465
}
66+
67+
func TestTimedCallGoroutineLeak(t *testing.T) {
68+
defer goleak.VerifyNone(t)
69+
err := TimedCall("function done, no goroutine leaks", timeToWait, func() error {
70+
time.Sleep(tooSlow)
71+
return fmt.Errorf("function done")
72+
})
73+
if assert.Error(t, err) {
74+
assert.EqualError(t, err, errMsg("function done, no goroutine leaks"))
75+
} else {
76+
t.Fail()
77+
}
78+
79+
// The code introduces a 2-second sleep to allow the goroutine to complete its execution.
80+
// Subsequently, it verifies if the goroutine created by the function exits as expected.
81+
time.Sleep(tooSlow)
82+
}

0 commit comments

Comments
 (0)