-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy iteration var into block-local to prevent clobbering spawned Gs.
- Loading branch information
Showing
1 changed file
with
6 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e9d9675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't Go's closure mechanics prevent this problem already? If not there may be other places with this problem as that was an assumption I had...
e9d9675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, it's a problem a lot of us have encountered now. It's surprising, yes? I fixed it everywhere we range over an iteration variable and spawn a goroutine, so the sarama codebase should be fine now.
e9d9675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found https://code.google.com/p/go-wiki/wiki/CommonMistakes which suggests parametrizing the go routine instead.
e9d9675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more difficult with the
withRecover
wrapper due to Go's lack of generics. Maybe there's some way to do it, but it wasn't immediately obvious to me.e9d9675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is why I wanted to put the defer call inside the go routines rather than passing to a wrapper in the first place. You could do it with reflection, but that seems unnecessarily complicated.