Repository navigation
Closed
Description
Causality is lost when Task.Run is used. This makes sense but I wonder if it's possible to stitch the async stacks in this case. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace WebApplication352
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await Task.Run(async () =>
{
await Task.Delay(10000);
await context.Response.WriteAsync("Hello World!");
});
});
});
}
}
}This is what dumpasync -stacks looks like:
0000021214e64c18 00007ff8325691c0 96 0 WebApplication352.Startup+<>c__DisplayClass1_0+<<Configure>b__2>d
Async "stack":
.0000021114e82640 System.Threading.Tasks.UnwrapPromise`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib]]
--------------------------------------------------------------------------------
0000021114e82688 00007ff832568770 96 0 WebApplication352.Startup+<>c+<<Configure>b__1_1>d
Async "stack":
.0000021114e82728 (0) Microsoft.AspNetCore.Routing.EndpointMiddleware+<<Invoke>g__AwaitRequestTask|6_0>d
..0000021114e827b0 (0) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__9
...0000021114e82840 (1) Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__217`1[[System.__Canon, System.Private.CoreLib]]
....0000021114e82900 (0) Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequestsAsync>d__216`1[[System.__Canon, System.Private.CoreLib]]
cc @stephentoub
Activity
[-]Causality is lost when `Task.Run` is used[/-][+]Causality is lost when Task.Run is used[/+]davidfowl commentedon Oct 5, 2019
Simpler example:
stephentoub commentedon Mar 27, 2022
I've rewritten dumpasync in C# and I've addressed this as well (or at least the simpler repro successfully connects the stacks... I didn't try the original).