Skip to content

Commit fc9aa45

Browse files
Propose a fix for globalsign#120
As discussed in the issue globalsign#120, isMaster() can cause a deadlock with the topology scanner if the connection it makes dies before running the command; mgo automagically attempts to make another socket in acquireSocket, but this can't work without topology. This commit forces isMaster() to actually run on the intended socket.
1 parent 08c5182 commit fc9aa45

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (cluster *mongoCluster) isMaster(socket *mongoSocket, result *isMasterResul
181181
})
182182
})
183183

184-
err := session.Run(cmd, result)
184+
err := session.RunOnSocket(socket, cmd, result)
185185
session.Close()
186186
return err
187187
}

session.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,15 @@ func (db *Database) Run(cmd interface{}, result interface{}) error {
848848
return db.run(socket, cmd, result)
849849
}
850850

851+
// RunOnSocket does the same as Run, but guarantees that your command will be run
852+
// on the provided socket instance; if it's unhealthy, you will receive the error
853+
// from it.
854+
func (db *Database) RunOnSocket(socket *mongoSocket, cmd interface{}, result interface{}) error {
855+
socket.Acquire()
856+
defer socket.Release()
857+
return db.run(socket, cmd, result)
858+
}
859+
851860
// Credential holds details to authenticate with a MongoDB server.
852861
type Credential struct {
853862
// Username and Password hold the basic details for authentication.
@@ -2312,6 +2321,13 @@ func (s *Session) Run(cmd interface{}, result interface{}) error {
23122321
return s.DB("admin").Run(cmd, result)
23132322
}
23142323

2324+
// RunOnSocket does the same as Run, but guarantees that your command will be run
2325+
// on the provided socket instance; if it's unhealthy, you will receive the error
2326+
// from it.
2327+
func (s *Session) RunOnSocket(socket *mongoSocket, cmd interface{}, result interface{}) error {
2328+
return s.DB("admin").RunOnSocket(socket, cmd, result)
2329+
}
2330+
23152331
// SelectServers restricts communication to servers configured with the
23162332
// given tags. For example, the following statement restricts servers
23172333
// used for reading operations to those with both tag "disk" set to

0 commit comments

Comments
 (0)