Skip to content

Commit f307d20

Browse files
committed
Ensure that we hop off the main actor after MainActor.run.
Asynchronous functions isolated to global actors hop to the global at the beginning of the function but do not hop back on return. For `MainActor.run`, this means that we would not "hop back" off the main actor after executing the closure, which lead to too much code running on the main thread. Dropping the "async" ensures that we hop back. While we my also want the general "hop back" semantics for asynchronous actor-isolated functions, for now this addresses the problem with `MainActor.run`. Fixes rdar://82138050.
1 parent def59df commit f307d20

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

stdlib/public/Concurrency/MainActor.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,25 @@ import Swift
4545
@available(SwiftStdlib 5.5, *)
4646
extension MainActor {
4747
/// Execute the given body closure on the main actor.
48-
public static func run<T>(
48+
///
49+
/// Historical ABI entry point, superceded by the Sendable version that is
50+
/// also inlined to back-deploy a semantic fix where this operation would
51+
/// not hop back at the end.
52+
@_silgen_name("$sScM3run10resultType4bodyxxm_xyYbKScMYcXEtYaKlFZ")
53+
@usableFromInline
54+
static func _historicalRunForABI<T>(
4955
resultType: T.Type = T.self,
5056
body: @MainActor @Sendable () throws -> T
5157
) async rethrows -> T {
52-
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) async rethrows -> T {
53-
return try body()
54-
}
58+
return try await body()
59+
}
5560

56-
return try await runOnMain(body: body)
61+
/// Execute the given body closure on the main actor.
62+
@_alwaysEmitIntoClient
63+
public static func run<T>(
64+
resultType: T.Type = T.self,
65+
body: @MainActor @Sendable () throws -> T
66+
) async rethrows -> T {
67+
return try await body()
5768
}
5869
}

0 commit comments

Comments
 (0)