Sending parameter concurrency error

How to fix this in Swift 6 mode?

import AVFoundation

actor A: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
    func foo(_ sampleBuffer: CMSampleBuffer) {
        // ...
    }
    
    nonisolated func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
        Task {
            await foo(sampleBuffer)
            // ❌ Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure
        }
    }
}

@unchecked Sendable will do the trick, either declare conformance on CMSampleBuffer directly or create a newtype:

extension CMSampleBuffer: @retroactive @unchecked Sendable {}

// or

struct UnsafeSendable<T>: @unchecked Sendable {
	let value: T
}

P.S. You could improve UnsafeSendable with ~Copyable conformance, I think :thinking:

1 Like