Skip to content

Commit 5f9721c

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 aef70f0 commit 5f9721c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

stdlib/public/Concurrency/MainActor.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,29 @@ import Swift
4545
@available(SwiftStdlib 5.5, *)
4646
extension MainActor {
4747
/// Execute the given body closure on the main actor.
48-
@_silgen_name("$sScM3run10resultType4bodyxxm_xyYbKScMYcXEtYaKlFZ")
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+
@usableFromInline
53+
static func run<T>(
54+
resultType: T.Type = T.self,
55+
body: @MainActor @Sendable () throws -> T
56+
) async rethrows -> T {
57+
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) rethrows -> T {
58+
return try body()
59+
}
60+
61+
return try await runOnMain(body: body)
62+
}
63+
64+
/// Execute the given body closure on the main actor.
65+
@_alwaysEmitIntoClient
4966
public static func run<T: Sendable>(
5067
resultType: T.Type = T.self,
5168
body: @MainActor @Sendable () throws -> T
5269
) async rethrows -> T {
53-
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) async rethrows -> T {
70+
@MainActor func runOnMain(body: @MainActor @Sendable () throws -> T) rethrows -> T {
5471
return try body()
5572
}
5673

test/api-digester/stability-concurrency-abi.test

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,4 @@
5050
// UNSUPPORTED: swift_evolve
5151

5252
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)
53-
5453
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

0 commit comments

Comments
 (0)