From 50391a1a842a2099284e15c8ddd3961604d67b83 Mon Sep 17 00:00:00 2001 From: Ruben Vermeersch Date: Thu, 25 Jan 2018 13:07:12 +0100 Subject: [PATCH] Fix a data race related to readErrCount --- conn.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/conn.go b/conn.go index cd3569d..5127bb9 100644 --- a/conn.go +++ b/conn.go @@ -14,6 +14,7 @@ import ( "net" "strconv" "sync" + "sync/atomic" "time" "unicode/utf8" ) @@ -256,7 +257,7 @@ type Conn struct { handlePong func(string) error handlePing func(string) error handleClose func(int, string) error - readErrCount int + readErrCount int32 messageReader *messageReader // the current low-level reader readDecompress bool // whether last read frame had RSV1 set @@ -955,8 +956,8 @@ func (c *Conn) NextReader() (messageType int, r io.Reader, err error) { // Applications that do handle the error returned from this method spin in // tight loop on connection failure. To help application developers detect // this error, panic on repeated reads to the failed connection. - c.readErrCount++ - if c.readErrCount >= 1000 { + count := atomic.AddInt32(&c.readErrCount, 1) + if count >= 1000 { panic("repeated read on failed websocket connection") }