I am getting this error in a couple of places in my code with Task closure after setting Swift 6 as Language version in XCode.
Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure Below is minimally reproducible sample code.
import Foundation
final class Recorder {
var writer = Writer()
func startRecording() {
Task {
await writer.startRecording()
print("started recording")
}
}
func stopRecording() {
Task {
await writer.stopRecording()
print("stopped recording")
}
}
}
actor Writer {
var isRecording = false
func startRecording() {
isRecording = true
}
func stopRecording() {
isRecording = false
}
}
While making the class Recorder as Actor would fix the problem, I fear I will have to make too many classes as Actors in the class and in my scenario, there could be performance implications where real time audio and video frames are being processed. Further, I don't see any race condition in the code above. Does the error talk about possible race conditions that could arise if I were to call other functions on the self in future, or the current code itself is not safe?