Swift Concurrency crash in iOS 18 and 18.1 in withTaskCancellationHandler

We are seeing a swift concurrency related crash in iOS 18 and iOS 18.1 that has no direct link to any part of my code base in the stack trace. We are not able to reproduce locally but see it in the Organizer. The crash seems to come from withTaskCancellationHandler in Concurrency.swift

Incident Identifier: C5331198-3922-471F-8E39-57186BBB962B
Distributor ID:      com.apple.AppStore
Hardware Model:      iPhone16,2
Process:             MyApp [866]
Path:                /private/var/containers/Bundle/Application/B320C8CF-5711-4F14-92C4-0693420DDE07/MyApp.app/MyApp
Identifier:          com.MyApp.release
Version:             10.0.1 (1)
AppStoreTools:       16A242b
AppVariant:          1:iPhone16,2:18
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.MyApp.release [989]

Date/Time:           2024-09-21 06:30:38.3210 -0500
Launch Time:         2024-09-21 06:18:03.0691 -0500
OS Version:          iPhone OS 18.1 (22B5007p)
Release Type:        Beta
Baseband Version:    2.15.01
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000004
Exception Codes: 0x0000000000000001, 0x0000000000000004
VM Region Info: 0x4 is not in any region.  Bytes before following region: 4340908028
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102bd0000-102be0000 [   64K] r-x/r-x SM=COW  /var/containers/Bundle/Application/B320C8CF-5711-4F14-92C4-0693420DDE07/MyApp.app/MyApp
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [866]

Triggered by Thread:  3

Thread 3 Crashed:
0   MyApp                        	0x0000000103b00b8c withTaskCancellationHandler<A>(operation:onCancel:isolation:) + 108 (/<compiler-generated>:0)
1   MyApp                        	0x0000000103b0284d closure #1 in DataRequest.dataTask<A>(automaticallyCancelling:forResponse:) + 1 (Concurrency.swift:352)
2   MyApp                        	0x0000000102f66011 partial apply for closure #1 in closure #1 in variable initialization expression of static FireAndForgetKey.liveValue + 1
3   MyApp                        	0x0000000102f80841 closure #1 in DataTask.response.getter + 1
4   MyApp                        	0x0000000102f66011 partial apply for closure #1 in closure #1 in variable initialization expression of static FireAndForgetKey.liveValue + 1
5   libswift_Concurrency.dylib    	0x000000019164e689 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1 (Task.cpp:471)
Answered by danielnissenbaum in 805695022

Hmmm... Looked at alamofire and looks like someone has reported a similar issue. https://github.com/Alamofire/Alamofire/issues/3896

They seem to be saying that it's a bug in the swift runtime however: https://github.com/swiftlang/swift/issues/75952

Hopefully a fix is coming very soon from Apple, since it doesn't look like a workaround is available.

Please post a full Apple crash report, per the advice in Posting a Crash Report.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Full crash report below! Also filed FB15236099

Thanks in advance for the help!

This is one of those cases where I wish you didn’t have a third-party crash reporter in place (see thread 11) because you got the crash report from Apple anyway, so all it’s doing is undermining my confidence in the Apple report )-: [1]

Anyway, I’m just gonna assume that the crash report is valid.

has no direct link to any part of my code base in the stack trace.

That’s not right though, is it? Because looking at the backtrace in the crashing thread, I see that at least frames 1 and 3 are coming from a third-party library that you’ve included in your app (Alamofire). And frame 1 is particularly significant, because it’s the code that’s directly calling the crashing routine.

Have you ask your library vendor if their other clients are seeing this issue?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] For an explanation as to why, see Implementing Your Own Crash Reporter.

Hmmm... Looked at alamofire and looks like someone has reported a similar issue. https://github.com/Alamofire/Alamofire/issues/3896

They seem to be saying that it's a bug in the swift runtime however: https://github.com/swiftlang/swift/issues/75952

Hopefully a fix is coming very soon from Apple, since it doesn't look like a workaround is available.

Swift Concurrency crash in iOS 18 and 18.1 in withTaskCancellationHandler
 
 
Q