I think the issue here is that you are calling RunLoop.main.run(). The main function calls dispatchMain() under the hood. I suspect that it is causing the issue. I can't reproduce the issue when removing RunLoop.main.run(). If you want to wait for the task to print "Hello" you can just await it, i.e. by storing the task and awaiting its value
Thanks for the answer. But on Windows, disaptchMain () doesn't work at all, and the executable exits after the dispatchMain () function is called.
In your example, I have another call stack on access violation
import Foundation
func test() async -> String {
return "Test"
}
@main
struct App {
static func main() async {
let t = Task {
print("Hello")
}
let str = await test()
print(str)
//RunLoop.main.run()
await t.value
}
}
C:\Users\gungravekoga\CLionProjects\untitled\.build\debug>swift --version
compnerd.org Swift version 5.5.2 (swift-5.5.2-RELEASE)
Target: x86_64-unknown-windows-msvc
Building via SPM C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin\swift.exe build --skip-update --product untitled --build-path C:\Users\gungravekoga\CLionProjects\untitled\.build
//Package.swift
import PackageDescription
let package = Package(
name: "untitled",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.executableTarget(
name: "untitled",
dependencies: [],
swiftSettings: [
.unsafeFlags([
"-g",
"-debug-info-format=codeview"
])
]),
.testTarget(
name: "untitledTests",
dependencies: ["untitled"]),
]
)
Interesting; I wonder if this is something that was fixed later. I would recommend that you use nightly snapshots or build swift from source if you are trying to work with async as there has been a bunch of stability work that has gone into main. It is unlikely that we would get this resolved in a 5.5 update, but hopefully 5.6 should have the fix for this.
The following code works most of the time with Swift 5.5 on Windows, but every five times or so it gets stuck before printing anything:
import Foundation
@main
struct AsyncTest {
static func main() async throws {
let myActor = await MyActor(name: "hello")
await myActor.say()
exit(0)
}
}
actor MyActor {
let name: String
init(name: String) async {
self.name = name
}
func say() async {
print(name)
}
}
A more complicated code that I have using actors and async/await practically never works on Windows (the outcome until it does not work anymore seems to be random).
I really hope that my problem has the same source (probably is has), and that it gets resolved soon. Could the snapshot from December 23, 2021 December 6, 2021 already resolve it? (I'll try out.)
UPDATE: Yes it works with 5.6-dev (LLVM 542eceb110d8480, Swift 4323d2fa26a6bf8) (snapshot from December 6, 2021), both my little example and my bigger project mentioned. Very glad to see the Windows version of Swift proceeding in a good direction! Thanks a lot. SPM seems also to work very fine.
Thanks a lot for reporting and looping back to the thread with the update that 5.6-dev is working well, @sspringer. Got me worried there for a moment, glad that it seems to be fixed. Happy holidays