Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close tasks channel after WaitGroup.Wait #3

Merged
merged 1 commit into from
Feb 2, 2021

Conversation

tklauser
Copy link
Member

@tklauser tklauser commented Feb 2, 2021

We could potentially run into an issue with writing to WorkerPool.tasks
in case the channel is closed concurrently:

  goroutine 1:                         goroutine 2:
   Submit                               Close
    -> wp.wg.Add(1)
    -> scheduled by runtime
                                        -> close(wp.tasks)
                                        -> scheduled by runtime
    -> wp.tasks <- &task{...}
    -> panic: write to closed channel

The situation can be provoked in some cases using the following test:

func TestSubmitCloseConcurrent(t *testing.T) {
       n := runtime.NumCPU()
       wp := New(n)
       for i := 0; i < n+2; i++ {
               go func() {
                       _ = wp.Submit("", func() error {
                               time.Sleep(time.Millisecond)
                               return nil
                       })
               }()
       }
       wp.Close()
}

Avoid the panic case by closing the tasks channel after waiting for the
WaitGroup.

The reproducing test is not added yet, as there seem to be other
concurrency issues present which the test triggers.

We could potentially run into an issue with writing to WorkerPool.tasks
in case the channel is closed concurrently:

  goroutine 1:                         goroutine 2:
   Submit                               Close
    -> wp.wg.Add(1)
    -> scheduled by runtime
                                        -> close(wp.tasks)
                                        -> scheduled by runtime
    -> wp.tasks <- &task{...}
    -> panic: write to closed channel

The situation can be provoked in some cases using the following test:

```go
func TestSubmitCloseConcurrent(t *testing.T) {
       n := runtime.NumCPU()
       wp := New(n)
       for i := 0; i < n+2; i++ {
               go func() {
                       _ = wp.Submit("", func() error {
                               time.Sleep(time.Millisecond)
                               return nil
                       })
               }()
       }
       wp.Close()
}
```

Avoid the panic case by closing the tasks channel after waiting for the
WaitGroup.

The reproducing test is not added yet, as there seem to be other
concurrency issues present which the test triggers.

Signed-off-by: Tobias Klauser <tobias@isovalent.com>
@tklauser tklauser requested a review from rolinh February 2, 2021 19:17
Copy link
Member

@rolinh rolinh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

@rolinh rolinh merged commit 5ea8879 into master Feb 2, 2021
@rolinh rolinh deleted the pr/tklauser/chan-close-panic branch February 2, 2021 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants