Skip to content

Commit

Permalink
livereload: Fix data race in close
Browse files Browse the repository at this point in the history
Fixes #2625
  • Loading branch information
bep committed Apr 29, 2017
1 parent 93a447c commit 355736e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions livereload/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package livereload

import (
"bytes"
"sync"

"github.com/gorilla/websocket"
)
Expand All @@ -25,6 +26,16 @@ type connection struct {

// Buffered channel of outbound messages.
send chan []byte

// There is a potential data race, especially visible with large files.
// This is protected by synchronisation of the send channel's close.
closer sync.Once
}

func (c *connection) close() {
c.closer.Do(func() {
close(c.send)
})
}

func (c *connection) reader() {
Expand Down
4 changes: 2 additions & 2 deletions livereload/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func (h *hub) run() {
h.connections[c] = true
case c := <-h.unregister:
delete(h.connections, c)
close(c.send)
c.close()
case m := <-h.broadcast:
for c := range h.connections {
select {
case c.send <- m:
default:
delete(h.connections, c)
close(c.send)
c.close()
}
}
}
Expand Down

0 comments on commit 355736e

Please sign in to comment.