Skip to content

AutofacWebApiDependencyScope never disposed and stays in finalizer queue #15

@srogovtsev

Description

@srogovtsev

Describe the Bug

We seem to have problem with AutofacWebApiDependencyScope sticking in the finalizer queue and consuming resources.

Steps to Reproduce

  1. Wire Autofac as recommended:
app
.UseAutofacWebApi(configuration)
.UseWebApi(configuration);
  1. Add something big to the scope (in our case it was a property in HttpContext, which was stored in IOwinContext, which was stored in ILifetimeScope
  2. Observe memory consumption after the request has ended: in our case we have AutofacWebApiDependencyScope in the finalizer queue, undisposed, holding a reference to disposed ILifetimeScope, which holds a reference to no longer relevant resources

Expected Behavior

AutofacWebApiDependencyScope doesn't stay in memory after the request is complete

Additional Info

It is my understanding of DependencyScopeHandler code that it, in fact, creates a AutofacWebApiDependencyScope, which is never disposed of, and as such will leave the AutofacWebApiDependencyScope for the finalizer to consume:

var dependencyScope = new AutofacWebApiDependencyScope(lifetimeScope);
request.Properties[HttpPropertyKeys.DependencyScope] = dependencyScope;
return base.SendAsync(request, cancellationToken);

This can be pretty easily rectified by adding the scope to either ILifetimeScope.Disposer or HttpRequestMessage.AddResourceForDisposal, but I assume I am missing some rationale for why this was originally done like this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions