1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -target %target-future-triple %import-libdispatch -enable-experimental-feature IsolatedDeinit -parse-stdlib -parse-as-library -module-name=main %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %env-SWIFT_IS_CURRENT_EXECUTOR_LEGACY_MODE_OVERRIDE=legacy %target-run %t/a.out | %FileCheck %s
// REQUIRES: libdispatch
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed
// REQUIRES: swift_feature_IsolatedDeinit
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// FIXME(distributed): Distributed actors currently have some issues on windows, isRemote always returns false. rdar://82593574
// UNSUPPORTED: OS=windows-msvc
import Swift
import _Concurrency
import Distributed
import Dispatch
#if canImport(Darwin)
import Darwin
typealias ThreadID = pthread_t
func getCurrentThreadID() -> ThreadID { pthread_self() }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { pthread_equal(a, b) != 0 }
#elseif canImport(Glibc)
import Glibc
typealias ThreadID = pthread_t
func getCurrentThreadID() -> ThreadID { pthread_self() }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { pthread_equal(a, b) != 0 }
#elseif os(Windows)
import WinSDK
typealias ThreadID = UInt32
func getCurrentThreadID() -> ThreadID { GetCurrentThreadId() }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { a == b }
#endif
var mainThread: ThreadID?
func isMainThread() -> Bool {
return equalThreadIDs(getCurrentThreadID(), mainThread!)
}
@_silgen_name("swift_task_isCurrentExecutor")
private func isCurrentExecutor(_ executor: Builtin.Executor) -> Bool
func getExecutor(_ a: AnyActor) -> Builtin.Executor {
let pack = (a, UnsafeRawPointer?.none)
return unsafeBitCast(pack, to: Builtin.Executor.self)
}
func isCurrent(_ a: AnyActor) -> Bool {
return isCurrentExecutor(getExecutor(a))
}
func isMainExecutor() -> Bool {
isCurrentExecutor(Builtin.buildMainActorExecutorRef())
}
actor A {}
distributed actor DA {
init(system: FakeActorSystem) {
self.actorSystem = system
}
}
distributed actor DA_userDefined {
init(system: FakeActorSystem) {
self.actorSystem = system
}
nonisolated deinit {}
}
distributed actor DA_userDefined_nonisolated {
init(system: FakeActorSystem) {
self.actorSystem = system
}
nonisolated deinit {
print("Deinitializing \(self.id) remote:\(__isRemoteActor(self)) isolated:\(isCurrent(self)) mainThread:\(isMainThread())")
}
}
distributed actor DA_userDefined_isolated {
init(system: FakeActorSystem) {
self.actorSystem = system
}
isolated deinit {
print("Deinitializing \(self.id) remote:\(__isRemoteActor(self)) isolated:\(isCurrent(self)) mainThread:\(isMainThread())")
}
}
distributed actor DA_state_nonisolated {
var name: String
var age: Int
init(name: String, age: Int, system: FakeActorSystem) {
self.name = name
self.age = age
self.actorSystem = system
}
nonisolated deinit {
print("Deinitializing \(self.id) name=\(name) age=\(age) remote:\(__isRemoteActor(self)) isolated:\(isCurrent(self)) mainThread:\(isMainThread())")
return
}
}
distributed actor DA_state_isolated {
var name: String
var age: Int
init(name: String, age: Int, system: FakeActorSystem) {
self.name = name
self.age = age
self.actorSystem = system
}
isolated deinit {
print("Deinitializing \(self.id) name=\(name) age=\(age) remote:\(__isRemoteActor(self)) isolated:\(isCurrent(self)) mainThread:\(isMainThread())")
return
}
}
@globalActor actor AnotherActor: GlobalActor {
static let shared = AnotherActor()
}
distributed actor DA_state_isolated_on_another {
let name: String
let age: Int
init(name: String, age: Int, system: FakeActorSystem) {
self.name = name
self.age = age
self.actorSystem = system
}
@AnotherActor
deinit {
print("Deinitializing \(self.id) name=\(name) age=\(age) remote:\(__isRemoteActor(self)) isolated-self:\(isCurrent(self)) isolated-other:\(isCurrent(AnotherActor.shared)) mainThread:\(isMainThread())")
return
}
}
// ==== Fake Transport ---------------------------------------------------------
struct ActorAddress: Sendable, Hashable, Codable {
let address: String
init(parse address : String) {
self.address = address
}
}
final class FakeActorSystem: @unchecked Sendable, DistributedActorSystem {
typealias ActorID = ActorAddress
typealias SerializationRequirement = Codable
typealias InvocationDecoder = FakeDistributedInvocationEncoder
typealias InvocationEncoder = FakeDistributedInvocationEncoder
typealias ResultHandler = FakeResultHandler
var n = 0
let group: DispatchGroup
init(group: DispatchGroup) {
self.group = group
}
deinit {
print("Deinit ActorSystem: mainExecutor=\(isMainExecutor()) mainThread=\(isMainThread())")
group.leave()
}
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
where Act: DistributedActor,
Act.ID == ActorID {
print("resolve type:\(actorType), address:\(id)")
return nil
}
func assignID<Act>(_ actorType: Act.Type) -> ActorID
where Act: DistributedActor,
Act.ID == ActorID {
n += 1
let address = ActorAddress(parse: "addr-\(n)")
print("assign type:\(actorType), address:\(address)")
return address
}
func actorReady<Act>(_ actor: Act)
where Act: DistributedActor,
Act.ID == ActorID {
print("ready actor:\(actor), address:\(actor.id)")
}
func resignID(_ id: ActorID) {
print("resign address:\(id)")
}
@inlinable func makeInvocationEncoder() -> InvocationEncoder {
.init()
}
func remoteCall<Act, Err, Res>(
on actor: Act,
target: RemoteCallTarget,
invocation invocationEncoder: inout InvocationEncoder,
throwing: Err.Type,
returning: Res.Type
) async throws -> Res
where Act: DistributedActor,
Act.ID == ActorID,
Err: Error,
Res: SerializationRequirement {
fatalError("not implemented: \(#function)")
}
func remoteCallVoid<Act, Err>(
on actor: Act,
target: RemoteCallTarget,
invocation invocationEncoder: inout InvocationEncoder,
throwing: Err.Type
) async throws
where Act: DistributedActor,
Act.ID == ActorID,
Err: Error {
fatalError("not implemented: \(#function)")
}
}
class FakeDistributedInvocationEncoder: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
typealias SerializationRequirement = Codable
func recordGenericSubstitution<T>(_ type: T.Type) throws { }
func recordArgument<Value: SerializationRequirement>(_ argument: RemoteCallArgument<Value>) throws { }
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws { }
func recordErrorType<E: Error>(_ type: E.Type) throws { }
func doneRecording() throws { }
// === Receiving / decoding -------------------------------------------------
func decodeGenericSubstitutions() throws -> [Any.Type] {
[]
}
func decodeNextArgument<Argument: SerializationRequirement>() throws -> Argument {
fatalError()
}
func decodeReturnType() throws -> Any.Type? {
nil
}
func decodeErrorType() throws -> Any.Type? {
nil
}
}
@available(SwiftStdlib 5.5, *)
public struct FakeResultHandler: DistributedTargetInvocationResultHandler {
public typealias SerializationRequirement = Codable
public func onReturn<Success: SerializationRequirement>(value: Success) async throws {
fatalError("Not implemented: \(#function)")
}
public func onReturnVoid() async throws {
fatalError("Not implemented: \(#function)")
}
public func onThrow<Err: Error>(error: Err) async throws {
fatalError("Not implemented: \(#function)")
}
}
typealias DefaultDistributedActorSystem = FakeActorSystem
// ==== Execute ----------------------------------------------------------------
func test() {
let group = DispatchGroup()
var dummy: AnyObject?
func check(factory: () -> AnyObject) {
dummy = factory()
// Test requires actor system to be released before the actor,
// so that release of the actor system from actor deinit is the last one
group.enter()
dummy = nil
group.wait()
}
// no lifecycle things make sense for a normal actor, double check we didn't emit them
print("before A")
_ = A()
print("after A")
// CHECK: before A
// CHECK: after A
check {
DA(system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA, address:ActorAddress(address: "[[ADDR1:addr-[0-9]]]")
// CHECK: resign address:ActorAddress(address: "[[ADDR1]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
check {
DA_userDefined(system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA_userDefined, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA_userDefined, address:ActorAddress(address: "[[ADDR2:addr-[0-9]]]")
// CHECK: resign address:ActorAddress(address: "[[ADDR2]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
// resign must happen as the _last thing_ after user-deinit completed
check {
DA_userDefined_nonisolated(system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA_userDefined_nonisolated, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA_userDefined_nonisolated, address:ActorAddress(address: "[[ADDR3:addr-[0-9]]]")
// CHECK: Deinitializing ActorAddress(address: "[[ADDR3]]") remote:false isolated:false mainThread:true
// CHECK-NEXT: resign address:ActorAddress(address: "[[ADDR3]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
// resign must happen as the _last thing_ after user-deinit completed
check {
DA_userDefined_isolated(system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA_userDefined_isolated, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA_userDefined_isolated, address:ActorAddress(address: "[[ADDR4:addr-[0-9]]]")
// CHECK: Deinitializing ActorAddress(address: "[[ADDR4]]") remote:false isolated:true mainThread:true
// CHECK-NEXT: resign address:ActorAddress(address: "[[ADDR4]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=false mainThread=true
// resign must happen as the _last thing_ after user-deinit completed
check {
DA_state_nonisolated(name: "Foo", age:37, system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA_state_nonisolated, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA_state_nonisolated, address:ActorAddress(address: "[[ADDR5:addr-[0-9]]]")
// CHECK: Deinitializing ActorAddress(address: "[[ADDR5]]") name=Foo age=37 remote:false isolated:false mainThread:true
// CHECK-NEXT: resign address:ActorAddress(address: "[[ADDR5]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
// resign must happen as the _last thing_ after user-deinit completed
check {
DA_state_isolated(name: "Bar", age:42, system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA_state_isolated, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA_state_isolated, address:ActorAddress(address: "[[ADDR6:addr-[0-9]]]")
// CHECK: Deinitializing ActorAddress(address: "[[ADDR6]]") name=Bar age=42 remote:false isolated:true mainThread:true
// CHECK-NEXT: resign address:ActorAddress(address: "[[ADDR6]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=false mainThread=true
// resign must happen as the _last thing_ after user-deinit completed
check {
DA_state_isolated_on_another(name: "Baz", age:57, system: DefaultDistributedActorSystem(group: group))
}
// CHECK: assign type:DA_state_isolated_on_another, address:[[ADDRESS:.*]]
// CHECK: ready actor:main.DA_state_isolated_on_another, address:ActorAddress(address: "[[ADDR6:addr-[0-9]]]")
// CHECK: Deinitializing ActorAddress(address: "[[ADDR6]]") name=Baz age=57 remote:false isolated-self:false isolated-other:true mainThread:false
// CHECK-NEXT: resign address:ActorAddress(address: "[[ADDR6]]")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=false mainThread=false
// a remote actor should not resign it's address, it was never "assigned" it
check {
let address = ActorAddress(parse: "remote-1")
return try! DA_userDefined_nonisolated.resolve(id: address, using: DefaultDistributedActorSystem(group: group))
}
// CHECK-NEXT: resolve type:DA_userDefined_nonisolated, address:ActorAddress(address: "remote-1")
// MUST NOT run deinit body for a remote distributed actor
// CHECK-NOT: Deinitializing ActorAddress(address: "remote-1")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
// a remote actor should not resign it's address, it was never "assigned" it
check {
let address = ActorAddress(parse: "remote-2")
return try! DA_userDefined_isolated.resolve(id: address, using: DefaultDistributedActorSystem(group: group))
}
// CHECK-NEXT: resolve type:DA_userDefined_isolated, address:ActorAddress(address: "remote-2")
// MUST NOT run deinit body for a remote distributed actor
// CHECK-NOT: Deinitializing ActorAddress(address: "remote-2")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
// a remote actor should not resign it's address, it was never "assigned" it
check {
let address = ActorAddress(parse: "remote-3")
return try! DA_state_isolated_on_another.resolve(id: address, using: DefaultDistributedActorSystem(group: group))
}
// CHECK-NEXT: resolve type:DA_state_isolated_on_another, address:ActorAddress(address: "remote-3")
// MUST NOT run deinit body for a remote distributed actor
// CHECK-NOT: Deinitializing ActorAddress(address: "remote-3")
// CHECK-NEXT: Deinit ActorSystem: mainExecutor=true mainThread=true
print("DONE")
// CHECK-NEXT: DONE
}
@main struct Main {
static func main() async {
mainThread = getCurrentThreadID()
test()
}
}
|