Skip to content

Commit

Permalink
event: fix datarace between Subscribe and Send
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Oct 20, 2017
1 parent 0e7d019 commit 65738c1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions event/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,23 @@ func (f *Feed) remove(sub *feedSub) {
// Send delivers to all subscribed channels simultaneously.
// It returns the number of subscribers that the value was sent to.
func (f *Feed) Send(value interface{}) (nsent int) {
rvalue := reflect.ValueOf(value)

f.once.Do(f.init)
<-f.sendLock

// Add new cases from the inbox after taking the send lock.
f.mu.Lock()
f.sendCases = append(f.sendCases, f.inbox...)
f.inbox = nil
f.mu.Unlock()

// Set the sent value on all channels.
rvalue := reflect.ValueOf(value)
if !f.typecheck(rvalue.Type()) {
f.sendLock <- struct{}{}
panic(feedTypeError{op: "Send", got: rvalue.Type(), want: f.etype})
}
f.mu.Unlock()

// Set the sent value on all channels.
for i := firstSubSendCase; i < len(f.sendCases); i++ {
f.sendCases[i].Send = rvalue
}
Expand Down

0 comments on commit 65738c1

Please sign in to comment.