go-zookeeper icon indicating copy to clipboard operation
go-zookeeper copied to clipboard

Conn.SetLogger() race condition

Open youngkin opened this issue 9 years ago • 2 comments

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?

youngkin avatar Nov 12 '15 16:11 youngkin

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.

ghost avatar Nov 12 '15 20:11 ghost

you can control this is a mutex lock as well.

nemith avatar May 23 '17 23:05 nemith