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

Fix another hypothetical wait group issue #451

Merged
merged 1 commit into from
May 20, 2015
Merged

Conversation

eapache
Copy link
Contributor

@eapache eapache commented May 18, 2015

Don't increment and immediately decrement the wait group on a message which we
just return with ErrShuttingDown. Otherwise:

prod.AsyncClose() // starts prod.shutdown() goroutine
prod.Input() <- msg
  1. shutdown() goroutine calls Wait()
  2. while in the call to Wait(), Go switches contexts
  3. the message is sent on Input(), is received, and causes the wait counter to
    increment from 0 while being waited on

Where the WaitGroup docs say "calls with a positive delta that occur when the
counter is zero must happen before a Wait".

@Shopify/kafka @wvanbergen hopefully this explanation makes sense, it was the second issue I came up with from #450.

Don't increment and immediately decrement the wait group on a message which we
just return with ErrShuttingDown. Otherwise:
```
prod.AsyncClose()
prod.Input() <- msg
```
1. shutdown() goroutine calls Wait()
2. while in the call to Wait(), Go switches contexts
3. the message is sent on Input(), is received, and causes the wait counter to
   increment from 0 while being waited on

Where the WaitGroup docs say "calls with a positive delta that occur when the
counter is zero must happen before a Wait".
@eapache
Copy link
Contributor Author

eapache commented May 18, 2015

Oh hmm. Willem's fix may have already fixed this case (though more subtly) because once the shutdown message has arrived and caused Done() to be called the wait group's internal semaphore has already been triggered, causing the Wait() in shutdown to unblock, so it probably doesn't matter that the increment happens from 0 because the wait isn't really waiting anymore? I'm not sure.

Better safe than sorry on this I guess?

@wvanbergen
Copy link
Contributor

Yeah, I don't think this is strictly necessary, but it doesn't hurt either. Given that the message never really is "in flight", it makes sense not to increment the counter for this.

eapache added a commit that referenced this pull request May 20, 2015
Fix another hypothetical wait group issue
@eapache eapache merged commit e8ad5e2 into master May 20, 2015
@eapache eapache deleted the wait-group-shutdown-error branch May 20, 2015 15:20
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