diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-10-20 19:01:38 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-10-20 19:42:19 +0800 |
commit | 65738c1eb37c789294124cc9b4bbe6d55d50a77f (patch) | |
tree | c107c095aa00a0e9f12c289f84d9765ba4ea25da /event/feed.go | |
parent | 0e7d019e0eb3a4929c504ac5899fbe55c4227f7e (diff) | |
download | dexon-65738c1eb37c789294124cc9b4bbe6d55d50a77f.tar.gz dexon-65738c1eb37c789294124cc9b4bbe6d55d50a77f.tar.zst dexon-65738c1eb37c789294124cc9b4bbe6d55d50a77f.zip |
event: fix datarace between Subscribe and Send
Diffstat (limited to 'event/feed.go')
-rw-r--r-- | event/feed.go | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/event/feed.go b/event/feed.go index b1b597f17..78fa3d98d 100644 --- a/event/feed.go +++ b/event/feed.go @@ -127,6 +127,8 @@ 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 @@ -134,14 +136,14 @@ func (f *Feed) Send(value interface{}) (nsent int) { 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 } |