Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/netcore] An System.AccessViolationException is raised when attempting to use $apply=aggregate #1221

Closed
techniq opened this issue Jan 23, 2018 · 12 comments

Comments

@techniq
Copy link

techniq commented Jan 23, 2018

An System.AccessViolationException is raised when attempting to use $apply=aggregate

Assemblies affected

  • 7.0.0-Nightly201801081323

Reproduce steps

  • Install Remotion.Linq version 2.2.0-alpha-005, which was recently released and implemented System.Linq.Queryable.AsQueryable, which in turn was needed to support OData's $apply=aggregate
  • Attempt to execute a query with $apply=groupby or $apply=aggregate, for example http://localhost:5000/odata/Employees?$apply=groupby((SupervisorId),aggregate(Id with countdistinct as Total))

Expected result

  • Result is returned

Actual result

  • dotnet process crashes. When debugging within Visual Studio on Windows, it appears the following exception is being raised: An unhandled exception of type 'System.AccessViolationException' occurred in Microsoft.AspNetCore.OData.dll Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

image

Additional detail

More details can be found within dotnet/efcore#6132 (comment) and related to feature support of #1154

@techniq techniq changed the title [feature/netcore] An unhandled exception of type 'System.AccessViolationException' occurred in Microsoft.AspNetCore.OData.dll Attempted to read or write protected memory. This is often an indication that other memory is corrupt. [feature/netcore] An System.AccessViolationException is raised when attempting to use $apply=aggregate Jan 23, 2018
@robward-ms
Copy link
Contributor

@techniq - Do you have a sample project or controller snippet you can share, specifically the integration with Remotion.Linq?

@robward-ms
Copy link
Contributor

@techniq - I did see a similar issue when using only EFCore and the Aggregation E2E tests. I'll use this bug to track a fix for that. #1154 will track the larger EFCore compatibility issues.

@techniq
Copy link
Author

techniq commented Feb 8, 2018

@robward-ms thanks. Do you still need a sample project? It sounds like the E2E tests should show the same. If so, how should I provide it (where to upload, etc)?

Thanks for your help.

@robward-ms
Copy link
Contributor

@techniq - The E2E won't test Remotion.Linq but it will test Aggregation and I did see the same exception you reported here. If you have a project, just push it to a branch/repo under your username and put the link here. Thanks - R

@robward-ms
Copy link
Contributor

@techniq - There are a few errors popping up, let's address each one:

a.) EFCore 2.0 - Aggregation fails due to lack of AsQueryable support, the original for EF Core 6132.: This overload of the method 'System.Linq.Queryable.AsQueryable' is currently not supported.

b.) Remotion.Linq v2.2.0-alpha.5 and EFCore 2.0 - Aggregation fails with System.AccessViolationException in OData. Seen when using these assembly redirect; EF Core 2.0.0 was not released to work with Remotion.Linq v2.2.0. I'm inclined to think of this as a compatibility issues when those two versions.

c.) EF Core 2.1.0-preview2-30148 - This is designed to work with Remotion.Linq v2.2.0-alpha.5 or better and longer throws the NotImplemented exception as in the first case or AccessviolationException as in the second case but also does not return the correct result.

WebApi constructs LINQ queries to run against EF Core and those are failing at the moment. I have seen both invalid column ID and Object reference not set to an instance of an object thrown when enumerating the LINQ query WebApi constructed against EF Core.

So at the moment, I think EF Core 2.1.0 or better is required and there seems to be a bug in the LINQ generation for EF Core. I'll continue to investigate this last issue in the context of this GitHub issue, i.e. I'll not focus on the AccessViolationException or NotImplementedException, let me know if that sounds wrong to you.

@techniq
Copy link
Author

techniq commented Feb 21, 2018 via email

@robward-ms
Copy link
Contributor

@techniq - After looking at it a bit more, I think what I'm now seeing could present itself as an AccessViolationException so it's possible that case b.) and c.) are the same. Either way, I'll sort it out.

@robward-ms
Copy link
Contributor

robward-ms commented Feb 27, 2018

@techniq - OK, what I'm seeing is not something that would show up as an AV, it's caused by lack of lazy-loading in EFCore:

### Lazy loading
Lazy loading is not yet supported by EF Core. You can view the lazy loading item on our backlog to track [this feature](https://github.com/aspnet/EntityFramework/issues/3797).

In the AggregateNavigationPropertyWorks test, the Customers IQueryable<> is returned from the controller without any Order objects. When then EnableQueryAttribute class runs the query, there is no result since the Orders are null.

First thing I tried as forcing the result to include Orders, i.e. db.Customers.Include(c => c.Order), but that led to an exception about some object not implementing IComparable.

I was able to get the test to pass only by running the query over an in-memory IQueryable, i.e. db.Customers.Include(c => c.Order).ToList().AsQueryable(). This does not resemble a reasonable work-around.

I have a branch with the EFCore version of Aggregtion tests + solution here: https://github.com/robward-ms/WebApi/tree/NetCore-E2ECore-EFCore

@divega
Copy link

divega commented May 16, 2018

@robward-ms are you still seeing these issues with EF Core 2.1 RC1? BTW, we have support for lazy loading, but it needs to be enabled explicitly.

First thing I tried as forcing the result to include Order ... but that led to an exception about some object not implementing IComparable

I would expect this to work. Where is the exception coming from?

EF Core 2.1 also supports translating LINQ GroupBy with aggregates to GROUP BY in SQL. I am not super familiar with OData aggregate, but wouldn't it be ideal to leverage is rather than computing the count in memory?

@techniq
Copy link
Author

techniq commented Jun 26, 2018

Still crashing/killing the process when running on dotnet/EF Core/ASP.NET 2.1.1 with OData 7.0.0-beta4

@kosinsky
Copy link
Contributor

kosinsky commented Aug 2, 2019

Fix #1728 was merged. Could you validate with 7.2.x?

@Nerevarin117
Copy link

I also had this issue and upgrading to 7.2.x fixed it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants