Skip to content

Causality is lost when Task.Run is used #528

Closed

Description

@davidfowl

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

  1. changed the title [-]Causality is lost when `Task.Run` is used[/-] [+]Causality is lost when Task.Run is used[/+] on Oct 5, 2019
  2. davidfowl commented on Oct 5, 2019

    @davidfowl
    MemberAuthor

    Simpler example:

    async Task StateMachine1()
    {
        await Task.Run(StateMachine2);
    }
    
    async Task StateMachine2()
    {
        await Task.Delay(5000);
    }
    0000016a796d1890 00007ff82fa127b8       96          0 WebApplication352.Program+<StateMachine2>d__3
    Async "stack":
    .0000016bf9725180 System.Threading.Tasks.UnwrapPromise`1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib]]
    --------------------------------------------------------------------------------
    0000016bf9725288 00007ff82f9feda8       96          0 WebApplication352.Program+<StateMachine1>d__2
    --------------------------------------------------------------------------------
    
  3. added
    enhancement
    on Oct 5, 2019
  4. added this to the 3.1 milestone on Oct 7, 2019
  5. modified the milestones: 3.1, 5.0 on Oct 8, 2019
  6. modified the milestones: 5.0, 6.0 on Dec 18, 2020
  7. modified the milestones: 6.0.0, 7.0.0 on Jun 21, 2021
  8. stephentoub commented on Mar 27, 2022

    @stephentoub
    Member

    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).

  9. ghost closed this as completedin #2964on Apr 14, 2022
  10. ghost locked as resolved and limited conversation to collaborators on Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

dotnet-dumpenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions