Skip to content

Commit

Permalink
Merge pull request #230 from globalsign/bugfix/MGO-135-data-race-socket
Browse files Browse the repository at this point in the history
Fix socket race condition
  • Loading branch information
eminano authored Aug 2, 2018
2 parents 64f7b05 + 3ed1fdc commit 5af851b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 4 additions & 4 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5183,13 +5183,13 @@ func (s *Session) acquireSocket(slaveOk bool) (*mongoSocket, error) {
s.m.RLock()
// If there is a slave socket reserved and its use is acceptable, take it as long
// as there isn't a master socket which would be preferred by the read preference mode.
if s.slaveSocket != nil && s.slaveSocket.dead == nil && s.slaveOk && slaveOk && (s.masterSocket == nil || s.consistency != PrimaryPreferred && s.consistency != Monotonic) {
if s.slaveSocket != nil && s.slaveSocket.Dead() == nil && s.slaveOk && slaveOk && (s.masterSocket == nil || s.consistency != PrimaryPreferred && s.consistency != Monotonic) {
socket := s.slaveSocket
socket.Acquire()
s.m.RUnlock()
return socket, nil
}
if s.masterSocket != nil && s.masterSocket.dead == nil {
if s.masterSocket != nil && s.masterSocket.Dead() == nil {
socket := s.masterSocket
socket.Acquire()
s.m.RUnlock()
Expand All @@ -5203,15 +5203,15 @@ func (s *Session) acquireSocket(slaveOk bool) (*mongoSocket, error) {
defer s.m.Unlock()

if s.slaveSocket != nil && s.slaveOk && slaveOk && (s.masterSocket == nil || s.consistency != PrimaryPreferred && s.consistency != Monotonic) {
if s.slaveSocket.dead == nil {
if s.slaveSocket.Dead() == nil {
s.slaveSocket.Acquire()
return s.slaveSocket, nil
} else {
s.unsetSocket()
}
}
if s.masterSocket != nil {
if s.masterSocket.dead == nil {
if s.masterSocket.Dead() == nil {
s.masterSocket.Acquire()
return s.masterSocket, nil
} else {
Expand Down
14 changes: 12 additions & 2 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func (socket *mongoSocket) Server() *mongoServer {
return server
}

// ServerInfo returns details for the server at the time the socket
// was initially acquired.
// ServerInfo returns details for the server at the time the socket was
// initially acquired.
func (socket *mongoSocket) ServerInfo() *mongoServerInfo {
if socket == nil {
return &mongoServerInfo{}
Expand All @@ -225,6 +225,16 @@ func (socket *mongoSocket) ServerInfo() *mongoServerInfo {
return serverInfo
}

// Dead returns the internal status of the socket
//
// It will return nil or an error if the socket is dead
func (socket *mongoSocket) Dead() error {
socket.Lock()
defer socket.Unlock()

return socket.dead
}

// InitialAcquire obtains the first reference to the socket, either
// right after the connection is made or once a recycled socket is
// being put back in use.
Expand Down

0 comments on commit 5af851b

Please sign in to comment.