When using compilation flag -warn-concurrency and -enable-actor-data-race-checks (possibly without? Not tried...)
actor ConnectorActor {
struct Config {}
private let config: Config
init(config: Config) {
self.config = config
}
func connect() async {
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
/* ... */
}
}
}
}
Non-sendable type '(inout ThrowingTaskGroup<Void, any Error>) async throws -> ()' exiting actor-isolated context in call to non-isolated global function 'withThrowingTaskGroup(of:returning:body:)' cannot cross actor boundary
With Xcode 14.1 Beta 2 (swiftlang-5.7.1.131.4 clang-1400.0.29.51), I see the same warning for reduce(into:) when I use it in MainActor as below.
Is this related and I should ignore it for now or this is legit?
I assume this counsel is just a temporary “this is how you can avoid this warning for now, until it is fixed”, and not intended as anything more than that. Is that correct?
actor MyActor {
func iterate<Sequence>(over sequence: Sequence) async rethrows where Sequence : AsyncSequence, Sequence.Element : Sendable {
// Sending '$ns$generator' risks causing data races
for try await ns in sequence {
print(ns)
}
}
}
I'm not sure this is a problem specifically with TaskGroup… I see a similar problem with an arbitrary AsyncSequence.
If I'm building from a new OS… I have this option from SE-0420:
actor MyActor {
func iterate<Sequence>(over sequence: Sequence) async rethrows where Sequence : AsyncSequence, Sequence.Element : Sendable {
var iterator = sequence.makeAsyncIterator()
while let ns = try await iterator.next(isolation: #isolation) {
print(ns)
}
}
}
This silences the error… but I still would like to support legacy OS versions (while building from the 6.0 toolchain).
@ktoso Would you know if we have any legit workarounds for that from Xcode_16_beta_2 that would also deploy back to the last OS releases (other than potentially not building from Swift 6 Strict Concurrency Checking)?
Heh, this made me realize that none of those APIs adopted #isolation and indeed will be getting such warnings... We'll have to re-evaluate more async sequence APIs and if they should take #isolation.
Thanks for the ping on it, I've added it a list of things to look into.