redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.65k stars 2.32k forks source link

fix ConnPool race in newConn #2885

Closed OlegStotsky closed 5 months ago

OlegStotsky commented 5 months ago

Currently there is a race in newConn. This means that any code that uses this client will fail tests with -race flag enabled. This PR fixes this.

OlegStotsky commented 5 months ago

@ofekshenawa please take a look

Aden-Q commented 5 months ago

@OlegStotsky You should mention my issue here (if you saw it). And I don't think it's the correct way to fix the issue. Even though it can protect the variable to be mutated while being read. There still can be interleaving execution flows, causing some errors on the semantic level (for example, read the value in one goroutine, afterwards it is updated by another goroutine. In which case there will not be data race, but there is a semantic error). I think we need to we protect the full scope of the critical section, instead of doing: lock-unlock, then lock-unlock again.

OlegStotsky commented 5 months ago

@Aden-Q I've added another check after we acquire the mutex again. Should be correct now.

OlegStotsky commented 5 months ago

@vmihailenco could you take a look please