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

Optimize hangs and stops responding. #27301

Closed
leniency opened this issue Jan 27, 2022 · 7 comments · Fixed by #27350
Closed

Optimize hangs and stops responding. #27301

leniency opened this issue Jan 27, 2022 · 7 comments · Fixed by #27350
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@leniency
Copy link
Contributor

leniency commented Jan 27, 2022

I'm having troubles with the dbcontext optimizer. The tool, both the powershell and cli versions, hang at a certain point in model generation. It doesn't proceed any further, just sits and does nothing.

The various web projects that use the DbContext work fine, it's just the optimizer. Here's an extract of the verbose logs that come out. Everything builds successfully, it logs some info about the configuration, and then hangs.

> dotnet ef dbcontext optimize --output-dir CompiledModels --namespace CompiledModels -v

... Builds project successfully...

Using context 'ApplicationDbContext'.
Finding design-time services referenced by assembly 'Enroller.Data'...
Finding design-time services referenced by assembly 'Enroller.Data'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'Enroller.Data'...
No design-time services were found.

No relationship from 'UserRole' to 'Organization' has been configured by convention...
... Lots of these, everything uses explicit configuration ...
The property 'User.Address#Address.UserId' was created in shadow state because there are no eligible CLR members with a matching name...
... Some of these for complex types...
... and then nothing - hangs here...

I've let it sit for 20+ minutes.

EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Tool: dotnet-ef 6.0.1

@leniency leniency changed the title Optimize hangs on building. Optimize hangs and stops responding. Jan 27, 2022
@ajcvickers
Copy link
Member

@leniency What is the size of your model? For example, how many entity types? Average number of properties per entity type?

@leniency
Copy link
Contributor Author

It's probably medium-small-ish in size.

Total Entities:         191
Non-Owned Entities:     167
Owned Entities:         24
Total Properties:       1486
Avg Properties:         7
Total Navigations:      641
Avg Navigations:        3

There are maybe 15 or so keyless views in there as well.

@ajcvickers
Copy link
Member

@leniency Is it possible to create a project you can share with us that reproduces this behavior so that we can investigate?

@leniency
Copy link
Contributor Author

Yes, I trimmed down the project and setup a private repo - invited you as a collaborator and can add anyone else needed. There's a readme in the root with the exact command I've been running.

@ajcvickers
Copy link
Member

@leniency Thanks. Please add @AndriySvyryd

leniency added a commit to leniency/efcore that referenced this issue Feb 2, 2022
…e T that implements a IEnumerable<T> (such as Json.net's JArray) causes and infinity loop when adding namespaces. Added a check for the sequence type's namespace so it won't attempt to re-add.
@leniency
Copy link
Contributor Author

leniency commented Feb 2, 2022

@AndriySvyryd

I made some progress on this one and tracked down the specific property that was giving issues. I have several json properties around, and most of them are either strongly typed or Newtonsoft.Json.Linq.JObject. One of them though was Newtonsoft.Json.Linq.JArray. This was causing a stack overflow exception at:

Microsoft.EntityFrameworkCore.Scaffolding.Internal.CSharpRuntimeModelCodeGenerator
  AddNamespace(Type type, ISet<String> namespaces)

JArray implements IEnumerable<JToken> which in turn implements ... IEnumerable<JToken> This gets picked up as a sequence type and so keeps calling back to AddNamespace:

private static void AddNamespace(Type type, ISet<string> namespaces)
    {
        ...

        var sequenceType = type.TryGetSequenceType();
        if (sequenceType != null)
        {
            AddNamespace(sequenceType, namespaces);
        }
    }

I have a fix and unit test in my fork here:
leniency@6e09df5

Should I send this in as a pull request, or look at a different method to solve?

@AndriySvyryd
Copy link
Member

@leniency Nice work. Please send a PR, but instead of stopping recursion on all known namespaces stop it when sequenceType is same as type.

@AndriySvyryd AndriySvyryd added this to the 7.0.0 milestone Feb 4, 2022
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 4, 2022
AndriySvyryd pushed a commit that referenced this issue Feb 4, 2022
@AndriySvyryd AndriySvyryd reopened this Feb 5, 2022
@AndriySvyryd AndriySvyryd modified the milestones: 7.0.0, 6.0.x Feb 5, 2022
AndriySvyryd pushed a commit that referenced this issue Feb 7, 2022
…eferences itself.

Fixes #27301
# Conflicts:
#	src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs
@ajcvickers ajcvickers modified the milestones: 6.0.x, 6.0.3 Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
3 participants