[CSHARP-217] Disposing a cursor that used a connection that failed leads to NullReferenceException Created: 30/Apr/11  Updated: 02/Apr/15  Resolved: 03/May/11

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

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


 Description   

The stack trace is as follows:
System.NullReferenceException: Object reference not set to an instance of an object.
at MongoDB.Driver.MongoServer.AcquireConnection(MongoDatabase database, MongoServerInstance serverInstance) in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Core\MongoServer.cs:line 827
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.AcquireConnection() in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 142
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.KillCursor() in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 222
at MongoDB.Driver.Internal.MongoCursorEnumerator`1.Dispose() in mongodb\mongodb-mongo-csharp-driver-1091cad\Driver\Internal\MongoCursorEnumerator.cs:line 69
at mongotest.Program.ReadTest() in tools\mongotest\mongotest\Program.cs:line 122

The MongoConnection.HandleException() terminates server instance, destroying it's connection pool, but when cursor is disposed, it tries to acquire connection to properly kill itself.
I guess that MongoCursorEnumerator.Dispose() or MongoCursorEnumerator.KillCursor() should either not acquire a connection from a dead server (TryAcquireConnection or something like that) or blindly ignore exceptions when connection is acquired (while this way is easier, I don't think that ignoring NREs is a good idea after all).

Also, adding some kind of a null check to MongoServerInstance.ConnectionPool and throwing ObjectDisposedException (or InvalidOperationException, since MongoServerInstance isn't actually an IDisposable, it's all up to you) would be nice, because NREs are hard to debug in optimized code because of method inlining even online, while postmortem dump-based debugging often becomes really nasty =)



 Comments   
Comment by Robert Stam [ 03/May/11 ]

Resolved by checking that the server is still connected before trying to acquire a connection for the purpose of killing the cursor.

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