rpcxio/rpcx-gateway

deadlock if rpcx-service is not registed in consul

worldyuan opened this issue · 2 comments

followed in gateway.go

g.mu.Lock()
if g.xclients[servicePath] == nil {
	g.xclients[servicePath] = client.NewXClient(servicePath, g.FailMode, g.SelectMode, g.serviceDiscovery.Clone(servicePath), g.Option)
}
xc = g.xclients[servicePath]
g.mu.Unlock()

cannot run g.mu.Unlock() if rpcx-service is not registed in consul, because call panic in consul_disconvery.go
you could add defer before g.mu.Unlock() .

I fixed the bug by followed code:

g.mu.Lock()
if g.xclients[servicePath] == nil {
	defer func() {
		//if err := recover(); err != nil {
			if !is_unlock {
				g.mu.Unlock()
				//panic(err)
			}
		//}
	}()
	g.xclients[servicePath] = client.NewXClient(servicePath, g.FailMode, g.SelectMode, g.serviceDiscovery.Clone(servicePath), g.Option)

}
xc = g.xclients[servicePath]
g.mu.Unlock()
is_unlock = true

fixed. Thanks