Tasks shown "alive" in Instruments display even after they should have finished.

In my TestApp I run the following code, to calculate every pixel of a bitmap concurrently:

private func generate() async {
    for x in 0 ..< bitmap.width{
        for y in 0 ..< bitmap.height{
            let result = await Task.detached(priority:.userInitiated){
               return iterate(x,y)
            }.value
            displayResult(result)
        }
    }
}

This works and does not give any warnings or runtime issues.

After watching the WWDC talk "Visualize and optimize Swift concurrency" I used instruments to visualize the Tasks:

The number of active tasks continuously raises until 2740 and stays constant at this value even after all 64000 pixels have been calculated and displayed.

What am I doing wrong?

Replies

Do you have any new insights on this problem? I'm seeing the same behaviour with URLSession tasks that keep accumulating in instruments.

Unfortunately not, ... but yesterday I heard someone talk about retain cycles in SwiftUI and closures and now that you ask, this may actually be the problem here: Task's closure retains some values. Try using some weak captures could be the solution.