HuobiRDCenter/huobi_Golang

WebSocketV1ClientBase.lastReceivedTime field data race

filinvadim opened this issue · 6 comments

WARNING: DATA RACE
Write at 0x00c000158128 by goroutine 56:
  .../pkg/client/websocketclientbase.(*WebSocketV1ClientBase).readLoop()
     .../pkg/client/websocketclientbase/websocketv1clientbase.go:212 +0x23c

Previous write at 0x00c000158128 by goroutine 16:
 .../pkg/client/websocketclientbase.(*WebSocketV1ClientBase).startTicker()
     .../pkg/client/websocketclientbase/websocketv1clientbase.go:137 +0xbc

Just:

p.timeMutex.Lock()
p.lastReceivedTime = time.Now()
p.timeMutex.Unlock()

Same for WebSocketV2ClientBase

@filinvadim thanks for the feedback, usually this won’t happen, however it has possibility in theory as lastReceivedTime is written in two routines. I don’t want to introduce mutex, so just move the lastReceivedTime initialization to earlier places. You can check the latest master branch.

@eynzhang so data race is still in place. You need a mutex, sorry.

@filinvadim could you share how you reproduce this issue?

Sorry no, @eynzhang . It's in your code. Just go run --race ...
You have access to the variable lastReceivedTime from 2 threads - tickerLoop and readLoop. That's data race. Reopen please to warn other developers.