Skip to content

Commit a643fe4

Browse files
committed
Run async main code on main thread
This patch moves the actual running of the main code onto the main thread by making it part of the main actor. This should be more consistent with what folks are expecting.
1 parent 03e0c4a commit a643fe4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ public func runAsyncAndBlock(_ asyncFun: @escaping () async -> ())
601601
public func _asyncMainDrainQueue() -> Never
602602

603603
public func _runAsyncMain(_ asyncFun: @escaping () async throws -> ()) {
604+
#if os(Windows)
604605
Task.runDetached {
605606
do {
606607
try await asyncFun()
@@ -609,6 +610,21 @@ public func _runAsyncMain(_ asyncFun: @escaping () async throws -> ()) {
609610
_errorInMain(error)
610611
}
611612
}
613+
#else
614+
@MainActor @concurrent
615+
func _doMain(_ asyncFun: @escaping () async throws -> ()) async {
616+
do {
617+
try await asyncFun()
618+
} catch {
619+
_errorInMain(error)
620+
}
621+
}
622+
623+
Task.runDetached {
624+
await _doMain(asyncFun)
625+
exit(0)
626+
}
627+
#endif
612628
_asyncMainDrainQueue()
613629
}
614630

0 commit comments

Comments
 (0)