Skip to content

[Concurrency] Downgrade reportUnexpectedExecutor to error on old SDKs by default #72610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion include/swift/Runtime/Bincompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -47,6 +47,17 @@ bool useLegacySwiftValueUnboxingInCasting();
/// if present.
bool useLegacySwiftObjCHashing();

/// Legacy semantics allowed for the `swift_task_reportUnexpectedExecutor` to
/// only log a warning. This changes in future releases and this function
/// will fatal error always.
///
/// Old behavior:
/// - logging a warning on concurrency violation is allowed
/// New behavior:
/// - always fatal error in `swift_task_reportUnexpectedExecutor`
SWIFT_RUNTIME_STDLIB_SPI
bool swift_bincompat_useLegacyWarningModeReportUnexpectedExecutor();

} // namespace bincompat

} // namespace runtime
Expand Down
11 changes: 10 additions & 1 deletion stdlib/public/Concurrency/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "swift/Concurrency/Actor.h"
#include "swift/Runtime/AccessibleFunction.h"
#include "swift/Runtime/Atomic.h"
#include "swift/Runtime/Bincompat.h"
#include "swift/Runtime/Casting.h"
#include "swift/Runtime/DispatchShims.h"
#include "swift/Threading/Mutex.h"
Expand Down Expand Up @@ -76,6 +77,7 @@ extern "C" void objc_autoreleasePoolPop(void *);
#endif

using namespace swift;
using namespace swift::runtime::bincompat;

/// Should we yield the thread?
static bool shouldYieldThread() {
Expand Down Expand Up @@ -353,7 +355,14 @@ static bool swift_task_isCurrentExecutorImpl(SerialExecutorRef executor) {
/// 0 - no logging
/// 1 - warn on each instance
/// 2 - fatal error
static unsigned unexpectedExecutorLogLevel = 2;
///
/// NOTE: The default behavior on Apple platforms depends on the SDK version
/// an application was linked to. Since Swift 6 the default is to crash,
/// and the logging behavior is no longer available.
static unsigned unexpectedExecutorLogLevel =
swift_bincompat_useLegacyWarningModeReportUnexpectedExecutor()
? 1 // legacy apps default to the logging mode, and cannot use `checkIsolated`
: 2; // new apps will only crash upon concurrency violations, and will call into `checkIsolated`

static void checkUnexpectedExecutorLogLevel(void *context) {
#if SWIFT_STDLIB_HAS_ENVIRON
Expand Down
12 changes: 12 additions & 0 deletions stdlib/public/runtime/Bincompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ bool useLegacySwiftObjCHashing() {
#endif
}

// FIXME(concurrency): Once the release is announced, adjust the logic detecting the SDKs
bool swift_bincompat_useLegacyWarningModeReportUnexpectedExecutor() {
#if BINARY_COMPATIBILITY_APPLE
return true; // For now, legacy behavior on Apple OSes
#elif SWIFT_TARGET_OS_DARWIN
return true; // For now, use legacy behavior on open-source builds for Apple platforms
#else
return false; // Always use the new behavior on non-Apple OSes
#endif
}


} // namespace bincompat

} // namespace runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ actor MyActor {
struct Runner {
static func main() async {
print("Launching a main-actor task")
// CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:25 was not called on the main thread
// CHECK: warning: data race detected: @MainActor function at main/data_race_detection_legacy_warning.swift:25 was not called on the main thread
launchFromMainThread()
sleep(1)

let actor = MyActor()
let actorFn = await actor.getTaskOnMyActor()
print("Launching an actor-instance task")
// CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:54 was not called on the same actor
// CHECK: warning: data race detected: actor-isolated function at main/data_race_detection_legacy_warning.swift:54 was not called on the same actor
launchTask(actorFn)

sleep(1)
Expand Down