Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Not all jobs are terminating before OnDestroy is called after updating to 2019.1b.10

Discussion in '2019.1 Beta' started by Abbrew, Apr 8, 2019.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    341
    There is a race condition that sometimes occurs when a Scene is destroyed. Some of the jobs that were scheduled do not terminate before OnDestroy is called. This causes a scheduling conflict because some of those jobs are scheduled to use Allocator.Persistent containers stored in a Monobehaviour. When the Monobehaviour calls NativeHashMap.Dispose(), sometimes those jobs are still scheduled or running, outputting error messages on the console.
    Code (CSharp):
    1. InvalidOperationException: The previously scheduled job NativeLRUCacheSingle`2:SetJob writes to the NativeArray SetJob.data.cache. You must call JobHandle.Complete() on the job NativeLRUCacheSingle`2:SetJob, before you can deallocate the NativeArray safely.
    2. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckDeallocateAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x159b914b0 + 0x00052> in <3b14123eec6449dcbdce6fe247c0f416>:0
    3. Unity.Collections.LowLevel.Unsafe.DisposeSentinel.Dispose (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle& safety, Unity.Collections.LowLevel.Unsafe.DisposeSentinel& sentinel) (at /Users/builduser/buildslave/unity/build/Runtime/Export/NativeArray/DisposeSentinel.cs:69)
    4. Unity.Collections.NativeHashMap`2[TKey,TValue].Dispose () (at Library/PackageCache/com.unity.collections@0.0.9-preview.17/Unity.Collections/NativeHashMap.cs:685)
    5. NativeLRUCacheSingle`2[K,V].Dispose () (at Assets/Scripts/Utility/NativeContainer/NativeLRUCacheSingle.cs:125)
    6. ResultCacheSingle`2[Request,Result].OnDestroy () (at Assets/Scripts/Environment/Physics/CalculatorIO/ResultCacheSingle.cs:33)
    7.  
    Some potentially helpful information:
    • The JobHandle that acts on those persistent containers is passed to EntityCommandBufferSystem.AddJobHandleForProducer(inputDeps). I sorta abuse EntityCommandBufferSystem to force jobs to complete at a certain time. A Monobehaviour usually titled [name of EntityCommandBufferSystem]Runner fetches the EntityCommandBufferSystem and manually runs it in FixedUpdate()
    • The strange thing is that whenever this error occurs, the next time I run the application, three NativeContainer leak errors appear. Those three NativeContainers are the ones that would've been disposed of had they been disposed of in the previous application run!
    If needed I can include a link to the project.
     
    Last edited: Apr 8, 2019
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,608
    It's important that you report these issues together with a reproduction project if you want them to get fixed. If you don't do it, it might be a long time until someone else reports them or until Unity Technologies find them.
    https://unity3d.com/unity/qa/bug-reporting
     
    LeonhardP likes this.
  3. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    341
    Will do!
     
    LeonhardP likes this.
  4. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    2,496
    Thank you! Please reply in here with the issue ID once you have submitted the report.
     
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    1,741
    Hey, I could be wrong here as I haven't seen a full repro (yet) but this sounds like your script is wrong.

    If you are disposing memory that might be in use by a running job, it is up to you to call JobHandle.Complete on that job first. It sounds like you should be doing that at the start of your OnDestroy method.

    This is exactly what the error message already tells you though, so maybe I'm mistaken here to be (re)stating the obvious? ;)
     
  6. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    341
    The issue ID is 1144526

    That makes sense. It looks like Unity is doing exactly as it should be, so could this bug report become a feature report? Namely, add in a feature that terminates all running jobs upon the destruction of a scene. Otherwise, we'd have to jump through hoops to pass in the JobHandle to the relevant Monobehaviours to wait on right before OnDestroy
     
    LeonhardP likes this.
  7. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    341
    I want to add that the Script Execution Order that produces the error is
    ActorsRunner
    BeforePreloaderSyncRunner
    PreloaderRunner
    BeforeActorsSyncRunner
    I may have forgotten to click "Apply" before sending the report package
     
  8. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    1,741
    Yeah it’s a possibility - that bug report should find its way to someone who can give you a more authorative answer on the best way to solve this. It should definitely be easy for you to do the right thing, and right now it sounds like it’s not :) Thanks for reporting it!
     
  9. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    1,741
    You should be able to reply to the fogbugz email to append that info - it would be best to make sure all the info is in the bug report. Alternatively, QA will be in touch if they can’t repro it :)
     
    Peter77 likes this.
unityunity