Swift 6 error: "Sending '$....$generator' risks causing data races"

I'm in the process of migrating a large project to Swift 6. So far, I've completed all of the "Upcoming Features" warnings, except for "Region Based Isolation."

When I turn that on, I get the warning:

Sending '$image$generator' risks causing data races; this is 
an error in the Swift 6 language mode

in multiple places in my codebase where I use withTaskGroup.

I've taken one example, and trimmed it down to a small snippet of code (it uses a custom NSItemProvider method), which will reliably produce this warning at the for await image in group line.

The warning appears only when the function test is annotated with @MainActor.

If I drop this snippet into a brand-new project, it compiles without warning, so it's possible there's something in my larger project that's causing this issue.

Is there something different I should be doing here to avoid this warning?

@MainActor
func test(results: [PHPickerResult]) {
  Task {
    await withTaskGroup(of: UIImage?.self) { group in
      for result in results {
        group.addTask {
          return try? await result.itemProvider.loadImage()
        }
      }
      
      var allImages: [UIImage] = []
      for await image in group { // warning on this line
        if let image {
          allImages.append(image)
        }
      }
      
      return allImages
    }
  }
}
1 Like

I was seeing similar warnings a couple of months ago but they look fixed to me as of Xcode_16_beta_2.

Thank you! This looks like exactly my problem, but I still see it in Xcode 16 beta 2.

It also looks like it might be a known issue on task groups (not just a known issue on any arbitrary async sequence).