[CSHARP-1966] StackOverflow when expression contains IQueryable Created: 21/Apr/17  Updated: 28/Oct/23  Resolved: 07/Feb/22

Status: Closed
Project: C# Driver
Component/s: Linq, LINQ3
Affects Version/s: 2.2.3, 2.4.3
Fix Version/s: 2.14.0

Type: Bug Priority: Major - P3
Reporter: Gushchin Anton Assignee: James Kovacs
Resolution: Fixed Votes: 0
Labels: triaged
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10, MSSQL 16, .NET 4.6.1, EntityFramework 6.1.3


Epic Link: CSHARP-3615

 Description   

1. Create empty console application
2. Install EntityFramework package (6.1.3)
3. Install MongoDB.Driver package (2.4.3)

namespace App
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Entity;
    using MongoDB.Driver;
 
    class Program
    {
        static void Main(string[] args)
        {
            var context = new SampleContext();
            
            var ids = context.Customers.Select(e => e.CustomerId);
            var arr = ids.ToArray();
            
            var mongoQuery = new MongoClient("mongodb://localhost:27017")
                .GetDatabase("MyShop")
                .GetCollection<MongoCustomer>("MongoCustomer")
                .AsQueryable();
 
            // OK
            mongoQuery.Where(e => arr.Contains(e.CustomerId)).ToArray();
 
            // StackOverflow
            mongoQuery.Where(e => ids.Contains(e.CustomerId)).ToArray();
        }
    }
 
    public class MongoCustomer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }
 
    public class Customer
    {
        public int CustomerId { get; set; }
        public string Name { get; set; }
    }
 
    public class SampleContext : DbContext
    {
        public SampleContext() : base("MyShop")
        {
        }
 
        public DbSet<Customer> Customers { get; set; }
    }
}



 Comments   
Comment by James Kovacs [ 07/Feb/22 ]

This issue has been fixed in the new LINQ provider (known as LINQ3), which is included in the 2.14 release.

Configure your MongoClientSettings to use LinqProvider.V3 if you want to use this functionality.

To configure a client to use the LINQ3 provider use code like the following

var connectionString = "mongodb://localhost";
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
clientSettings.LinqProvider = LinqProvider.V3;
var client = new MongoClient(clientSettings);

Comment by Robert Stam [ 21/Apr/17 ]

Sorry... just realized that the exception was a StackOverflow.

That would be a really big stack trace...

Comment by Robert Stam [ 21/Apr/17 ]

Can you provide a full stack trace from the exception?

Looks like you've found a viable workaround: execute the Entity Framework query first before passing the resulting list to the MongoDB query.

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