Skip to content

Commit

Permalink
Merge pull request #107 from catundercar/master
Browse files Browse the repository at this point in the history
feat: fix goroutine leakage problem caused by errCh write blocking.
  • Loading branch information
generikvault authored Oct 18, 2024
2 parents 121093f + 713faca commit 9162467
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func toFunc(f interface{}) function {
if f, ok := f.(func(arguments ...interface{}) (interface{}, error)); ok {
return function(func(ctx context.Context, arguments ...interface{}) (interface{}, error) {
var v interface{}
errCh := make(chan error)
errCh := make(chan error, 1)
go func() {
defer func() {
if recovered := recover(); recovered != nil {
Expand All @@ -28,6 +28,7 @@ func toFunc(f interface{}) function {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-errCh:
close(errCh)
return v, err
}
})
Expand All @@ -40,7 +41,7 @@ func toFunc(f interface{}) function {
t := fun.Type()
return func(ctx context.Context, args ...interface{}) (interface{}, error) {
var v interface{}
errCh := make(chan error)
errCh := make(chan error, 1)
go func() {
defer func() {
if recovered := recover(); recovered != nil {
Expand Down Expand Up @@ -83,6 +84,7 @@ func toFunc(f interface{}) function {
case <-ctx.Done():
return nil, ctx.Err()
case err := <-errCh:
close(errCh)
return v, err
}
}
Expand Down

0 comments on commit 9162467

Please sign in to comment.