Swift 6 concurrency error of passing sending closure

As @donny_wals said, the problem is the implicit capture of self. Since it happens implicitly (you don't write await self.writer...) it's admittedly less obvious.

A quicker fix than using a let constant and making Recorder Sendable (which might be hard in your actual code) is simply only capturing what you need with an explicit capture list, i.e. in this case write Task { [writer] in for your closure. Since Writer is an actor it can safely be passed to the new execution contexts for the closures.

Small thing that's perhaps only relevant to the example here: Of course should you switch the writer (as it's a var) in between calls to the start and stop methods, you might miss a call to stop (assuming they should also be paired).

2 Likes