Skip to content

Commit

Permalink
Merge pull request #7463 from tinyspeck/am_error_group_zero
Browse files Browse the repository at this point in the history
[concurrency] Add guard against potentially blocking forever in ErrorGroup.Wait() when NumGoroutines is 0
  • Loading branch information
deepthi authored Feb 8, 2021
2 parents 3c5e2c2 + d1e07fc commit 39df6cd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go/vt/concurrency/error_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (eg ErrorGroup) Wait(cancel context.CancelFunc, errors chan error) *AllErro
responseCounter := 0
rec := &AllErrorRecorder{}

if eg.NumGoroutines < 1 {
return rec
}

for err := range errors {
responseCounter++

Expand Down
34 changes: 34 additions & 0 deletions go/vt/concurrency/error_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package concurrency

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestErrorGroup(t *testing.T) {
t.Run("Wait() returns immediately when NumGoroutines = 0", func(t *testing.T) {
eg := ErrorGroup{
NumGoroutines: 0,
}

rec := eg.Wait(nil, nil)
assert.NoError(t, rec.Error())
})
}

0 comments on commit 39df6cd

Please sign in to comment.