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

Fix memory leak in ModelContainer - #1101 #1506

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

mikepizzo
Copy link
Member

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)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

Explore other options. Could add modelIds to Request and have it clean up the modelIds directly.

@@ -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>
Copy link
Member

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);
Copy link
Member

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);
Copy link
Member

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 ()
Copy link
Member

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

{
HttpRequestScope httpRequestScope = request.RequestContainer.GetService(typeof(HttpRequestScope)) as HttpRequestScope;
HttpRequest httpRequest = httpRequestScope == null ? null : httpRequestScope.HttpRequest;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space

Copy link
Contributor

@AlanWong-MS AlanWong-MS left a 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

@AlanWong-MS
Copy link
Contributor

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()
Copy link
Contributor

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?

@brjohnstmsft
Copy link
Contributor

@mikepizzo Will this PR be merged eventually, or has the memory leak been solved some other way...?

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

Successfully merging this pull request may close these issues.

5 participants