Skip to content

Commit 90781a8

Browse files
committed
[stdlib] Make expected actor isolation checking crash by default
Only affects projects compiled with `-enable-actor-data-race-checks` flag.
1 parent b98d7a5 commit 90781a8

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ static bool swift_task_isCurrentExecutorImpl(SerialExecutorRef executor) {
353353
/// 0 - no logging
354354
/// 1 - warn on each instance
355355
/// 2 - fatal error
356-
static unsigned unexpectedExecutorLogLevel = 1;
356+
static unsigned unexpectedExecutorLogLevel = 2;
357357

358358
static void checkUnexpectedExecutorLogLevel(void *context) {
359359
#if SWIFT_STDLIB_HAS_ENVIRON

test/Concurrency/Runtime/data_race_detection.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -enable-actor-data-race-checks %import-libdispatch -parse-as-library) > %t.log 2>&1
2-
// RUN: %FileCheck %s < %t.log
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %import-libdispatch -Xfrontend -disable-availability-checking -enable-actor-data-race-checks -parse-as-library -I %t %s -o %t/a.out -module-name main
3+
// RUN: %target-codesign %t/a.out
4+
// RUN: not --crash %target-run %t/a.out 2>&1 | %FileCheck %s
35

46
// REQUIRES: executable_test
57
// REQUIRES: concurrency
@@ -58,15 +60,10 @@ actor MyActor {
5860
@main
5961
struct Runner {
6062
static func main() async {
61-
print("Launching a main-actor task")
62-
// CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:23 was not called on the main thread
63-
launchFromMainThread()
64-
sleep(1)
65-
6663
let actor = MyActor()
6764
let actorFn = await actor.getTaskOnMyActor()
6865
print("Launching an actor-instance task")
69-
// CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:52 was not called on the same actor
66+
// CHECK: error: data race detected: actor-isolated function at main/data_race_detection.swift:57 was not called on the same actor
7067
launchTask(actorFn)
7168

7269
sleep(1)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %import-libdispatch -Xfrontend -disable-availability-checking -enable-actor-data-race-checks -parse-as-library -I %t %s -o %t/a.out -module-name main
3+
// RUN: %target-codesign %t/a.out
4+
// RUN: env SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL=1 %target-run %t/a.out 2>&1 | %FileCheck %s
5+
6+
// Make sure that without downgrade the program crashes by default
7+
// RUN: not --crash %target-run %t/a.out 2>&1
8+
9+
// REQUIRES: executable_test
10+
// REQUIRES: concurrency
11+
// REQUIRES: libdispatch
12+
13+
// rdar://76038845
14+
// REQUIRES: concurrency_runtime
15+
// UNSUPPORTED: back_deployment_runtime
16+
// UNSUPPORTED: single_threaded_concurrency
17+
18+
// Simulators don't pass `env` through to simulators even with `%env-`
19+
// REQUIRES: OS=macosx || OS=linux-gnu
20+
21+
import _Concurrency
22+
import Dispatch
23+
24+
// For sleep
25+
#if canImport(Darwin)
26+
import Darwin
27+
#elseif canImport(Glibc)
28+
import Glibc
29+
#endif
30+
31+
@MainActor func onMainActor() {
32+
print("I'm on the main actor!")
33+
}
34+
35+
func promiseMainThread(_ fn: @escaping @MainActor () -> Void) -> (() -> Void) {
36+
typealias Fn = () -> Void
37+
return unsafeBitCast(fn, to: Fn.self)
38+
}
39+
40+
func launchTask(_ fn: @escaping () -> Void) {
41+
if #available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 8.0, *) {
42+
DispatchQueue.global().async {
43+
fn()
44+
}
45+
}
46+
}
47+
48+
func launchFromMainThread() {
49+
launchTask(promiseMainThread(onMainActor))
50+
}
51+
52+
actor MyActor {
53+
var counter = 0
54+
55+
func onMyActor() {
56+
counter = counter + 1
57+
}
58+
59+
func getTaskOnMyActor() -> (() -> Void) {
60+
return {
61+
self.onMyActor()
62+
}
63+
}
64+
}
65+
66+
@main
67+
struct Runner {
68+
static func main() async {
69+
print("Launching a main-actor task")
70+
// CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:28 was not called on the main thread
71+
launchFromMainThread()
72+
sleep(1)
73+
74+
let actor = MyActor()
75+
let actorFn = await actor.getTaskOnMyActor()
76+
print("Launching an actor-instance task")
77+
// CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:57 was not called on the same actor
78+
launchTask(actorFn)
79+
80+
sleep(1)
81+
}
82+
}

0 commit comments

Comments
 (0)