swift icon indicating copy to clipboard operation
swift copied to clipboard

withCheckedContinuation crashes on Xcode 16 RC

Open MarkVillacampa opened this issue 1 year ago • 10 comments

Description

I can reproduce on Xcode 16 RC and Xcode 16.1 beta, but ONLY when running the app as "Designed for iPhone/iPad" on macOS Sonoma, and ONLY in debug mode (crash goes away in release mode).

I cannot reproduce by running on iOS 17 or 18 neither device nor simulator, and cannot reproduce either when running on macOS Sequoia beta.

Some developers have reported the issue also reproduces when installing the app via TestFlight on an iOS 18 device.

Original issue: https://github.com/RevenueCat/purchases-ios/issues/4177

FB14855530

Reproduction

_ = try await withCheckedContinuation { continuation in
    continuation.resume(returning: true)
}

Stack dump

Thread 0 Crashed:
0   asdfsadf.debug.dylib          	       0x102551354 withCheckedContinuation<A>(isolation:function:_:) + 232
1   asdfsadf.debug.dylib          	       0x102551135 closure #1 in closure #2 in ContentView.body.getter + 1 (ContentView.swift:21)
2   asdfsadf.debug.dylib          	       0x102552299 partial apply for closure #1 in closure #2 in ContentView.body.getter + 1
3   libswift_Concurrency.dylib    	       0x2548aa0f9 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1

Expected behavior

It doesnt crash

Environment

Xcode 16.1 beta 1

Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1) Target: arm64-apple-macosx14.0

Additional information

No response

MarkVillacampa avatar Aug 19 '24 13:08 MarkVillacampa

Quick update:

  • Crash is still reproducible in Xcode 16 beta 6.
  • The crash happens with withCheckedThrowingContinuation as well:
_ = try await withCheckedThrowingContinuation { continuation in
    continuation.resume(returning: true)
}

MarkVillacampa avatar Aug 21 '24 07:08 MarkVillacampa

Same issue for me.

I'm on Xcode 16.1 beta.

App crashes like @MarkVillacampa described with only iOS 18 simulators.

Thread 1: EXC_BAD_ACCESS (code=1, address=0x4)

mszyszylo avatar Sep 03 '24 13:09 mszyszylo

The crash is still present in Xcode 16 RC when running a Debug build as "Designed for iPad/iPhone" on macOS Sonoma

MarkVillacampa avatar Sep 09 '24 20:09 MarkVillacampa

Has anyone posted a question on the Apple Developer Forum, and is there a final solution to the problem?

random-yang avatar Sep 10 '24 12:09 random-yang

+1. Any update on this?

kiran-nayak-cheq avatar Sep 12 '24 05:09 kiran-nayak-cheq

@kiran-nayak-cheq Looks like the issue has been fixed in the updated version? https://developer.apple.com/forums/thread/761580 https://forums.developer.apple.com/forums/thread/762682?page=1#803104022

random-yang avatar Sep 13 '24 05:09 random-yang

We're seeing this for our publicly released apps using Xcode version 16.0 (16A242d) (published, final updates to App Store, not debug versions) where random users are seeing persistent crashes at launch and the stack traces are vague. We see the following in each of them though:

9   libswift_Concurrency.dylib    	       0x26737e8a5 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1

We are personally unable to reproduce this in any environment (debug / release) or on any OS. The users experiencing this range from being on macOS 11 to macOS 15, and iOS 16 / 18. It could potentially be affecting all operating system versions but we are unable to say this with certainty. We are using Xcode 16 to compile our apps using Swift v5 (5.10, not 6). The last update posted from Xcode 15 does not exhibit this issue.

guidedways avatar Sep 17 '24 04:09 guidedways

We're seeing something similar in Alamofire, but in withTaskCancellationHandler that wraps withCheckedContinuation. Switching to unsafe continuations or the deprecated vision of withTaskCancellationHandler had no effect.

jshier avatar Sep 21 '24 07:09 jshier

We saw similar things on our app but we were able to identify it only happening on iOS 18/macOS 15 betas that were before the RC builds for each platform. We've found 3 cases of users where this was fixed by updating to the actual RC builds for each platform.

We did not see this happening before iOS 18/macOS 15.

