go-zookeeper
go-zookeeper copied to clipboard
Conn.SetLogger() race condition
I'd like to use Conn.SetLogger(Logger)
to change the behavior of the default logger. go test -race
reports a race with the call to this function and calls to c.logger.Printf(...)
in conn.go (e.g., in the connect(...)
function. I'm using SetLogger(...)
as follows:
zkConn, zkConnEventChl, err := zk.Connect(zkServerList, timeoutMs)
nullLogger := nullZKLogger{}
zkConn.SetLogger(zk.Logger(nullLogger))
Am I using this capability incorrectly or is it a bug?
I think the race is due to the fact that zk.Connect()
spawns a goroutine that accesses zkConn.logger
and calling zkConn.SetLogger()
also touches that property. I don't know if this can really be a problem in practice, but you could solve it by passing an option to zk.Connect()
. Namely, defining
func loggerOption(c *zk.Conn) {
logger := nullZKLogger{}
c.SetLogger(logger)
}
and then calling
zk.Connect(zkServerList, timeoutMs, loggerOption)
That way, the logger is set before the "loop" goroutine starts.
you can control this is a mutex lock as well.