From c659a98587ea66204afc1037d4c8a217a79ba55f Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 26 Jan 2021 18:01:06 +0100 Subject: [PATCH] Prevent multiple close of channel --- emlid/reachview/broadcast.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/emlid/reachview/broadcast.go b/emlid/reachview/broadcast.go index d944994..9e2bc5d 100644 --- a/emlid/reachview/broadcast.go +++ b/emlid/reachview/broadcast.go @@ -2,6 +2,7 @@ package reachview import ( "context" + "sync" gosocketio "forge.cadoles.com/Pyxis/golang-socketio" "github.com/mitchellh/mapstructure" @@ -31,6 +32,8 @@ type Broadcast struct { func (c *Client) Broadcast(ctx context.Context) (chan Broadcast, error) { out := make(chan Broadcast) + closer := new(sync.Once) + handler := func(_ *gosocketio.Channel, data interface{}) { res := Broadcast{} if err := mapstructure.WeakDecode(data, &res); err != nil { @@ -38,8 +41,12 @@ func (c *Client) Broadcast(ctx context.Context) (chan Broadcast, error) { } select { case <-ctx.Done(): - c.Off(eventBroadcast) - close(out) + closer.Do(func() { + c.Off(eventBroadcast) + close(out) + }) + + return default: out <- res }