Skip to content

Provide an API for traversing/reasoning about async call stacks #14434

Open

Description

@davidfowl

Any tool that shows exceptions today also shows the callstack. With the introduction of the async keyword (yield was the same I guess), it's becoming increasingly harder to show reasonable call stacks because of the async state machine. It would be nice if there was an API somewhere that could be used to clean up the async state machine and pretty print a call stack with readable method names (tools like reflector do this).

Here's an example of a typical error page in an MVC 6 application:

image

Activity

  1. Maxwe11 commented on Apr 10, 2015

    @Maxwe11
    Contributor

    👍 👏

  2. HaloFour commented on Apr 14, 2015

    @HaloFour

    Check out this article from MSDN Magazine:

    Async Programming: Async Causality Chain Tracking

    It would have something like that built-in. However if it would have to capture the stack trace at the point of awaiting it would probably add a non-trivial amount of overhead, as the article mentions.

  3. AlfredoMS commented on Apr 14, 2015

    @AlfredoMS
    Contributor

    @HaloFour @davidfowl Async Causality tracking existsin mscorlib, see https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Threading/Tasks/AsyncCausalityTracer.cs
    VS2013 uses it to populate some of the information in the Task window.
    Although I would agree it needs to be improved for xplat and there could be more tools taking advantage of its output.

  4. HaloFour commented on Apr 15, 2015

    @HaloFour

    Hrm, doesn't look like something you can use programmatically from within
    your own program? I used the tricks from that MSDN Magazine article to
    implement better exception messages in my web app as it was heavily async.
    On Apr 14, 2015 7:57 PM, "Alfredo Menendez Sancho" [email protected]
    wrote:

    @HaloFour https://github.com/HaloFour @davidfowl
    https://github.com/davidfowl Async Causality tracking existsin
    mscorlib, see
    https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Threading/Tasks/AsyncCausalityTracer.cs
    VS2013 uses it to populate some of the information in the Task window.
    Although I would agree it needs to be improved for xplat and there could
    be more tools taking advantage of its output.

    —
    Reply to this email directly or view it on GitHub
    https://github.com/dotnet/corefx/issues/1370#issuecomment-93117933.

  5. noahfalk commented on Oct 13, 2015

    @noahfalk
    Member

    Adding @tmat
    @davidfowl Have you talked to the Roslyn guys about this at all? Ostensibly the abstraction you are trying to restore comes in two parts:

    1. The framework provides the Task libraries. The fact that TaskAwaiter.GetResult() has calls to TaskAwaiter.HandleNonSuccessAndDebuggerNotification is a framework detail, so it would seem reasonable that any algorithm to clean up that part of the stack trace is owned by the framework.
    2. The C# compiler provides the implementation of async and code-rewriting that creates the async state machines. Once we hit runtime the only information that could tie d__43.MoveNext() to a source location is contained inside custom debug information records in the PDB.

    I'd assume the Roslyn Expression Evaluator, which runs within Visual Studio Debugger, has the algorithms you are looking for to sanitize these stack traces. Given that ASP.NET is already loading the compiler portion of Roslyn to runtime compilation, maybe we should also have you guys load the debugger portions of Roslyn for diagnostics such as this? I'm hoping @tmat can shed a little more light on how this is factored, or give us bad news that some key component is inside VS debugger itself rather than in the EE.

  6. davidfowl commented on Oct 27, 2015

    @davidfowl
    MemberAuthor

    @tmat @gregg-miskelly Any ideas for this one? We could use heuristics to collapse the state machine frames (perhaps hide anything compiler generated?)

    /cc @Tratcher

  7. RichiCoder1 commented on Oct 27, 2015

    @RichiCoder1

    Wanted say this would be a big win. Async stack traces are a serious pain to diagnose, and adding a mechanism to erase implementation details (TaskAwaiter, GetResult, ThrowForNonSuccess) and unify the generated state machines into something comprehensible would be amazing.

  8. gregg-miskelly commented on Oct 27, 2015

    @gregg-miskelly
    Contributor

    The code that the VS debugger uses for walking the async stack isn't open, but I am happy to point Microsoft employees to it. The only role that the EE has in this is to give the 'MoveNext' method a better name.

  9. gregg-miskelly commented on Oct 27, 2015

    @gregg-miskelly
    Contributor

    Though I should say that if the problem we are trying to solve is the stack trace for an exception object (and not some sort of new 'Environment.GetAsyncStack()') then I am not sure that any of the code that the debugger has will be all that useful. We likely need enhancements/enlightenments between the stack trace code and the task implementation code to make that story good.

  10. noahfalk commented on Oct 27, 2015

    @noahfalk
    Member

    The debugger has code that understands the C# async state machine and how it relates back to the source the user originally authored right? Presumably that is the key bit that is missing.

    I agree the solution would look a bit different if we were modifying Exception.StackTrace vs. creating a new API, but I think both solutions would require reversing the C# compiler's state machine construction.

  11. gregg-miskelly commented on Oct 27, 2015

    @gregg-miskelly
    Contributor

    If we are only concerned with fixing Exception.StackTrace, I actually think we don't need to decode the state machine because the method in the state machine class (.MoveNext) is on the stack already. What we need to do is clean up the task goo. It might also be nice to use method/class names that the user understands rather than the metadata names.

  12. noahfalk commented on Feb 19, 2016

    @noahfalk
    Member
  13. aelij commented on Feb 19, 2016

    @aelij
    Contributor

    I think both solutions would require reversing the C# compiler's state machine construction.

    That's what I do in order to get the original method name. The downside is that it's compiler- and language-specific. Perhaps a long-term solution could be to have the compiler emit an attribute for the async state machine class that includes a string with the original method's signature. That way we could also print the full parameter list.

  14. tmat commented on Feb 19, 2016

    @tmat
    Member

    We emit Async/IteratorStateMachineAttribute on the kickoff method that points to the state machine type.

    We will emit reverse mapping to Portable PDBs (I don't think we do that right now, but certainly will do so for dotnet cli release): https://github.com/dotnet/corefx/blob/master/src/System.Reflection.Metadata/specs/PortablePdb-Metadata.md#StateMachineMethodTable

    We do stack cleanup and formatting for the REPL as well:
    http://source.roslyn.io/#Microsoft.CodeAnalysis.Scripting/Hosting/ObjectFormatter/CommonObjectFormatter.cs,d92226e92a7dee26,references

    there is a few hacks that I want to get cleaned up at some point, but it generally works.

  15. 8 remaining items

  16. benaadams commented on Oct 18, 2017

    @benaadams
    Member

    Had a go at changing the StackTrace by changing the stack :)

    Has some outstanding issues/questions though dotnet/coreclr#14564

  17. benaadams commented on Jan 18, 2018

    @benaadams
    Member
  18. removed their assignment
    on Aug 24, 2018
  19. transferred this issue fromdotnet/corefxon Jan 31, 2020
  20. added this to the 5.0 milestone on Jan 31, 2020
  21. added
    untriaged
    on Feb 23, 2020
  22. removed
    untriaged
    on Mar 7, 2020
  23. modified the milestones: 5.0.0, Future on Jun 17, 2020
  24. added
    backlog-cleanup-candidate
    on Dec 17, 2024
  25. niemyjski commented on Dec 17, 2024

    @niemyjski

    Bump

  26. removed
    backlog-cleanup-candidate
    on Dec 17, 2024
  27. mattzink commented on Dec 18, 2024

    @mattzink

    Tool that produces clean ResolvedStackTraces https://github.com/benaadams/Ben.Demystifier

    Ben's library has been unmaintained for a long time. Would still love to see an official solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Diagnostics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions