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

Fast but No Exceptions - Why recommended?

Discussion in 'iOS and tvOS' started by yugu, Aug 7, 2018.

  1. yugu

    yugu

    Joined:
    Mar 8, 2018
    Posts:
    4
    So, as described in the doc:

    1. Fast but no exceptions provides no perf benefit on IL2CPP
    2. Yet the : "we recommend using Fast but no exceptions on release builds
    to avoid undefined behaviour."

    Why this is the case?
    By avoiding undefined behavior, is this referring to crash the game immediately when native exceptions happens so that game don't continue to run in an undefined state?

    It's quite confusing without much context provided. Can someone clarify this?

    Thanks a lot!
     
    dan_ginovker, Hinuko and protopop like this.
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    This is how I understand it.
     
  3. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    The incorrectly named (for IL2CPP) "Fast but no exceptions" option means that any managed exceptions which are not caught in user script code, and escape to the engine code will cause an immediate crash. With the "Slow and safe" option selected, those exceptions will simply be logged by the engine code, meaning that user script code be left in an invalid state.

    The is what the documentation means by "avoiding undefined behavior".
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,003
    If a script gets an exception with "slow but safe", it will stop executing at that point, which means your code will not do what it's intended to do, leading to "undefined behaviour".
     
  5. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Sorry, I don't believe this is correct. In "slow but safe" mode, when a exception escapes from user script code to the engine code (e.g. out of an Update method), Unity will log the exception and continue execution. Script code execution will not stop at this point, and the app will continue to run.
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,003
    I stand corrected.

    (although, for clarity I didn't mean the app will not continue to run, I just meant the rest of the specific script that got an exception will not run, which has been my experience, although it may have been cause a lot of the rest of the code depended on that null not being null?)
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Ahh, sorry, I misunderstood. Your statement here is correct, none of the script that occurs after the exception will execute.

    This is a rather confusing topic, and one that we should simplified, IMO.
     
    AcidArrow likes this.
  8. unity_grJWwPgd0nddEA

    unity_grJWwPgd0nddEA

    Joined:
    Feb 4, 2020
    Posts:
    16
    Sorry for being the gravedigger here, but I'm trying to understand a little bit more about what you mean by: "avoiding undefined behavior".

    Can you clarify a little bit better of what is this behaviour and why avoiding it its important?

    And this is nowadays valid to Android as well?
     
    protopop likes this.
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Sure! If a managed exception occurs and is caught, but execution continues, your code could get into a state that is does not expect. Suppose some deserialization occurs, but throws an exception. Then maybe the the value of a field will not be deserialized, but will instead have its default value. That might cause a problem elsewhere in your code.

    No, this option only applies to iOS.
     
    unity_grJWwPgd0nddEA likes this.
  10. unity_grJWwPgd0nddEA

    unity_grJWwPgd0nddEA

    Joined:
    Feb 4, 2020
    Posts:
    16

    That's perfect! I actually caught the exact issue that you mentioned in a released build with SlowAndSafe! And caused a lot of other problems! Thanks for the explanation!

    About the android builds, so basically android works by default as FastButNoException? Would be correct to assume that?
     
  11. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Yes!
     
    unity_grJWwPgd0nddEA likes this.
  12. unity_grJWwPgd0nddEA

    unity_grJWwPgd0nddEA

    Joined:
    Feb 4, 2020
    Posts:
    16
    Thank you so much for the feedback!
     
  13. HugoClip

    HugoClip

    Joined:
    Feb 28, 2018
    Posts:
    52
    I recall we had some issues with exceptions not crashing in Android. For testing purposes we had to force the app to crash when a unhandled exception was thrown, but from your answer this should not be true.

    Also by a managed exception you mean exceptions thrown in C# user code correct?
     
  14. unity_KGLtSbzBuqkYuA

    unity_KGLtSbzBuqkYuA

    Joined:
    Aug 5, 2019
    Posts:
    2
    I can't find anywhere in the docs what callback will be invoked before the crash, using the FastButNoException setting. I need to track the error before the app crashes, previously AppDomain.CurrentDomain.UnhandledException was invoked but I'm not sure that's the case anymore.

    @JoshPeterson what event should I subscribe to, in case I want to know about the exception, just before the crash using fastButNoExceptions?
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    The AppDomain.UnhandledException event is the one to subscribe to. If this is not triggered, can you submit a bug report? Something must be wrong, because it should be triggered.
     
  16. protopop

    protopop

    Joined:
    May 19, 2009
    Posts:
    1,549
    Just a note, I always used Fast but no exceptions for my games. mostly because of the better performance idea.

    I had an InvalidCastException in a test version of my game today that would crash it. Something I was going to remove, but instead I thought it was a good chance to test how Slow and Safe would handle it.

    My game wash crashing on an InvalidCastException but it kept playing when I was on Slow and Safe. It didn't appear to affect the gameplay, but I think I am going to release my next update as slow and safe because why not if you reduce crashes and there's no performance hit.

    I will leave it on Fast no exceptions while I am testing though, because its good to catch every problem you can - a bug free game is better. But I don't see any reason not to publish with slow and safe since its just an extra layer of protection against player frustration, presuming any invalid states allowed to continue don't adversely affect gameplay, or represents a good enough trade off.
     
  17. unity_grJWwPgd0nddEA

    unity_grJWwPgd0nddEA

    Joined:
    Feb 4, 2020
    Posts:
    16
    The biggest problem with Slow and Safe is: You game can end in what unity calls "Undefined Behaviour" and this can cause way more harm than a simple crash, for instance in a project I've worked in the past we had some cases of using corrupting their whole save data because of a random crash that was supposed to crash the game but It didn't since was published on Slow And Safe.

    For me it should be the other way around, use slow and safe on development since this can spill out more information to help you fix those crashes, but releases, always with Fast but no Exceptions.
     
    AcidArrow and protopop like this.
  18. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    227
    Hi @JoshPeterson I tested the option "Slow and Safe" vs "Fast but no exception" on iOS 16, Unity 2021.3.2f1

    I intentionally make a nullref exception when a button is clicked.

    I built within unity as release build, generated the Xcode project, and I ran both of my project in Xcode debug build.

    Expectation
    - Slow and Safe: App does not crash
    - Fast but no exception: App crashes

    Actual result
    - Slow and safe: App didn't crash
    - Fash but no excception: App didn't crash

    Is this a correct behaviour? Does "Fast but no exception" option's "crashing immediately" happens only if both Unity and Xcode is built with Release option?
     
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    No, I don't think a debug vs. release build in Xcode should impact this behavior. I'm not sure why they seem to behave the same. Your understanding of what should happen is the same as mine. Is there any hint in the player log file about what happened?
     
  20. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    227
    I didn't find any meaningful logs from player logs.:(

    I tested using various scenarios and here is the results:

    1. Editor
    1. The exception had occured on the Main Thread
    - Slow and safe: Exception detected, and the game keeps running
    - Fast but no exception: Exception detected, and the game keeps running​

    2. The exception had occured on a Task that was running on a Background Thread
    - If the task was awaited, then both mode catches exception
    - Else, then both didn't catch exception​

    2. iOS Device
    1. The exception had occured on the Main Thread
    - Slow and safe: Exception detected, and the game keeps running
    - Fast but no exception: Exception detected, and the game keeps running​

    2. The exception had occured on a Task that was running on a Background Thread
    - Slow and safe: No crashes wether the task was awaited or not
    - Fast but no exception: Crash occurs only if the exception is thrown from a task that is awaited on main thread​
     
  21. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    I'm unclear what the expected behavior is in each of these cases. I've asked our mobile team about it though, as I believe this option is specific to iOS.
     
    NGC6543 likes this.