I have an use-case for actor which matches following snippet:
actor SomeActor {
let executor = DispatchSerialQueue(label: "serial")
var counter = 0
nonisolated var unownedExecutor: UnownedSerialExecutor { executor.asUnownedSerialExecutor() }
}
func anyActorIsolatedFunc(_ act: isolated (any Actor)? = #isolation) {
Task {
if let act = act as? SomeActor {
dispatchPrecondition(condition: .onQueue(act.executor)) // success
}
print("any actor start executed")
}
if let act = act as? SomeActor {
dispatchPrecondition(condition: .onQueue(act.executor))
Task {
dispatchPrecondition(condition: .onQueue(act.executor)) // error
print("any actor executed")
}
}
Task {
if let act = act as? SomeActor {
dispatchPrecondition(condition: .onQueue(act.executor)) // success
}
print("any actor end executed")
}
}
When trying to use anyActorIsolatedFunc:
await anyActorIsolatedFunc(act)
try await Task.sleep(nanoseconds: 1_000_000_000)
I am getting error in the Task created from the if block. From my understanding, the default task initializer always inherits current actor context and starts executing on the current actor’s executor. Is this a bug?