简体   繁体   中英

C# mongo db driver randomly cannot connect to db

I have console app that calls mongo db by C# mongo driver in separate thread. Connection pool limit is 10, connection string is: mongodb+srv://login:pass@address/test?retryWrites=true&w=majority&connect=replicaSet

in half of times when I launch my console app it falls after 30 seconds of timeout. When I restart app it will work fine with 50% probability. If first operation falls - any other will fall too. If first is OK, any other will work fine.

Exceptions:

MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [], DnsMonitorException : "DnsClient.DnsResponseException: Unhandled exception ---> System.IndexOutOfRangeException: Cannot read byte 277, out of range. at DnsClient.DnsDatagramReader.ReadByte() at DnsClient.DnsDatagramReader.ReadLabels() at DnsClient.DnsDatagramReader.ReadQuestionQueryString() at DnsClient.DnsRecordFactory.ReadRecordInfo() at DnsClient.DnsMessageHandler.GetResponseMessage(ArraySegment 1 responseData) at DnsClient.DnsUdpMessageHandler.Query(IPEndPoint server, DnsRequestMessage request, TimeSpan timeout) at DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) --- End of inner exception stack trace --- at DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) at DnsClient.LookupClient.QueryInternal(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) at DnsClient.LookupClient.QueryInternal(IReadOnlyCollection 1 servers, DnsQuestion question, Boolean useCache) at DnsClient.LookupClient.Query(String query, QueryType queryType, QueryClass queryClass) at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveSrvRecords(String service, CancellationToken cancellationToken) at MongoDB.Driver.Core.Clusters.DnsMonitor.Monitor()" }.)'

This exception was originally thrown at this call stack: MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(MongoDB.Driver.Core.Clusters.ServerSelectors.IServerSelector, MongoDB.Driver.Core.Clusters.ClusterDescription) MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(System.Threading.Tasks.Task) MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(MongoDB.Driver.Core.Clusters.ServerSelectors.IServerSelector, MongoDB.Driver.Core.Clusters.ClusterDescription, System.Threading.Tasks.Task, System.TimeSpan, System.Threading.CancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) MongoDB.Driver.Core.Clusters.Cluster.SelectServerAsync(MongoDB.Driver.Core.Clusters.ServerSelectors.IServerSelector, System.Threading.CancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.Compiler Services.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) ... [Call Stack Truncated]

Inner:

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [], DnsMonitorException : "DnsClient.DnsResponseException: Unhandled exception ---> System.IndexOutOfRangeException: Cannot read byte 277, out of range. at DnsClient.DnsDatagramReader.ReadByte() at DnsClient.DnsDatagramReader.ReadLabels() at DnsClient.DnsDatagramReader.ReadQuestionQueryString() at DnsClient.DnsRecordFactory.ReadRecordInfo() at DnsClient.DnsMessageHandler.GetResponseMessage(ArraySegment 1 responseData) at DnsClient.DnsUdpMessageHandler.Query(IPEndPoint server, DnsRequestMessage request, TimeSpan timeout) at DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) --- End of inner exception stack trace --- at DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) at DnsClient.LookupClient.QueryInternal(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) at DnsClient.LookupClient.QueryInternal(IReadOnlyCollection 1 servers, DnsQuestion question, Boolean useCache) at DnsClient.LookupClient.Query(String query, QueryType queryType, QueryClass queryClass) at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveSrvRecords(String service, CancellationToken cancellationToken) at MongoDB.Driver.Core.Clusters.DnsMonitor.Monitor()" }.

I asked our NodeJS developer and he answered that he have no such problem. Does anybody faced with this issue?

I was also getting the exact error for last one day. I am not sure the exact problem but it is definitely related to what network I use. I tested by connecting my dev machine to my mobile's hot-spot and it worked. But when I again switched to broadband connection and it fails.

Hope it helps someone.

I have also raised a bug on mongodb support. Here is the link .

EDIT:

Finally got it fixed by upgrading the " DnsClient " nuget package to version 1.3.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM