scylladb / gocql

Package gocql implements a fast and robust ScyllaDB client for the Go programming language.

Home Page:https://docs.scylladb.com/stable/using-scylla/drivers/cql-drivers/scylla-go-driver.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Potential wrong lock handling at session.go

dkropachev opened this issue · comments

Please answer these questions before submitting your issue. Thanks!

What version of Gocql are you using?

master

gocql/session.go

Lines 232 to 234 in 9dd9a7f

s.control.getConn().conn.mu.Lock()
s.tabletsRoutingV1 = s.control.getConn().conn.tabletsRoutingV1
s.control.getConn().conn.mu.Unlock()

s.control.getConn() can return different connection on each call, at least one path is possible when c.attemptReconnectToAnyOfHosts is executed in parallel.

Which means that this code can lock one connection and unlock another:

 s.control.getConn().conn.mu.Lock() # connection1 is locked
 # c.attemptReconnectToAnyOfHosts executed in parallel replacing `s.control.getConn().conn` with new value
 s.tabletsRoutingV1 = s.control.getConn().conn.tabletsRoutingV1 
 s.control.getConn().conn.mu.Unlock() # connection2 is unlocked

Suggested code:

 conn := s.control.getConn().conn
 conn.Lock()
 s.tabletsRoutingV1 = conn.tabletsRoutingV1 
 conn.Unlock()

Also probably it makes sense to make getter for tabletsRoutingV1 that would Lock/Unlock