aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

Don't hold onto the HttpContext in the HostingLogScope #1531

Closed davidfowl closed 6 years ago

davidfowl commented 6 years ago

Intentional memory leak

using System.Threading;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication23
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {

        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            var cts = new CancellationTokenSource();

            app.Run(async (context) =>
            {
                cts.Token.Register(() =>
                {

                });

                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}

This code will capture every HttpContext in the logging scope:

image image

It's rooted by the scope which is being captured in the CancellationToken callback.

cc @benaadams

benaadams commented 6 years ago

Getting gummed up everywhere image