[CSHARP-411] NRE in timer thread Created: 14/Mar/12  Updated: 02/Apr/15  Resolved: 20/Mar/12

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

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

Backwards Compatibility: Fully Compatible

 Description   

After running for a few minutes doing some DB operations, I attached the debugger and found out there are NREs happening in the background at the following stack trace:
MongoDB.Driver.DLL!MongoDB.Driver.Internal.MongoConnection.Open() Line 362 + 0x5 bytes C#
MongoDB.Driver.DLL!MongoDB.Driver.Internal.MongoConnection.GetNetworkStream() Line 522 C#
MongoDB.Driver.DLL!MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoDB.Driver.Internal.MongoRequestMessage message, MongoDB.Driver.SafeMode safeMode) Line 473 + 0xc bytes C#
MongoDB.Driver.DLL!MongoDB.Driver.Internal.MongoConnection.RunCommand(string collectionName, MongoDB.Driver.QueryFlags queryFlags, MongoDB.Driver.CommandDocument command, bool throwOnError) Line 389 + 0x3f bytes C#
MongoDB.Driver.DLL!MongoDB.Driver.MongoServerInstance.Ping() Line 217 + 0x2c bytes C#
MongoDB.Driver.DLL!MongoDB.Driver.MongoServerInstance.VerifyState() Line 236 + 0xc bytes C#
MongoDB.Driver.DLL!MongoDB.Driver.Internal.MongoConnectionPool.TimerCallback(object state) Line 344 C#
mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x178 bytes

It appears that _serverInstance.EndPoint is null for some reason. I'm using git version 77ac96b73b3fe9f4517cc79d3c72c2d87e1bbc9a.



 Comments   
Comment by Robert Stam [ 20/Mar/12 ]

The DNS lookups are now handled slightly differently so that this NullReferenceException no longer occurs.

Comment by Robert Stam [ 15/Mar/12 ]

Thanks for the extra information. Even though the exception can be explained by a DNS failure to resolve, this is a situation that could happen to anybody and it would be better if it was handled without throwing exceptions, so I will fix that.

Knowing the root cause will make it easy to reproduce.

Comment by Aristarkh Zagorodnikov [ 15/Mar/12 ]

Good thing you mentioned DNS, I checked and found that one of the servers mentioned in the connection string (it's a replica set) had its DNS name spelled wrong.
The server is in the "Connected" state, but the server instance is in the "Disconnected" state.

Comment by Robert Stam [ 15/Mar/12 ]

Getting a NullReferenceException on that line makes more sense. I'll investigate further.

The endPoint is the result of a DNS lookup, which is done once when Connect is called. The timer code doesn't do the Ping if the server state is Disconnected or Disconnecting, but it looks like there is a race condition where the server might be in some other state but has not yet resolved the endPoint address.

In a sense this is harmless because the exception is ignored. But annoying nonetheless.

Comment by Aristarkh Zagorodnikov [ 15/Mar/12 ]

My line 362 is:
var tcpClient = new TcpClient(endPoint.AddressFamily);

Comment by Robert Stam [ 15/Mar/12 ]

What's at your line number 362 in MongoConnection?

I have:

tcpClient.Connect(endPoint);

Which couldn't throw a NullReferenceException, and there's another use of the endPoint variable 4 lines earlier which would have thrown a NullReferenceException before reaching this line.

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