Skip to content

Proposed fix for deadlock in globalsign/mgo#120 #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (cluster *mongoCluster) isMaster(socket *mongoSocket, result *isMasterResul
})
})

err := session.Run(cmd, result)
err := session.RunOnSocket(socket, cmd, result)
session.Close()
return err
}
Expand Down
35 changes: 35 additions & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,41 @@ func (s *S) TestConnectCloseConcurrency(c *C) {
wg.Wait()
}

func (s *S) TestNoDeadlockOnClose(c *C) {
if *fast {
// Unfortunately I seem to need quite a high dial timeout to get this to work
// on my machine.
c.Skip("-fast")
}

var shouldStop int32
atomic.StoreInt32(&shouldStop, 0)

listener, err := net.Listen("tcp4", "127.0.0.1:")
c.Check(err, Equals, nil)

go func() {
for atomic.LoadInt32(&shouldStop) == 0 {
sock, err := listener.Accept()
if err != nil {
// Probs just closed
continue
}
sock.Close()
}
}()
defer func() {
atomic.StoreInt32(&shouldStop, 1)
listener.Close()
}()

session, err := mgo.DialWithTimeout(listener.Addr().String(), 10*time.Second)
// If execution reaches here, the deadlock did not happen and all is OK
if session != nil {
session.Close()
}
}

func (s *S) TestSelectServers(c *C) {
if !s.versionAtLeast(2, 2) {
c.Skip("read preferences introduced in 2.2")
Expand Down
16 changes: 16 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,15 @@ func (db *Database) Run(cmd interface{}, result interface{}) error {
return db.run(socket, cmd, result)
}

// RunOnSocket does the same as Run, but guarantees that your command will be run
// on the provided socket instance; if it's unhealthy, you will receive the error
// from it.
func (db *Database) RunOnSocket(socket *mongoSocket, cmd interface{}, result interface{}) error {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that accepting a package level parameter in a public method is a good idea.
There is no way this can be used outside of the mgo package.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KJTsanaktsidis maybe make a private function instead of a public method?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can definitely do this - don’t think I’ll get to it for a couple of days though, sorry :/

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KJTsanaktsidis you could merge https://github.com/zendesk/mgo/pull/1 if you agree with the change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @dvic - I merged your change into this PR. Sorry this took so long for me to get to :/

socket.Acquire()
defer socket.Release()
return db.run(socket, cmd, result)
}

// Credential holds details to authenticate with a MongoDB server.
type Credential struct {
// Username and Password hold the basic details for authentication.
Expand Down Expand Up @@ -2312,6 +2321,13 @@ func (s *Session) Run(cmd interface{}, result interface{}) error {
return s.DB("admin").Run(cmd, result)
}

// RunOnSocket does the same as Run, but guarantees that your command will be run
// on the provided socket instance; if it's unhealthy, you will receive the error
// from it.
func (s *Session) RunOnSocket(socket *mongoSocket, cmd interface{}, result interface{}) error {
return s.DB("admin").RunOnSocket(socket, cmd, result)
}

// SelectServers restricts communication to servers configured with the
// given tags. For example, the following statement restricts servers
// used for reading operations to those with both tag "disk" set to
Expand Down