Skip to content

Commit

Permalink
fix: goroutine leak
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Oct 15, 2023
1 parent 779f153 commit f8eed9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/internal/utils/timedcall.go
Expand Up @@ -10,7 +10,7 @@ import (
// TimedCall runs fn, failing if it doesn't complete in the given duration.
// The description is used in the timeout error message.
func TimedCall(description string, d time.Duration, fn func() error) error {
done := make(chan error)
done := make(chan error, 1)
timer := time.NewTimer(d)
defer timer.Stop()
go func() { done <- fn() }()
Expand Down
13 changes: 13 additions & 0 deletions api/internal/utils/timedcall_test.go
Expand Up @@ -5,6 +5,7 @@ package utils_test

import (
"fmt"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -62,3 +63,15 @@ func TestTimedCallSlowWithError(t *testing.T) {
t.Fail()
}
}

func TestTimedCallGoroutineLeak(t *testing.T) {
beforeGroutine := runtime.NumGoroutine()
_ = TimedCall("expect no goroutine leaks", time.Duration(time.Second), func() error {

Check failure on line 69 in api/internal/utils/timedcall_test.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary conversion (unconvert)
time.Sleep(2 * time.Second)
return fmt.Errorf("not done")
})
afterGoroutine := runtime.NumGoroutine()
if beforeGroutine != afterGoroutine {
t.Fatal("goroutine leak")
}
}

0 comments on commit f8eed9a

Please sign in to comment.