Fix a data race related to readErrCount

This commit is contained in:
Ruben Vermeersch 2018-01-25 13:07:12 +01:00
parent 292fd08b25
commit 50391a1a84
1 changed files with 4 additions and 3 deletions

View File

@ -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")
}