Tasks do not complete in the order they were started #613
Replies: 1 comment
-
|
This is expected behavior in the current UniTask implementation and not an issue in your code. Why completion order can differAll three Because of this, when one item completes, the last element in the array is moved into its place. This can change the iteration order of the remaining active items within the same frame, which may result in completion logs appearing as This behavior has been acknowledged in discussions such as #453, and the general design choice is that UniTask does not guarantee FIFO ordering of completion callbacks for parallelly running tasks. Related discussions like #701 also mention potential FIFO-preserving approaches, but the current implementation prioritizes performance and allocation-free updates. About ordering guaranteesUniTask does not guarantee that tasks started earlier will complete earlier when they are running in parallel, even if they have identical delay durations. This is especially true for About adding a toggleA toggle for FIFO-preserving removal is unlikely to be introduced, since it would add branching and an alternative execution path for a behavior that is intentionally not guaranteed today. If strict ordering is required, it should be handled at the application level. Recommended approachesIf strict sequential order is required (common in puzzle/game visual flows), the simplest solution is to await sequentially: async UniTaskVoid Start()
{
for (var i = 0; i < 3; i++)
await UpdateAsync(i);
}If work needs to run in parallel but results must be processed in order, you can separate execution from presentation by collecting tasks and then consuming them in sequence: var tasks = new UniTask[3];
for (int i = 0; i < 3; i++)
tasks[i] = UpdateAsync(i);
await UniTask.WhenAll(tasks);Then apply any ordered presentation logic after completion if needed. Summary
Hope this clarifies the behavior. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
We are using UniTask to run visualizations in our puzzle game and have observed an issue when running multiple tasks with equal duration. They do not complete in the order they started. For example, this code
Would start tasks 0-1-2 but complete 0-2-1.
This behavior is caused by the way UniTask manages player loop items in PlayerLoopRunner. When an item is completed, it's removed from the item array by swapping, which alters the order in which the remaining items are updated.
We would prefer that the item would be removed by copying, which would retain the order of the other items.
Would you consider making this behavior toggleable by a setting in UniTask?
Or is there another way to accomplish the same result?
Beta Was this translation helpful? Give feedback.
All reactions