Sending '$0' risks causing data races

I had no luck to compile a sample code provided by apple with Xcode 16.0 beta 5.

ScreenCaptureKit demo (https://developer.apple.com/documentation/screencapturekit/capturing_screen_content_in_macos)

The part it is failling is,

streamOutput.capturedFrameHandler = { continuation.yield($0) }

And the error message is

Sending '$0' risks causing data races
Task-isolated '$0' is passed as a 'sending' parameter; Uses in callee may race with later task-isolated uses

Please enlighten me why this is an issue and how to avoid? Thanks in advance!

Answered by Engineer in 799859022

@Kyung, please use Feedback Assistant to submit a bug report regarding this issue. As @Claude31 pointed out, the sample fails to compile because the CapturedFrame struct declared in line 14 of CaptureEngine.swift does not conform to the Sendable protocol. You can work around the issue in the meantime by having CapturedFrame conform to @unchecked Sendable, for example:

/// A structure that contains the video data to render.
struct CapturedFrame: @unchecked Sendable

I cannot find this line of code in the referenced link…

But it could have to deal with $0 not being sendable. Read here explanations in a different context: https://forums.swift.org/t/cannot-understand-the-nature-of-data-race-warnings-in-swift-5-10/70472

Accepted Answer

@Kyung, please use Feedback Assistant to submit a bug report regarding this issue. As @Claude31 pointed out, the sample fails to compile because the CapturedFrame struct declared in line 14 of CaptureEngine.swift does not conform to the Sendable protocol. You can work around the issue in the meantime by having CapturedFrame conform to @unchecked Sendable, for example:

/// A structure that contains the video data to render.
struct CapturedFrame: @unchecked Sendable
Sending '$0' risks causing data races
 
 
Q