Skip to content
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

MacOS 15 / Xcode 16: asyncWrite chokes at compile time #8687

Open
mmcnamara-oliveriq opened this issue Sep 18, 2024 · 4 comments
Open

MacOS 15 / Xcode 16: asyncWrite chokes at compile time #8687

mmcnamara-oliveriq opened this issue Sep 18, 2024 · 4 comments

Comments

@mmcnamara-oliveriq
Copy link

How frequently does the bug occur?

Always

Description

The use of asyncWrite makes Xcode 16 unhappy upon compilation. This seems to be the case whether using Swift 5 or 6. Simple example:

func asyncDeleteAllDevices(config: Realm.Configuration) async throws {
        let realm = try await Realm(configuration: config)
        
        try await realm.asyncWrite {
            let allDevices = realm.objects(Device.self)
            realm.delete(allDevices)
        }
}

Presumably it's some fallout from the new concurrency checks. Swap to .write and no problems compiling.

Stacktrace & log output

1.	Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2)
2.	Compiling with effective version 5.10
3.	Contents of /Users/xx/Library/Developer/Xcode/DerivedData/App-btqvywndbmyjobbzmpfmjayfzxvr/Build/Intermediates.noindex/App.build/Debug-iphonesimulator/App (iOS) Development.build/Objects-normal/arm64/sources-221:
---
... .swift
---
4.	While evaluating request TypeCheckSourceFileRequest(source_file "/Users/xxx/Development/App/Shared/Foo.swift")
5.	While evaluating request TypeCheckFunctionBodyRequest(App.(file).Foo extension.fooFunc(_:config:)@/Users/xxx/Development/App/Shared/Foo.swift:845:10)
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x00000001060030fc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106001350 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x00000001060036c8 SignalHandler(int) + 292
3  libsystem_platform.dylib 0x000000018b754184 _sigtramp + 56
4  swift-frontend           0x0000000101d14acc (anonymous namespace)::ActorIsolationChecker::checkApply(swift::ApplyExpr*) + 2032
5  swift-frontend           0x0000000101d14acc (anonymous namespace)::ActorIsolationChecker::checkApply(swift::ApplyExpr*) + 2032
6  swift-frontend           0x0000000101d11544 (anonymous namespace)::ActorIsolationChecker::walkToExprPre(swift::Expr*) + 1872
7  swift-frontend           0x0000000102107560 (anonymous namespace)::Traversal::visitDynamicMemberRefExpr(swift::DynamicMemberRefExpr*) + 48
8  swift-frontend           0x0000000102106edc (anonymous namespace)::Traversal::visit(swift::Expr*) + 308
9  swift-frontend           0x0000000102107584 (anonymous namespace)::Traversal::visitDynamicMemberRefExpr(swift::DynamicMemberRefExpr*) + 84
10 swift-frontend           0x0000000102106f18 (anonymous namespace)::Traversal::visit(swift::Expr*) + 368
11 swift-frontend           0x000000010210a678 (anonymous namespace)::Traversal::visit(swift::Stmt*) + 2500
12 swift-frontend           0x0000000101d057f0 swift::checkFunctionActorIsolation(swift::AbstractFunctionDecl*) + 544
13 swift-frontend           0x0000000101e1614c swift::TypeCheckFunctionBodyRequest::evaluate(swift::Evaluator&, swift::AbstractFunctionDecl*) const + 2268
14 swift-frontend           0x0000000102226798 swift::TypeCheckFunctionBodyRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()>(swift::TypeCheckFunctionBodyRequest const&, swift::TypeCheckFunctionBodyRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckFunctionBodyRequest>(swift::Evaluator&, swift::TypeCheckFunctionBodyRequest, swift::TypeCheckFunctionBodyRequest::OutputType)::'lambda'()) + 636
15 swift-frontend           0x0000000102195308 swift::AbstractFunctionDecl::getTypecheckedBody() const + 160
16 swift-frontend           0x0000000101e63e2c swift::TypeCheckSourceFileRequest::evaluate(swift::Evaluator&, swift::SourceFile*) const + 868
17 swift-frontend           0x0000000101e6b3c0 swift::TypeCheckSourceFileRequest::OutputType swift::Evaluator::getResultUncached<swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()>(swift::TypeCheckSourceFileRequest const&, swift::TypeCheckSourceFileRequest::OutputType swift::evaluateOrDefault<swift::TypeCheckSourceFileRequest>(swift::Evaluator&, swift::TypeCheckSourceFileRequest, swift::TypeCheckSourceFileRequest::OutputType)::'lambda'()) + 620
18 swift-frontend           0x0000000101e63aac swift::performTypeChecking(swift::SourceFile&) + 328
19 swift-frontend           0x0000000100d82ce0 swift::CompilerInstance::performSema() + 260
20 swift-frontend           0x00000001009b0190 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1528
21 swift-frontend           0x00000001009aef58 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3572
22 swift-frontend           0x000000010093601c swift::mainEntry(int, char const**) + 3680
23 dyld                     0x000000018b39c274 start + 2840

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

20.0.0

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

iOS 17 / 18

Build environment

ProductName:		macOS
ProductVersion:		15.0
BuildVersion:		24A335

/Applications/Xcode.app/Contents/Developer
Xcode 16.0
Build version 16A242d

pod not found
(not in use here)

/bin/bash
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin24)

carthage not found
(not in use here)

/usr/bin/git
git version 2.39.5 (Apple Git-154)
Copy link

sync-by-unito bot commented Sep 18, 2024

➤ PM Bot commented:

Jira ticket: RCOCOA-2434

@mmcnamara-oliveriq
Copy link
Author

Naive workaround that circumvents the compile problem:

extension Realm {
    func asyncWrite<T>(_ block: @escaping (Realm) throws -> T) async throws -> T {
        return try await withCheckedThrowingContinuation { continuation in
            DispatchQueue(label: "xx.yy.zz").async {
                do {
                    let realm = try Realm(configuration: self.configuration)
                    try realm.write {
                        let result = try block(realm)
                        continuation.resume(returning: result)
                    }
                } catch {
                    continuation.resume(throwing: error)
                }
            }
        }
    }
}

@nikolay-mdev
Copy link

I had the same problem and found another solution. I created a DatabaseActor and passed it to the asyncWrite method.

@globalActor actor DatabaseActor: GlobalActor {
    static var shared = DatabaseActor()
}

open class BaseDAO<Model : Object> {
    
    // - Realm
    internal var realm: Realm!
    
    init() async throws {
        realm = try await Realm(actor: DatabaseActor.shared)
    }
    
    // - Save
    func save(_ array: [Model]) async throws {
        try await self.realm.asyncWrite(_isolation: DatabaseActor.shared) { [weak self] in
            self?.realm.add(array, update: .all)
        }
    }
    
}

@AnthonyWharton
Copy link

Thank you @nikolay-mdev, this fixed it for me. Looking into it a little more it looks like this is present when using v6 or greater of the Swift compiler. It seems asyncWrite has two type signatures/versions, depending on compiler if you look at the source code.

You can check your swift version in a terminal with: xcrun swift -version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants