Here is my code:
` // A 3rd-party class I must use. class MySession{ init() async throws { // .. } }
actor SessionManager{
private var mySession: MySession? // The MySession is not Sendable
func createSession() async {
do {
mySession = try await MySession()
log("getOrCreateSession() End, success.")
} catch {
log("getOrCreateSession() End, failure.")
}
}
}`
I get this warning: "Non-sendable type 'MySession' returned by implicitly asynchronous call to a nonisolated function cannot cross the actor boundary."
How can this be fixed?
Hello @OleksiyPavlenko,
This issue is well-described in this current Swift Evolution proposal: https://github.com/hborla/swift-evolution/blob/async-function-isolation/proposals/NNNN-async-function-isolation.md#alternatives-considered
An important piece from that document for your case, "It's easy to write invalid async APIs. If the performAsync method were in a library that the programmer doesn't own, it's not possible to workaround the data-race safety error without using unsafe opt outs."
In other words, the only way for you to work around your particular issue is to use an unsafe opt out, for example, by marking MySession to be @unchecked Sendable, however, it is important to note that, depending on the implementation of MySession, it may be totally invalid to do that.
You may also be interested in this active pitch thread on the Swift Forums: https://forums.swift.org/t/pitch-inherit-isolation-by-default-for-async-functions/74862
Best regards,
Greg