Skip to content

Commit 65738c1

Browse files
committed
event: fix datarace between Subscribe and Send
1 parent 0e7d019 commit 65738c1

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎event/feed.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,23 @@ func (f *Feed) remove(sub *feedSub) {
127127
// Send delivers to all subscribed channels simultaneously.
128128
// It returns the number of subscribers that the value was sent to.
129129
func (f *Feed) Send(value interface{}) (nsent int) {
130+
rvalue := reflect.ValueOf(value)
131+
130132
f.once.Do(f.init)
131133
<-f.sendLock
132134

133135
// Add new cases from the inbox after taking the send lock.
134136
f.mu.Lock()
135137
f.sendCases = append(f.sendCases, f.inbox...)
136138
f.inbox = nil
137-
f.mu.Unlock()
138139

139-
// Set the sent value on all channels.
140-
rvalue := reflect.ValueOf(value)
141140
if !f.typecheck(rvalue.Type()) {
142141
f.sendLock <- struct{}{}
143142
panic(feedTypeError{op: "Send", got: rvalue.Type(), want: f.etype})
144143
}
144+
f.mu.Unlock()
145+
146+
// Set the sent value on all channels.
145147
for i := firstSubSendCase; i < len(f.sendCases); i++ {
146148
f.sendCases[i].Send = rvalue
147149
}

0 commit comments

Comments
 (0)