efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Projection with nested projection hangs/does not complete in 2.0 Preview 2

Open bridgesquared opened this issue 9 years ago • 35 comments

When we updated our code base to 2.0 Preview-2 we had significant problems with a number of our queries hanging. We initially flagged this as AutoMapper/AutoMapper/#2184. But during discussions it has become apparent that this is a change at the EF Core 2 level.

The following style of query with an inner projection now fails to complete / hangs:

var result = await context.Documents.Select(d => new DocumentDTO
    {
        Reference = d.Reference,
        Items = d.Items.Select(i => new DocumentItemDTO
        {
            Reference = i.Reference
        }).ToList()
    }).FirstAsync();

It used to work in 2.0 Preview 1 (and 1.x.x) but now hangs in 2.0 Preview 2.

I appreciate that it is a problematic query, in the sense that the inner projection uses ToList() whilst the outer projection is FirstAsync() and I have some ideas for workarounds

But overall I feel that this should continue to work, it feels natural, it is not unreasonable to request nested data and a projection in one step (we are trying to reduce the data retrieved from the db server) and I can't see how you could rewrite the inner projection as async.

It seems not unreasonable to ask EF Core to resolve this type of query correctly and return the data requested asynchronously regardless of how inefficient / ugly it is.

I have created the following gists to repro this.

Discussions on the AutoMapper repo have suggested that this is related to #8208 and possibly #9029.

Meanwhile, we've gone back to 2.0 Preview 1 and are trying to work out how we are going to address this in 2.0 Final. So any thoughts/fixes etc. would be much appreciated. Many thanks.

bridgesquared avatar Jul 09 '17 09:07 bridgesquared

+1

I am also facing the same issue in -preview2 with many queries involving nested projections. FYI using npgsql.

BenjiZombie avatar Jul 10 '17 15:07 BenjiZombie

@bridgesquared Thanks for reporting this--we will investigate, but we will not be able to get a fix in for 2.0. Some workarounds to try:

  • Use First instead of FirstAsync. (Since the inner query is not async there will be some blocking anyway.)
  • Remove the ToList call and then enumerate Items explicitly. (This may change the way the DTO gets consumed, so look into your app code as to whether this is acceptable.)
  • Split it explicitly into two queries and then combine the results into the DTO afterwards.

ajcvickers avatar Jul 10 '17 19:07 ajcvickers

I think that this should be on high priority. The async operation is crucial sometimes in performance manner...

tomerpeled avatar Jul 11 '17 12:07 tomerpeled

In my personal opinion, this is quite a major issue and 2.0 shouldn't be released until async queries with nested projections can be safely written without deadlocking. It worked in preview1, it should not result in a deadlock in an RTM release...

stgeorge avatar Jul 16 '17 17:07 stgeorge

We should consider the specific scenario form https://github.com/aspnet/EntityFramework/issues/8208 when we work on this.

divega avatar Jul 27 '17 20:07 divega

Hi,

This is a major concern for us, all our framework is based on async/await, and queries are blocked. We actually consider moving to EF 6, unfortunately it is a show-stopper.

Do you have a time frame?

Thanks in advance

shlomiw avatar Jul 30 '17 13:07 shlomiw

@shlomiw do not move to EF 6. I have the same issue using EF 6.1.3

luisms89 avatar Aug 17 '17 04:08 luisms89

@anpete Do we need a new issue for patch approval?

AndriySvyryd avatar Aug 18 '17 00:08 AndriySvyryd

Risk: Low: Adds a small amount of new code to the query compiler that detects the offending query patterns and performs a transformation from sync to async operators. Justification: Fixes a deadlock that can occur in reasonably common customer scenarios. After the fix, such queries become fully async, which is the ideal behavior.

anpete avatar Aug 18 '17 21:08 anpete

Hi, tried the suggested patch via:

services.AddDbContext<ApplicationDbContext>(options =>
{
	options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
	// temporary fix for ef #9128
	options.ReplaceService<ITaskBlockingExpressionVisitor, TaskBlockingExpressionVisitor2>();
	options.ReplaceService<IProjectionExpressionVisitorFactory, ProjectionExpressionVisitorFactory2>();
});

