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.

Question UpdateNavMeshDataAsync Locking Main Thread

Discussion in 'Navigation' started by metallibus, Mar 12, 2023.

  1. metallibus

    metallibus

    Joined:
    Jun 1, 2019
    Posts:
    9
    If I submit a bunch of mesh data to NavMesh.UpdateNavMeshDataAsync(), the function itself does in fact run asynchronously, but it's queueing up a bunch of job work that seems to lock up the main thread either on that frame or a subsequent frame shortly after, while some large amount of the work completes. The returned AsyncOperation still isn't marked complete for a few more frames, so some amount of it is happening separately, but I'm consistently seeing one unbearably-long frame along the way.... Is there some way to remove this frame locking/bottleneck and allow the main thread to continue rendering without waiting on parts of the nav mesh jobs?

    These calls seem to be dominated by NavMesh.TileMesh... It seems that the frame doesn't start rendering until a bunch of these are finished, though some do seem to persist beyond the frame render, so not sure if this is really the culprit.

    upload_2023-3-12_15-35-12.png upload_2023-3-12_15-35-37.png

    Basically everything else is idle at this point, so I don't think there's anything else holding up the render timing:
    upload_2023-3-12_15-39-45.png
     
  2. metallibus

    metallibus

    Joined:
    Jun 1, 2019
    Posts:
    9
    So, a little bit more info here...

    After drilling a little deeper, and closing the Scene view while profiling, it looks like the main thread is getting hung up trying to execute some rendering job...

    upload_2023-3-16_9-44-43.png

    It looks to me like the rendering system is trying to use a job for some portion of its logic, seemingly related to shadows, and waiting on the result.... But since the NavMesh system has submitted so many jobs, all the workers are full, so it looks to be switching to help finish those jobs, until its own is then executed....

    This seems insane.... If the render system needs jobs, why does it not have its own worker pool? Why can't it increase its own priority? It seems pretty crazy that submitting enough work to a system that's meant to help unblock main thread work, can in itself block the main thread...

    That being said, maybe this becomes more of a jobs question and less of a NavMesh question... unless there's some way to control how many workers the NavMesh calls use? I don't see anything clear, but since lots of these workers are stuck in mesh tiling, maybe submitting fewer meshes than there are job workers would alleviate this? But it would greatly slowdown NavMeshing performance, especially because I should be able to process multiple meshes per worker per frame, but wouldn't be sure that I won't bottleneck something if I try doing so. This seems a little silly to juggle, and also will mean I have to be careful about what other jobs I'm submitting as well....
     
  3. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    1,801
    @metallibus I suggest u submit bug report for this issue
     
  4. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    255
    It doesn't solve the overall problem I guess, but you actually can set MaxJobWorkers in NavMeshBuildSettings.