shithub: hugo

Download patch

ref: 355736ec357c81dfb2eb6851ee019d407090c5ec
parent: 93a447c5dd053e1c934036a66ebd05b95250a1d1
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Apr 29 21:10:03 EDT 2017

livereload: Fix data race in close

Fixes #2625

--- a/livereload/connection.go
+++ b/livereload/connection.go
@@ -15,6 +15,7 @@
 
 import (
 	"bytes"
+	"sync"
 
 	"github.com/gorilla/websocket"
 )
@@ -25,6 +26,16 @@
 
 	// 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() {
--- a/livereload/hub.go
+++ b/livereload/hub.go
@@ -41,7 +41,7 @@
 			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 {
@@ -48,7 +48,7 @@
 				case c.send <- m:
 				default:
 					delete(h.connections, c)
-					close(c.send)
+					c.close()
 				}
 			}
 		}