[CSHARP-294] Deadlock when connecting to a replica set Created: 01/Aug/11  Updated: 02/Apr/15  Resolved: 05/Aug/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.2
Fix Version/s: 1.2

Type: Bug Priority: Major - P3
Reporter: Aristarkh Zagorodnikov Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-268 NullReferenceException on lost primary Closed

 Description   

Initial connection to a replica set leads to deadlock. The source of the deadlock is the change of the locking protocol in response to the CSHARP-268.
While connecting to a replica set of 3 machines (primary, secondary, arbiter) first time, a deadlock occurs with the following four threads of interest:
1. actual connection thread:
MongoDB.Driver.dll!MongoDB.Driver.Internal.BlockingQueue<System.__Canon>.Dequeue(System.TimeSpan timeout) Line 53 C#
MongoDB.Driver.dll!MongoDB.Driver.Internal.ReplicaSetConnector.Connect(System.TimeSpan timeout) Line 61 + 0xc bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoServer.Connect(System.TimeSpan timeout) Line 457 C#
MongoDB.Driver.dll!MongoDB.Driver.MongoServer.GetServerInstance(bool slaveOk) Line 976 + 0xd bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoServer.AcquireConnection(MongoDB.Driver.MongoDatabase database, bool slaveOk) Line 920 C#
MongoDB.Driver.dll!MongoDB.Driver.MongoCursorEnumerator<System.__Canon>.AcquireConnection() Line 184 + 0x42 bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoCursorEnumerator<MongoDB.Bson.BsonDocument>.GetFirst() Line 194 + 0xc bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoCursorEnumerator<System.__Canon>.MoveNext() Line 126 + 0x8 bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoDatabase.GetCollectionNames() Line 649 + 0xdc bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoDatabase.CollectionExists(string collectionName) Line 290 + 0xa bytes C#

2,3,4: instance connection threads:
[In a sleep, wait, or join]
MongoDB.Driver.dll!MongoDB.Driver.MongoServer.InstanceStateChanged(object sender, object args) Line 1037 C#
MongoDB.Driver.dll!MongoDB.Driver.MongoServerInstance.State.set(MongoDB.Driver.MongoServerState value) Line 174 + 0xd bytes C#
MongoDB.Driver.dll!MongoDB.Driver.MongoServerInstance.Connect(bool slaveOk) Line 216 + 0x11 bytes C#
MongoDB.Driver.dll!MongoDB.Driver.Internal.ReplicaSetConnector.ConnectWorkItem(object argsObject) Line 100 + 0xd bytes C#
mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x178 bytes
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xa1 bytes
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x69 bytes
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x193 bytes
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() + 0x35 bytes

Both kinds of threads have the lock on serverLock (MongoServer.Connect for 1st kind and MongoServer.InstanceStateChanged for 2nd kind) and the first thread appears to wait second ones to connect, which they can't do, because they're blocked on the lock.



 Comments   
Comment by Robert Stam [ 05/Aug/11 ]

The changes to fix CSHARP-268 also fixed this issue.

Comment by Robert Stam [ 05/Aug/11 ]

The changes made to fix CSHARP-268 also fixed this issue.

Comment by Aristarkh Zagorodnikov [ 02/Aug/11 ]

Since the deadlock fixes and followup are in CSHARP-268, maybe this case should be closed as duplicate or merged with CSHARP-268?

Comment by Aristarkh Zagorodnikov [ 01/Aug/11 ]

Forgot to add that while deadlock isn't indefinite, it leads to "Unable to connect to server." error on first connection after it times out.

Generated at Wed Feb 07 21:36:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.