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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Jobs in Editor with EditorCoroutines

Discussion in 'Entity Component System' started by tonytopper, Jul 29, 2022.

  1. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    200
    Having problems with this loop never finishing evening though I am fairly sure the last job in that array is in fact complete. This is inside an EditorCoroutine.

    Code (CSharp):
    1. while (!jobHandles[^1].IsCompleted)
    2. {
    3.     yield return null;
    4. }
    What's more frustrating is it only hangs there sometimes.

    Other times it will hang in different spots with this message, which never goes away.

    Dreaded_Screenshot 2022-07-29 085819.png

    I've tried adding breakpoints to the for loop it hangs in but this makes it not hang.

    One of my suspicions is there may be an interaction between Jobs and EditorCoroutines. In this case, it's a coroutine within a coroutine, but I've tried it by just nesting the IENumerator in a yield as well.

    It's a complex process, so this issue has been hard to diagnose. It's part of a complex world-creation coroutine queue system where some coroutines schedule jobs.

    Looking for insights into unraveling this mystery. What else should I be looking at?

    Unity: 2022.1.11f1
    Project: URP. Not using DOTs but using Jobs.
    Jobs Version 0.51.0-preview.32
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,594
    Maybe you got some coorutine infinity loop or so? Are you able to narrow problem down? I. E. disable jobs.

    BTW, jobs are part of DOTS, so even not using all packages, you are stil using DOTS.
     
    tonytopper and Anthiese like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,200
    How do you start the coroutine? Maybe you do that too often.
    Editor is not calling events as regular as you would expect. At least for Update there can be significant delays when there is no user interaction. Since coroutines are tied to frame updates maybe that is an issue.
    A JobHandle array looks like you could simply use JobHandle.CombineDependencies (or similar) to get one handle for all the jobs you schedule. Maybe try that first.

    I‘ve used jobs in editor coroutines without issues btw.
     
    tonytopper likes this.
  4. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    200
    Embarrassingly, the first hang was from a situational infinite loop and the second one may be.

    I put timeouts on them. But even then with an infinite job running Unity can be unstable, ofc. Often I get this hang-up.

    Screenshot 2022-08-08 090539.png

    Moving this system to Unity's Jobs has led to excellent performance gains, but troubleshooting has gotten a good bit harder.

    For the second hang, it could be something else. I haven't seen this version go infinite.

    I am using a custom scheduler there. It will schedule 100+ series of 7 linear jobs. So there would be 700+ jobs scheduled. And I am starting to wonder if the jobs ever start, but my job debugging skills probably need to be leveled up.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,200
    700+ jobs sounds like a lot. I remember one forum post where Mr. Ante mentioned that hundreds/thousands of jobs scheduled (per frame) would not be a good idea. I do not have personal experience with that and it may be different in editor where users mind more about getting the jobs to complete rather than sustaining 60+ fps.

    I may be getting to that point soon though. I mean in-editor jobs, once I jobify my mesh graph editor.
     
  6. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    200
    This is a map generation task. So the user will basically be watching some sort of world generation loading screen. I am migrating our prototype for world generation to Jobs so I can get this process down to ~30 seconds or less.

    The second problem was something inside NativeQueue. A bug I think. I switched to a NativeList and the problem went away. For a while, I was getting a Null Reference Exception from within Unity's NativeQueue code but it wasn't affecting anything so I ignored it. Through some regression or change somewhere that error was the culprit taking down this process.

    For background on the 700+, we're creating a "Region", which is 7 linear jobs. And I might be scheduling 100+ "Region" creations at a time. Doing this with Jobs has been 10x to 100x faster.

    One of the 7 jobs is a parallel job that deals with the faces of a "Region". A region can have anywhere from 4 to 100+ faces. So theoretically a lot of NativeQueues could be running at once. I suspect this was jacking up something inside Unity's NativeQueue code.

    Here's a visual example:

    Screenshot 2022-08-10 110832.png