Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by elveszett42 · Apr 23, 2021 at 08:59 AM · voxelthreadingchunk

Infinite Task ending abruptly.

Hey. I'm doing a voxel game and I'm trying to use a Task for the world generation. The way it works, simplified, is that you add chunk positions to a queue, while an infinite Task checks that queue and creates the necessary chunks if there's a position in the queue. This is the task:

    public async Task GenerateQueuedCenters () {
         while (!TaskSynchronization.GameClosed) {
             foreach (var request in centersToGenerate) {
                 await GenerateChunksFromCenterAsync(request);
             }
             centersToGenerate.Clear();
             await Task.Delay(50);
         }
         Debug.Log("Generator listener says: That's it, I quit this job.");
     }

I call this task with a simple Task.Run(() => GenerateQueuedCenters());. It works fine for a while, until suddenly the task stops for no reason. Doing some tests, I've discovered that the task always stops after the foreach loop, but before centersToGenerate.Clear(). The task fails, as I've also tested its status and it, indeed, is "TaskStatus.Faulted" (to test that, I called it like this: Task.Run(() => __REMOVE_GenerateQueuedCenters()).ContinueWith(t => { if (t.Status == TaskStatus.Faulted) Debug.Log("Fail"); });.

I have no idea why it may be failing. I'd like to know if it is something inherent to unity, or if there may be any other piece of code that could cause such a crash. I know I could do this with a coroutine, but I don't want to bound this method to fps, but instead have it dispatched on a background thread so fps don't drop no matter what.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by unity_ek98vnTRplGj8Q · Apr 23, 2021 at 03:57 PM

This looks like it could be a Concurrent Modification error. You are not allowed to modify a collection (centersToGenerate in your case) while it is being iterated through (using the foreach loop). You will need to use a lock to make sure that when you are going through your list no other code is allowed to add or remove from that list, or you need to go through your collection in a way that doesn't use an iterator, such as using a for loop or a while loop. I recommend using a Queue for your collection, and doing something like

 while(!myQueue.isEmpty()){
     myQueue.Pop();
 }

In your task, which is a nice efficient way to handle this kind of problem

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by Bunny83 · Apr 23, 2021 at 05:34 PM

Well, there might be an exception somewhere in your code. An exception would of course terminate the execution. First you might want to attach visual studio as a debugger and maybe step through your code to see where it fails. Another way to get some insight what might happening is to wrap your code in a try catch statement and log any exception thrown. Something like that:

 public async Task GenerateQueuedCenters () {
     try {
         while (!TaskSynchronization.GameClosed) {
             foreach (var request in centersToGenerate) {
                 await GenerateChunksFromCenterAsync(request);
             }
            centersToGenerate.Clear();
            await Task.Delay(50);
        }
        Debug.Log("Generator listener says: That's it, I quit this job.");
     }
     catch (Exception ex) {
         Debug.LogException(ex);
     }
 }

Do you get an exception? if that's the case you can also put a break point inside the catch in visual studio so the execution will halt there. This might give you a chance to inspect the exception and get more details about its origin. If that doesn't help, break down your code further and see where the error may occur. Though break points are usually the best way to debug your code.


ps: you said that "centersToGenerate" is a queue. Did you mean that literally? If so, what exact type is that "centersToGenerate" variable? As @unity_ek98vnTRplGj8Q mentioned, if you alter the collection while iterating over it an exception would be thrown.


For anything asynchronous you may want to have a look at the ConcurrentQueue class which is specifically made for queueing and dequeueing items from seperate threads. You would generally use TryDequeue in your processing loop. So with a concurrent queue it would look something like that:

 while (!TaskSynchronization.GameClosed) {
     while(centersToGenerate.TryDequeue(out request)) {
         await GenerateChunksFromCenterAsync(request);
     }
     await Task.Delay(50);
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What to do to make the chunk check other chunks? 1 Answer

Talking across too many objects causes big lagg? 2 Answers

Unloading Chunks not working 0 Answers

How to periodically pull output from an IJobParrallel or is there a better way to do this? 1 Answer

Infinite terrain generation algorithm problems 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges