[CSHARP-791] Race condition with reusing connections Created: 07/Aug/13  Updated: 25/Aug/15  Resolved: 30/Aug/13

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

Type: Bug Priority: Critical - P2
Reporter: John Lyon-Smith Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OSX, Mono 3.2.1


Attachments: Zip Archive CSHARP-791.zip    
Issue Links:
Related
Backwards Compatibility: Fully Compatible

 Description   

When executing the code:

List<TDmo> items = cursor.ToList();

An error occurs deserializing a document. Code in the finally block of MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs, DeserializeMemberValue(BsonReader, BsonMemberMap) throws a nice exception.

Then, while unwinding the stack the finally block in MongoCSharp/MongoDB.Driver/Operations/QueryOperation.cs, Execute(IConnectionProvider) calls KillCursor() which calls AcquireConnection(). This appears to acquire the connection that is in the process of being closed from the exception above.

If you step through the code in the debugger (MonoDevelop or Xamarin Studio), you slow things down enough so that a FileFormatException is throw. In production, you typically just the InvalidOperationException.



 Comments   
Comment by Githook User [ 25/Aug/15 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-791: Ignore any exceptions thrown by KillCursor.
Branch: v1.8.x
https://github.com/mongodb/mongo-csharp-driver/commit/f53571164b4b73347cb346e164557ac1dd47b473

Comment by Githook User [ 25/Aug/15 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-791: Don't let an exception on one connection cause us to think that we are no longer connected to the server.
Branch: v1.8.x
https://github.com/mongodb/mongo-csharp-driver/commit/03fd9b5625aaa4298ca1846050f193a88c767510

Comment by auto [ 25/Sep/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-791: Ignore any exceptions thrown by KillCursor.
Branch: v1.8.3
https://github.com/mongodb/mongo-csharp-driver/commit/f53571164b4b73347cb346e164557ac1dd47b473

Comment by auto [ 25/Sep/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-791: Don't let an exception on one connection cause us to think that we are no longer connected to the server.
Branch: v1.8.3
https://github.com/mongodb/mongo-csharp-driver/commit/03fd9b5625aaa4298ca1846050f193a88c767510

Comment by auto [ 29/Aug/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-791: Ignore any exceptions thrown by KillCursor.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/c26ce6e33ab6552b5b94ac9627cc992e3af419b7

Comment by auto [ 29/Aug/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-791: Don't let an exception on one connection cause us to think that we are no longer connected to the server.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/bda77f528b72ee95348790af8113d6645d4e3d5b

Comment by John Lyon-Smith [ 25/Aug/13 ]

Sample that repros the bug under Mono 3.0.

Comment by John Lyon-Smith [ 25/Aug/13 ]

I'm attaching a sample program that reproduces the problem. Here is the stack trace I get from running in Xamarin Studio:

System.InvalidOperationException: Server instance localhost:27017 is no longer connected.
at MongoDB.Driver.MongoServerInstance.AcquireConnection () [0x0002e] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/Communication/MongoServerInstance.cs:389
at MongoDB.Driver.MongoServer.AcquireConnection (MongoDB.Driver.MongoServerInstance serverInstance) [0x00091] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/MongoServer.cs:985
at MongoDB.Driver.MongoCursor`1+MongoCursorConnectionProvider[CSHARP791.Test].AcquireConnection () [0x0003f] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/MongoCursor.cs:1004
at MongoDB.Driver.Operations.QueryOperation`1[CSHARP791.Test].KillCursor (IConnectionProvider connectionProvider, Int64 cursorId) [0x00002] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/Operations/QueryOperation.cs:186
at MongoDB.Driver.Operations.QueryOperation`1+<Execute>c_Iterator1[CSHARP791.Test].<>_Finally0 () [0x00036] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/Operations/QueryOperation.cs:120
at MongoDB.Driver.Operations.QueryOperation`1+<Execute>c__Iterator1[CSHARP791.Test].MoveNext () [0x00292] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/Operations/QueryOperation.cs:115
at System.Collections.Generic.List`1[CSHARP791.Test].AddEnumerable (IEnumerable`1 enumerable) [0x00013] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.2/mcs/class/corlib/System.Collections.Generic/List.cs:134
at System.Collections.Generic.List`1[CSHARP791.Test]..ctor (IEnumerable`1 collection) [0x0002f] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.2/mcs/class/corlib/System.Collections.Generic/List.cs:69
at System.Linq.Enumerable.ToList[Test] (IEnumerable`1 source) [0x00006] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.2/mcs/class/System.Core/System.Linq/Enumerable.cs:2951
at CSHARP791.MainClass.Main (System.String[] args) [0x00094] in /Users/john/Projects/Scratch/CSHARP-791/CSHARP-791/Program.cs:49

If you change numDocs to 1 you get the expected exception:

System.IO.FileFormatException: An error occurred while deserializing the Items property of class CSHARP791.Test: An error occurred while deserializing the B property of class CSHARP791.Item: Input string was not in the correct format ---> System.IO.FileFormatException: An error occurred while deserializing the B property of class CSHARP791.Item: Input string was not in the correct format ---> System.FormatException: Input string was not in the correct format
at System.Int32.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00012] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.2/mcs/class/corlib/System/Int32.cs:639
at System.Xml.XmlConvert.ToInt32 (System.String s) [0x00000] in /private/tmp/source/bockbuild-xamarin/profiles/mono-mac-xamarin-no-pcl/build-root/mono-3.2.2/mcs/class/System.XML/System.Xml/XmlConvert.cs:476
at MongoDB.Bson.Serialization.Serializers.Int32Serializer.Deserialize (MongoDB.Bson.IO.BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options) [0x00090] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/Int32Serializer.cs:79
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue (MongoDB.Bson.IO.BsonReader bsonReader, MongoDB.Bson.Serialization.BsonMemberMap memberMap) [0x000c8] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:594
— End of inner exception stack trace —
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue (MongoDB.Bson.IO.BsonReader bsonReader, MongoDB.Bson.Serialization.BsonMemberMap memberMap) [0x00135] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:601
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (MongoDB.Bson.IO.BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options) [0x001b0] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:173
at MongoDB.Bson.Serialization.Serializers.EnumerableSerializer`1[CSHARP791.Item].Deserialize (MongoDB.Bson.IO.BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options) [0x00093] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/EnumerableSerializer.cs:242
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue (MongoDB.Bson.IO.BsonReader bsonReader, MongoDB.Bson.Serialization.BsonMemberMap memberMap) [0x000c8] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:594
— End of inner exception stack trace —
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMemberValue (MongoDB.Bson.IO.BsonReader bsonReader, MongoDB.Bson.Serialization.BsonMemberMap memberMap) [0x00135] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:601
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (MongoDB.Bson.IO.BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options) [0x001b0] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:173
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize (MongoDB.Bson.IO.BsonReader bsonReader, System.Type nominalType, IBsonSerializationOptions options) [0x00066] in /Users/john/Projects/MongoCSharp/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs:75
at MongoDB.Driver.Internal.MongoReplyMessage`1[CSHARP791.Test].ReadFrom (MongoDB.Bson.IO.BsonBuffer buffer, IBsonSerializationOptions serializationOptions) [0x00199] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/Communication/Messages/MongoReplyMessage.cs:122
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[Test] (MongoDB.Bson.IO.BsonBinaryReaderSettings readerSettings, IBsonSerializer serializer, IBsonSerializationOptions serializationOptions) [0x0008b] in /Users/john/Projects/MongoCSharp/MongoDB.Driver/Communication/MongoConnection.cs:266

Comment by Craig Wilson [ 08/Aug/13 ]

Thanks for reporting. Could you provide the stack traces so we can attempt to reproduce? If you have a small program demonstrating the problem, that would help even more.

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