samuel / go-zookeeper

Native ZooKeeper client for Go. This project is no longer maintained. Please use https://github.com/go-zookeeper/zk instead.
BSD 3-Clause "New" or "Revised" License
1.64k stars 672 forks source link

data race in conn.go #260

Open huaz836 opened 3 years ago

huaz836 commented 3 years ago

Reproduce this case when test my app with go race. The log:

2021/06/21 15:21:58 recv loop terminated: err=failed to read from connection: read tcp 192.168.1.101:56952->192.168.1.101:2181: i/o timeout
2021/06/21 15:21:58 recv loop terminated: err=failed to read from connection: read tcp 192.168.1.101:56976->192.168.1.101:2181: i/o timeout
==================
WARNING: DATA RACE
Write at 0x00c0001ee000 by goroutine 77:
  github.com/samuel/go-zookeeper/zk.(*Conn).recvLoop()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:910 +0xc4c
  github.com/samuel/go-zookeeper/zk.(*Conn).loop.func2()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:521 +0x2f1

Previous read at 0x00c0001ee000 by goroutine 8:
  github.com/samuel/go-zookeeper/zk.(*Conn).sendSetWatches()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:637 +0x472
  github.com/samuel/go-zookeeper/zk.(*Conn).loop()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:535 +0x5a4
  github.com/samuel/go-zookeeper/zk.Connect.func1()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:223 +0x3c

Goroutine 77 (running) created at:
  github.com/samuel/go-zookeeper/zk.(*Conn).loop()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:516 +0x576
  github.com/samuel/go-zookeeper/zk.Connect.func1()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:223 +0x3c

Goroutine 8 (running) created at:
  github.com/samuel/go-zookeeper/zk.Connect()
      /gopath/src/github.com/samuel/go-zookeeper/zk/conn.go:222 +0x8c6

     // ... more application call stack
     // call like this
     // zkConn, chanConnect, err := zk.Connect([]string{zkAddr}, time.Second*30, zk.WithLogInfo(false))
==================

centos 7 go 1.16.2 zookeeper 3.6.2

Originally posted by @huaz836 in https://github.com/samuel/go-zookeeper/issues/178#issuecomment-864974329