[CSHARP-1244] Unhandled exception in IQueryable.ToList() Created: 20/Apr/15  Updated: 21/Mar/17  Resolved: 21/Mar/17

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

Type: Bug Priority: Major - P3
Reporter: Eva Assignee: Robert Stam
Resolution: Cannot Reproduce Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows Server 2012 Standard, .NET 4.0



 Description   

In our application, we are using the following code to retrieve some information from MongoDB (the relevant bits):

    public MongoCollection<T> GetCollection<T>(string collection)
    {
      return database.GetCollection<T>(collection);
    }
 
    private MongoCollection<SystemNotification> SystemNotification(string collectionName)
    {
      return GetCollection<SystemNotification>(collectionName);
    }
 
    public IList<SystemNotification> GetListOfValidSystemNotification(string scopeId, DateTime timestampInRange)
    {
      return SystemNotification(scopeId).AsQueryable().Where(x =>
        (x.ValidFrom == null || x.ValidFrom <= timestampInRange) &&
        (x.ValidTill == null || x.ValidTill >= timestampInRange)).ToList();
    }

The above code crashes the whole application unpredictably when processing the result of the query in GetListOfValidSystemNotification (approx 1/week) with the following exception:

Application: [our application].exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Reflection.TargetInvocationException
Stack:
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
   at System.Delegate.DynamicInvokeImpl(System.Object[])
   at MongoDB.Driver.Linq.PartialEvaluator+SubtreeEvaluator.EvaluateSubtree(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.ExpressionVisitor.VisitBinary(System.Linq.Expressions.BinaryExpression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.ExpressionVisitor.VisitBinary(System.Linq.Expressions.BinaryExpression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.ExpressionVisitor.VisitBinary(System.Linq.Expressions.BinaryExpression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.ExpressionVisitor.VisitLambda(System.Linq.Expressions.LambdaExpression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.ExpressionVisitor.VisitUnary(System.Linq.Expressions.UnaryExpression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Collections.ObjectModel.ReadOnlyCollection`1<System.Linq.Expressions.Expression>)
   at MongoDB.Driver.Linq.ExpressionVisitor.VisitMethodCall(System.Linq.Expressions.MethodCallExpression)
   at MongoDB.Driver.Linq.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.MongoQueryTranslator.Translate(MongoDB.Driver.Linq.MongoQueryProvider, System.Linq.Expressions.Expression)
   at MongoDB.Driver.Linq.MongoQueryable`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].GetEnumerator()
   at System.Collections.Generic.List`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]..ctor(System.Collections.Generic.IEnumerable`1<System.__Canon>)
   at System.Linq.Enumerable.ToList[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1<System.__Canon>)
   at ...MongoDb.MetaStorageRepositoryMongo.GetListOfValidSystemNotification(System.String, System.DateTime)
....

Please note that this output is from Windows Event VIewer. We have exception handling in place, but because this is an application crash, it catches nothing. The issue is not, as far as we have managed to determine, reproducible at will.



 Comments   
Comment by Robert Stam [ 07/Mar/17 ]

I know this was reported some time ago... I tried to reproduce this today using this code but no exception was thrown:

using System;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
 
namespace TestCSharp1244
{
    public class SystemNotification
    {
        public DateTime? ValidFrom { get; set; }
        public DateTime? VaildTill { get; set; }
    }
 
    public static class Program
    {
        public static void Main(string[] args)
        {
            var client = new MongoClient("mongodb://localhost");
            var server = client.GetServer();
            var database = server.GetDatabase("test");
            var collection = database.GetCollection<SystemNotification>("test");
 
            var timestampInRange = new DateTime(2017, 3, 7, 12, 0, 0, DateTimeKind.Utc);
            var queryable = collection.AsQueryable().Where(x =>
                (x.ValidFrom == null || x.ValidFrom <= timestampInRange) &&
                (x.VaildTill == null || x.VaildTill >= timestampInRange));
            var mongoQueryable = (MongoQueryable<SystemNotification>)queryable;
            var query = mongoQueryable.GetMongoQuery().ToJson();
        }
    }
}

Also, 1.10 is a very old version of the driver that is no longer actively being supported. The 2.x version of the driver has an entirely new LINQ implementation that converts LINQ queries to aggregation framework pipelines instead of to find queries. This allows more of LINQ to be supported.

I'm changing the status of this ticket to "Debugging with Submitter" but will soon change it to "Works as Designed".

Comment by Joel Sanderson [ 30/Jun/15 ]

We are experiencing the same issue. We currently are using version 1.10.0.62 (file version of MongoDB.Driver.dll) of the C# driver. However, ours only started doing this in the past few days. The same version of the driver had been working very well since Feb 2015.

However, we've been moving toward using IMongoQuery instead of lambda expressions, so to workaround this, I'm going to try converting the queries to that.

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