[Concurrency/Distributed] nonisolated-nonsending by default breaks distributed thunks

the new NonisolatedNonsendingByDefault upcoming feature breaks remote
calls in distributed actors, because the expected isolation doesn't
match and the runtime swift_distributed_execute_target_resume will
crash.

This is a short term fix to unblock adopters, however preferably we
should mark the thunks as nonisolated(nonsending), though that seems to
be more involved.

resolves rdar://159247975
This commit is contained in:
Konrad Malawski
2025-08-27 18:42:43 +09:00
parent cd0a1513ad
commit 4c283e5959
6 changed files with 95 additions and 2 deletions

View File

@@ -3004,6 +3004,13 @@ public:
/// `distributed var get { }` accessors.
bool isDistributedGetAccessor() const;
/// Is this a 'distributed thunk'?
///
/// Distributed thunks are synthesized functions which perform the "is remote?"
/// check, before dispatching to a 'system.remoteCall' (if actor was remote).
/// They are always 'async' and 'throws'.
bool isDistributedThunk() const;
bool hasName() const { return bool(Name); }
bool isOperator() const { return Name.isOperator(); }

View File

@@ -1369,6 +1369,13 @@ bool ValueDecl::isDistributedGetAccessor() const {
return false;
}
bool ValueDecl::isDistributedThunk() const {
if (auto func = dyn_cast<AbstractFunctionDecl>(this)) {
return func->isDistributedThunk();
}
return false;
}
ConstructorDecl *
NominalTypeDecl::getDistributedRemoteCallTargetInitFunction() const {
auto mutableThis = const_cast<NominalTypeDecl *>(this);

View File

@@ -737,6 +737,7 @@ static FuncDecl *createSameSignatureDistributedThunkDecl(DeclContext *DC,
thunk->setSynthesized(true);
thunk->setDistributedThunk(true);
// TODO(distributed): These would benefit from becoming nonisolated(nonsending)
thunk->getAttrs().add(NonisolatedAttr::createImplicit(C));
return thunk;

View File

@@ -5231,6 +5231,13 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
if (decl->getASTContext().LangOpts.hasFeature(
Feature::NonisolatedNonsendingByDefault)) {
if (auto *value = dyn_cast<ValueDecl>(decl)) {
// TODO(distributed): make distributed thunks nonisolated(nonsending) and remove this if
if (value->isAsync() && value->isDistributedThunk()) {
// don't change isolation of distributed thunks until we make them nonisolated(nonsending),
// since the runtime calling them assumes they're just nonisolated right now.
return ActorIsolation::forNonisolated(nonisolatedAttr->isUnsafe());
}
if (value->isAsync() &&
value->getModuleContext() == decl->getASTContext().MainModule) {
return ActorIsolation::forCallerIsolationInheriting();

View File

@@ -4,8 +4,6 @@
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --enable-var-scope
// X: %target-run-simple-swift( -Xfrontend -module-name=main -target %target-swift-5.7-abi-triple -parse-as-library -Xfrontend -I -Xfrontend %t ) | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed

View File

@@ -0,0 +1,73 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule \
// RUN: -module-name FakeDistributedActorSystems -target %target-swift-5.7-abi-triple \
// RUN: %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-build-swift -module-name main -enable-upcoming-feature NonisolatedNonsendingByDefault \
// RUN: -target %target-swift-5.7-abi-triple -j2 -parse-as-library -I %t %s \
// RUN: %S/../Inputs/FakeDistributedActorSystems.swift -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --enable-var-scope
// REQUIRES: swift_feature_NonisolatedNonsendingByDefault
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed
// rdar://76038845
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
// FIXME(distributed): Distributed actors currently have some issues on windows, isRemote always returns false. rdar://82593574
// UNSUPPORTED: OS=windows-msvc
import Distributed
import FakeDistributedActorSystems
typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem
distributed actor Greeter: CustomStringConvertible {
distributed func echo(name: String) -> String {
return "Echo: \(name) (impl on: \(self.id))"
}
distributed func error() throws -> String {
throw SomeError()
}
nonisolated var description: String {
"\(Self.self)(\(id))"
}
}
struct SomeError: Error {}
// ==== Test -------------------------------------------------------------------
func test() async throws {
let system = DefaultDistributedActorSystem()
let local = Greeter(actorSystem: system)
let ref = try Greeter.resolve(id: local.id, using: system)
let reply = try await ref.echo(name: "Caplin")
// CHECK: > encode argument name:name, value: Caplin
// CHECK-NOT: > encode error type
// CHECK: > encode return type: Swift.String
// CHECK: > done recording
// CHECK: >> remoteCall
// CHECK: > decode return type: Swift.String
// CHECK: > decode argument: Caplin
// CHECK: << onReturn: Echo: Caplin (impl on: ActorAddress(address: "<unique-id>"))
print("got: \(reply)")
// CHECK: got: Echo: Caplin (impl on: ActorAddress(address: "<unique-id>"))
}
@main struct Main {
static func main() async {
try! await test()
}
}