Open ABridoux opened 3 weeks ago
here's a slightly reduced reproduction that can be run as a command line tool:
@main
struct S {
static func main() async {
await SharedActor.shared.test()
}
actor SharedActor {
static let shared = SharedActor()
deinit {
print("DEINIT")
}
func test() {
Task { [unowned self] in
_ = self
}
}
}
}
facts of note:
self
must actually be used in the Task closure or the issue appears to go away[unowned self]
also appears needed. aliasing self
in various ways and capturing the alias seems to prevent the issue from arising.
Description
I have been using Xcode 16 for a few weeks now and noticed a behavior regarding
actor
deinitialization that surprises me.In our app, we use a few actors that are stored as constant static properties and thus live as long as the app is not killed. Compiling with Xcode 16, we have noticed that capturing the actor as
unowned
lead to a crash in several cases, like aTask
initialization (even though for aTask
, capturingself
asunowned
is unneeded most of the time).Reproduction
To exemplify this, here's a block of code for a command-line tool.
Here's what is printed in the console.
Expected behavior
The
deinit
block should not be called, as theactor
is stored as a static constant property and thus should live as long as the application or command-line tool lives.Environment
swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) Target: arm64-apple-macosx15.0
Additional information
The behavior differs when a
class
is used instead of anactor
, which lead me to think this might be a bug. The issue is mentioned in the Swift Forums.