Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Catching Coroutine Exceptions and StackTracing CoRoutines

Discussion in 'Scripting' started by a436t4ataf, Nov 26, 2020.

  1. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,873
    ...anyone done this succesfully? :)

    My current success:

    1. I 'fixed' Unity's StartCoroutine method so that instead of silently crashing when it hits Exceptions, exceptions are properly supported, and reported, and they stop the flow of execution. This is done using Microsoft's own suggested (unoffficial) workaround for the bugs in Microsoft's code that cause Unity coroutines to fail on exceptions normally.

    2. I can wrap any call to StartCoroutine with a call that preserves the callstack, fixing the forever-bug in StartCoroutine that it destroys callstacks, making debugging super hard. This is done by manually capturing the stacktrace every time you start a coroutine, and storing it with the coroutine, then retrieving it if needed (eg if an exception is caught).

    Net effect: I can debug Unity3D apps that use coroutines much better and much much faster (no more "hit and hope" debugging! You can see actual line numbers for every exception, even embedded multiple layers deep in coroutines).

    My disappointments:

    i. Unity Console ignores the line-numbers in my stacktraces, even though the full filename + line-number are there and preserved. I'm not sure what the 'magic' is that's required to make Unity's Console work correctly here --- any pointers?

    ii. Why is this not built-in to Unity? :( :( :(

    iii. It means the code:
    "yield return StartCoroutine( foobar() )"
    has to become
    "yield return StartCoroutine( FixUnityCoroutine( StartCoroutine( foobar() ), new StackTrace( true ) ) )"
    which is a lot more typing and - as far as I can tell - can't be avoided, since C# doesn't allow macros.

    (note: that "extra typing" cannot be avoided using basic OOP/functions/etc, would have to be done with macros/preprocessors, because: Microsoft's implementation of IEnumerator exists outside the main C# systems - that's what lets it do its magic! Its implemented differently at the compiler level, it's not using the core C# systems)

    Any improvements?
     
    Last edited: Nov 26, 2020
  2. ByMedion

    ByMedion

    Joined:
    May 10, 2018
    Posts:
    19