Search Unity

Async problems

Discussion in 'Scripting' started by yonek_idreams, Jul 24, 2019.

  1. yonek_idreams

    yonek_idreams

    Joined:
    Sep 10, 2014
    Posts:
    11
    Hi!

    I have a game that has several thousands users per day and one in 1000 gets an fatal error from my async code.
    There is a lot of anonymous methods that are run with a delay that is managed by the IEnumerator, async/await or a custom event loop that triggers events from its Update method. All of them are having issues where the code inside the anonymous method can't be run because the object doesn't exists anymore.
    This issue happens also when a code is triggered from Unity's UI input or System.Net calls.

    As I've said it is very very rare error, I can't event repeat it, but having thousands of users (Android/iOS) a see it happens.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,172
    When writing async callbacks you should always assume what operation can take any amount of time between request and completion and game state may change in any way. Basic example is arrow. While on launch the archer must be alive and present in scene, at the moment of hit it may be the game is already over and player watching final movie. Also there's a common pitfall scenarion, when you request operation, the game session overs and starts again and you recieve completion callback for operation this game session never requested. All that cases should be correctly handled by your async code.
     
    Vryken and Lysander like this.
  3. Lawrence57

    Lawrence57

    Joined:
    Jul 27, 2019
    Posts:
    1
    Why is asynchronous code in Javascript so complicated and confusing? There are no shortage of articles and questions from people trying to wrap their bean around it.
     
  4. Lysander

    Lysander

    Joined:
    Feb 24, 2013
    Posts:
    1,652
    ... what? No one mentioned Javascript.

    While it may still compile for the moment, Unity no longer actively develops or supports Boo or UnityScript (their version of Javascript/TypeScript), so writing code for them is a bad idea in general. I'm pretty sure support died before async operations were even readily available in Unity though (I'm not sure the new compiler actually works with UnityScript- I think it's just the old one). I also don't think async operations in UnityScript was ever a popular topic for articles and questions, so I have to assume you're talking about for web development. Why mention that here (it's completely irrelevant to Unity), and with your first post?

    I'm not trying to be a jerk, I'm legitimately confused. Did I miss something?
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    773
    I'm confused as well, especially since asynchronous code in JavaScript is very similar to asynchronous code in C#.
    Both involve marking an action as
    async
    , and then using
    await
    when calling the action to wait for its process to complete without stopping the main thread.
     
  6. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    216
    Use tracing (for example, Jaeger) to better understand structured concurrent execution bugs like async code issues.