Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Async methods continue to execute when you stop the game in the editor, potentially dangerous

Discussion in '2018.3 Beta' started by Prodigga, Dec 1, 2018.

  1. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Hi there

    I understand that the assemblies do not get reloaded when you stop the game in the editor, hence async methods continue to execute despite the fact that you stopped the game in the editor.

    An example of a dangerous scenario:
    - On Awake, an Async method waits 10 seconds, then destroys that gameobject.
    - The user hits play in the editor.
    - The user hits stop before the 10 seconds are up
    - now, in edit mode, the 10 seconds are up and the gameobject destroys itself.
    - user saves the scene without realizing
    - losses work...

    1) I don't think many people are aware of this. Every time I bring it up on Reddit, people thank me because I just helped them Dodge a bullet.
    2) I think the "expected" behavior is for the async methods to stop executing like coroutines when you stop the game in the editor.
    3) If nothing is done by Unity to prevent execution after stopping the game in the editor, then the work falls on us developers. We have to make sure we add Application.IsPlaying checks after every await.. just to patch around something that is only an issue in the editor. Not to mention the fact that you have to know about this quirk in the first place.
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    That's a great observation, thanks for sharing it. I didn't know that.

    Thinking of Unity as a safe sandbox, I actually expected that async methods there were started during playmode don't fire if the editor stops playmode.

    I wonder if Unity Technologies would like to get a bug-report on that issue.
     
    M_R and Prodigga like this.
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    it's not just async methods it's threads generally. If you forget this behavior it can cause all kinds of havoc.
     
  4. buFFalo94

    buFFalo94

    Joined:
    Sep 14, 2015
    Posts:
    273
    i had the same problem today with async / await and Task
     
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Yes, first thing to realize about async methods and why I decided to only use it for editor code at the moment. I don't think it will be easy for Unity to "fix" this, since it's the same thing with threads.
     
  6. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    231
    Use a static cancellation token that gets fired upon
    OnApplicationQuit
     
  7. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    I also have submitted a bug report months ago (1064557), and they said it's a duplicate of this https://issuetracker.unity3d.com/is...operation-doesnt-stop-after-exiting-play-mode that is marked as won't fix without further info :( :(

    if you use a cancellation token, then you get
    TaskCanceledException
    error on the console for each active task, unless you catch it in the tasks.

    a difference from threads is that Unity is managing the synchronization context, so they can stop/cancel all tasks from there.
     
    Bshsf_9527, Prodigga and SugoiDev like this.
  8. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Man, the issue tracker website feels like such a black box and so disconnected from the forum and the community. Can't believe judgement has been passed on this issue ages ago (January!) with only 3 votes. I bet thats also '3 views', how many people would've seriously seen that entry?

    How do we go about getting this re-reviewed or getting some sort of proper developer response? I feel like a whole conversation has been had on this issue, and all we the community get to hear about it is that it 'wont be fixed' with no explanation what so ever.

    I think this is the key thing to note here.
     
    Last edited: Dec 3, 2018
  9. mkderoy

    mkderoy

    Unity Technologies

    Joined:
    Oct 25, 2016
    Posts:
    22
    Right now we've chosen to follow the existing behavior of threads within Unity. We're looking into an overarching solution for this issue.