On iOS the crashes looked like SIGSEV 4 with the following line as the top stack. On macOS they looked like the signal in this report (maybe because it's on the simulator it matches the macOS crash signature)

‘‘‘ @_swiftmacro_7Copilot0022Controllersswift_tiAIefMX24_33_9isolationfMf.swift - Line 1 ‘‘‘

I was not able to find this generated file anywhere.

sergiocampama avatar Sep 21 '24 12:09 sergiocampama

This is a known issue documented in the Xcode 16 release notes (search for 134793410). It was addressed on the release/6.0.1 branch here: [6.0.1] SILOptimizer: Allow inlining of transparent functions in @backDeployed thunks by tshortli · Pull Request #76218 · swiftlang/swift · GitHub. Designed for iPhone/iPad binaries built with debug optimizations and deployed to macOS Sonoma or earlier are affected.

tshortli avatar Sep 21 '24 16:09 tshortli

I'm facing the same Thread 1: EXC_BAD_ACCESS (code=1, address=0x4) issue with Xcode Version 16.0 and iOS 18.0, but it only occurs on specific devices and isn't consistently reproducible. this crash seems to depend on the specific device or scenario. It would be great if this could be investigated further to understand the underlying cause and ensure stability.

I'm using Swift 5 with Xcode 16. Below is a sample code to demonstrate the issue:

import Alamofire
import Foundation

struct MyModel: Decodable {
    let id: Int
    let name: String
}

func fetchData() async -> Result<MyModel, Error> {
    return await AF.request("https://api.example.com/data")
        .response(MyModel.self)
}

extension DataRequest {
    @MainActor
    func response<T: Decodable>(_ t: T.Type) async -> Result<T, Error> {
        return await withCheckedContinuation { continuation in // Fails on here `EXC_BAD_ACCESS error` 
            responseData(queue: .main) { response in
                switch response.result {
                case .success(let data):
                    do {
                        let decoder = JSONDecoder()
                        let obj = try decoder.decode(t.self, from: data)
                        continuation.resume(returning: .success(obj))
                    } catch {
                        continuation.resume(returning: .failure(error))
                    }
                case .failure(let error):
                    continuation.resume(returning: .failure(error))
                }
            }
        }
    }
}

The crash occurs when navigating between screens while the API call is still in progress, which leads to an EXC_BAD_ACCESS error. This only seems to happen on specific devices running iOS 18.0, and it's not consistently reproducible.

abdorizak avatar Oct 10 '24 08:10 abdorizak

I'm facing the same Thread 1: EXC_BAD_ACCESS (code=1, address=0x4) issue with Xcode Version 16.0 and iOS 18.0, but it only occurs on specific devices and isn't consistently reproducible. this crash seems to depend on the specific device or scenario. It would be great if this could be investigated further to understand the underlying cause and ensure stability.

I'm using Swift 5 with Xcode 16. Below is a sample code to demonstrate the issue:


import Alamofire

import Foundation



struct MyModel: Decodable {

    let id: Int

    let name: String

}



func fetchData() async -> Result<MyModel, Error> {

    return await AF.request("https://api.example.com/data")

        .response(MyModel.self)

}



extension DataRequest {

    @MainActor

    func response<T: Decodable>(_ t: T.Type) async -> Result<T, Error> {

        return await withCheckedContinuation { continuation in // Fails on here `EXC_BAD_ACCESS error` 

            responseData(queue: .main) { response in

                switch response.result {

                case .success(let data):

                    do {

                        let decoder = JSONDecoder()

                        let obj = try decoder.decode(t.self, from: data)

                        continuation.resume(returning: .success(obj))

                    } catch {

                        continuation.resume(returning: .failure(error))

                    }

                case .failure(let error):

                    continuation.resume(returning: .failure(error))

                }

            }

        }

    }

}

The crash occurs when navigating between screens while the API call is still in progress, which leads to an EXC_BAD_ACCESS error. This only seems to happen on specific devices running iOS 18.0, and it's not consistently reproducible.

Check with these users, they are most likely on an older beta version of iOS 18. I can confirm all our users were on an older beta version and updating to the latest iOS version fixed these crashes for them.

guidedways avatar Oct 10 '24 09:10 guidedways

This was addressed in Swift 6.0.1.

DougGregor avatar Nov 08 '24 19:11 DougGregor

There is a similar issue that affects older iOS 18 betas. Those devices should really be updated to the iOS 18 release (or newer), but if you need an app-side workaround, see https://forums.swift.org/t/global-with-concurrency-functions-crash-on-catalyst-designed-for-ipad-on-macos/74823/13.

DougGregor avatar Nov 11 '24 15:11 DougGregor

This was addressed in Swift 6.0.1.

hi! in our project we are still using swift5 image

does this mean that the fix will not be applied while targeting swift5?

thanks in advance

mike123789-dev avatar Nov 15 '24 02:11 mike123789-dev

No, this is a common source of confusion but the setting you have screenshot is the Swift "language mode". The language mode is independent of the version of the Swift compiler you are building with. Swift 6.0.1 refers to the version of the compiler.

tshortli avatar Nov 15 '24 02:11 tshortli

@tshortli i was worried that we will have to migrate a whole codebase to swift 6.

thanks for the reply

mike123789-dev avatar Nov 15 '24 04:11 mike123789-dev

hello. I am still experiencing the same issue. Is anyone else having the same problem as me? I deployed the app in the following environment

Environment

Xcode-16.1(16B40) swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) Target: arm64-apple-macosx14.0

I have not been able to reproduce the crash in apps built on my local Mac, but it seems to happen in apps uploaded to the app store.

Lyine0924 avatar Nov 20 '24 05:11 Lyine0924

I have just updated to: MacOS: 15.1.1 (24B91) Xcode: Version 16.1 (16B40) swift --version produces: swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) IOS-18.0 sims crash. IOS-18.1 and 17.2 simulators do not crash

This is specifically when building for development.

For what its worth a coworker building the same application with these same versions does not cause the same crash.

Edit, This is for Rosetta Simulators.

wingsofdoug avatar Dec 03 '24 20:12 wingsofdoug

Hello, I've been struggling with the same problem recently. Could you please let me know the iOS version where the problem occurred?

The problem only occurs in apps distributed through the App Store.

Issue iOS version is 18.0.0

Environment

xcode 16.1(16B40) swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) Target: arm64-apple-macosx15.0(24A335)

HoonHaChoi avatar Dec 26 '24 09:12 HoonHaChoi

@HoonHaChoi you should ask the users of your app to upgrade their iOS version to the latest. They're most likely (read: for sure) on one of the earlier beta versions with the issue. This is no longer an issue with 18 RC or above.

guidedways avatar Dec 26 '24 09:12 guidedways

A crash previously occurred related to this issue.
However, with the following combination, the crash does not occur.
Has the issue been fixed?

  • macOS Sequoria 15.3.1
  • Xcode 16.3 Beta
  • Simulator iPhone 16 Pro + OS 18.4

ios-kkr avatar Feb 24 '25 00:02 ios-kkr