Called this query:

var report = await Context.Reports
	.Include(x => x.ReportFiles)
	.FirstOrDefaultAsync(q => q.Id == id, CancellationToken);

Throws the following error:

Unable to cast object of type 
'System.Func`1[System.Linq.IOrderedQueryable`1[TestDb.Models.ReportFile]]' to type 
'System.Func`1[System.Collections.Generic.IAsyncEnumerable`1[System.Object]]'

dbbaleva avatar Aug 30 '17 10:08 dbbaleva

I'm facing a similar issue. Did ef core 2.0 get a RTM greenlight despite this regression? Hard to believe.

await _ctx.AllSchedules
               .Where(s => s.Id == cmd.ScheduleId)
               .Select(s => new
                                            {
                                                Schedule = s,
                                                Shop = s.Shop,
                                                LastRun = s.Runs.OrderByDescending(r => r.StartTimestamp).FirstOrDefault(r => r.IsSuccess)
                                          })
               .SingleAsync();

clement911 avatar Sep 06 '17 12:09 clement911

@clement911 I posted on 7/16 that they shouldn't go RTM with this obvious and very common issue that causes hard deadlocks, the product is useless with this massive gotcha in there, especially if you started building something that worked fine in earlier previews. It's not an incorrect pattern. Doesn't seem like whoever is making decisions cares much about quality, only deadlines. Makes me question about using or recommending .NET Core at all if this is the attitude they have.

stgeorge avatar Sep 06 '17 13:09 stgeorge

Out of interest, I was having this issue and saw some further strangeness: when I add an 'Include' for the property of the inner projection, the deadlock goes away. I get a warning logged saying the Include is not necessary because of the projection, and will be ignored, but it certainly changes the behaviour.

It's not a good workaround for this issue though, because it looks like EF core then does separate subqueries for each item.

var blogs = await _context.Blogs
    .Include(blog => blog.Posts)  // adding this prevents deadlock, but causes multiple subqueries
    .Select(blog => new
    {
        Id = blog.BlogId,
        Url = blog.Url,
        Posts = blog.Posts.Select(p => p.Title).ToArray()
    })
    .ToListAsync();

spdjudd avatar Sep 08 '17 15:09 spdjudd

I'm getting the same issue with the EF Core 2.0 (final). Wondering whether any positive workaround is out there yet? Our solution was working well with EF Core 1.1.2. Its really annoying when the final versions have fundamental bugs like this. Really don't get the point of releasing a product having obvious known issues that almost make it useless for most of the users. This results only waste of time!

Wijithapaw avatar Sep 11 '17 11:09 Wijithapaw

@Wijithapaw I believe the only workaround right now is to not use async when having nested projections.

stgeorge avatar Sep 11 '17 12:09 stgeorge

we face the same problem. MS give us please a delivery date.

ghost avatar Sep 11 '17 12:09 ghost

@stgeorge, For that I have to change a good chuck of back end methods. And would be even annoying when reverting them back once fixing the issue. So I decided not to do the .Net 2.0 upgrade until this issue is fixed.

Even after 2 months of reporting this show-stopper there is no a proper fix. That means there is no a simple fix!. I won't be surprised if we have to do some significant changes in our code (AGAIN) to comply with architectural changes in next .Net Core release.

Wijithapaw avatar Sep 11 '17 12:09 Wijithapaw

This is tentatively scheduled to release near the beginning of October.

ajcvickers avatar Sep 11 '17 16:09 ajcvickers

This patch bug is approved for the 2.0.x patch. Please send a PR to the feature/2.0.1 branch and get it reviewed and merged. When we have the rel/2.0.1 branches ready please port the commit to that branch.

Eilon avatar Sep 15 '17 16:09 Eilon

I can confirm that the workaround @spdjudd mentioned that uses .Include() does generate a warning that says it is ignored but does in fact, fix the hang issue.

john-mills-nz avatar Oct 04 '17 01:10 john-mills-nz