-
Notifications
You must be signed in to change notification settings - Fork 475
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
Fix memory leak in ModelContainer - #1101 #1506
base: master
Are you sure you want to change the base?
Conversation
@@ -521,19 +539,21 @@ public virtual object ApplyQuery(object entity, ODataQueryOptions queryOptions) | |||
/// <param name="modelFunction">A function to get the model.</param> | |||
/// <param name="path">The OData path.</param> | |||
/// <param name="createErrorAction">A function used to generate error response.</param> | |||
/// <param name="request">The HttpRequest.</param> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HttpRequestMessage?
@@ -509,7 +514,20 @@ public virtual object ApplyQuery(object entity, ODataQueryOptions queryOptions) | |||
throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull); | |||
} | |||
|
|||
return new ODataQueryContext(model, elementClrType, path); | |||
ODataQueryContext queryContext = new ODataQueryContext(model, elementClrType, path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Core version, to use "ODataFeature" to add the Per-request information.
In Classic version, to use "ODataProperties" to add the Per-request information.
string index = _map.GetOrAdd(model, m => Guid.NewGuid().ToString()); | ||
_reverseMap.TryAdd(index, model); | ||
string index = Guid.NewGuid().ToString(); | ||
_map.TryAdd(index,model); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think TryAdd should be GetOrAdd, maybe i am wrong?
@@ -483,6 +483,8 @@ public class Microsoft.AspNet.OData.ODataQueryContext { | |||
Microsoft.OData.Edm.IEdmNavigationSource NavigationSource { public get; } | |||
ODataPath Path { public get; } | |||
System.IServiceProvider RequestContainer { public get; } | |||
|
|||
protected virtual void Finalize () |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to change the Core version publicApi.bsl
…zzo/WebAPI # Conflicts: # src/Microsoft.AspNet.OData.Shared/EnableQueryAttribute.cs # src/Microsoft.AspNet.OData.Shared/Query/Expressions/ModelContainer.cs
{ | ||
HttpRequestScope httpRequestScope = request.RequestContainer.GetService(typeof(HttpRequestScope)) as HttpRequestScope; | ||
HttpRequest httpRequest = httpRequestScope == null ? null : httpRequestScope.HttpRequest; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can fix the extra line in a different PR if you want since there's huge overhead in rebuilding and re-testing for the build pass
Are you still exploring the route of adding ModelIds to Request and having Request do the cleanup? |
/// <summary> | ||
/// Destructor called to clean up reference in ModelContainer | ||
/// </summary> | ||
~ODataQueryContext() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use the IDisposable pattern instead of destructor?
@mikepizzo Will this PR be merged eventually, or has the memory leak been solved some other way...? |
Issues
This pull request fixes issue #1101.
Description
Explores addressing memory leak by having each QueryContext keep it's own reference to a model and then cleaning it up in finalizer. Adds QueryContext to Request to ensure that the QueryContext is not cleaned up before the request is finished with it.
Checklist (Uncheck if it is not completed)
Additional work necessary
Explore other options. Could add modelIds to Request and have it clean up the modelIds directly.