Async let calls crashes libswift_Concurrency in background thread with EXC_BAD_ACCESS

I am getting an occasion crash when using several async let in a swiftUI task. The crash is sporadic and I'm really struggling to pin down the main cause. I am unable to repeatedly reproduce it though I have observed it happening locally. I am seeing the crash logs come in via Organizer.

The code and crash logs are shown below. Really appreciate any advice on where to start pinning down this issue. Thank you!

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000050
Exception Codes: 0x0000000000000001, 0x0000000000000050
VM Region Info: 0x50 is not in any region.  Bytes before following region: 4330717104
      REGION TYPE                 START - END      [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      UNUSED SPACE AT START
--->  
      __TEXT                   102218000-102224000 [   48K] r-x/r-x SM=COW  ...ea.app/****
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 11 Segmentation fault: 11
Terminating Process: exc handler [75970]

Triggered by Thread:  12


Thread 12 name:
Thread 12 Crashed:
0   libswift_Concurrency.dylib    	0x000000022746e8e4 swift_task_create_commonImpl(unsigned long, swift::TaskOptionRecord*, swift::TargetMetadata<swift::InProcess> const*, void (swift::AsyncContext* swift_async_context) swiftasynccall*, void*, unsigne... + 448 (MetadataValues.h:189)
1   libswift_Concurrency.dylib    	0x000000022746e83c swift_task_create_commonImpl(unsigned long, swift::TaskOptionRecord*, swift::TargetMetadata<swift::InProcess> const*, void (swift::AsyncContext* swift_async_context) swiftasynccall*, void*, unsigne... + 280 (Task.cpp:507)
2   libswift_Concurrency.dylib    	0x000000022746c614 swift_asyncLet_begin + 40 (AsyncLet.cpp:188)
3   *******                        	0x00000001025a4764 CommonClient.preload(id:) + 344 (Preloaders.swift:15)
4   *******                        	0x00000001023b0639 closure #3 in View.body.getter + 1 (View.swift:72)
5   *******                        	0x00000001023b0859 partial apply for closure #3 in View.body.getter + 1 (<compiler-generated>:0)
6   SwiftUI                       	0x00000001ae6e0b01 $s7SwiftUI13_TaskModifierV05InnerD033_293A0AF83C78DECE53AFAAF3EDCBA9D4LLV4body7contentQrAA05_ViewD8_ContentVyAFG_tFyycfU_yyYaYbcfU_TQ0_ + 1 (TaskModifier.swift:197)
7   SwiftUI                       	0x00000001ae6e82f5 $s7SwiftUI13_TaskModifierV05InnerD033_293A0AF83C78DECE53AFAAF3EDCBA9D4LLV4body7contentQrAA05_ViewD8_ContentVyAFG_tFyycfU_yyYaYbcfU_TATQ0_ + 1 (<compiler-generated>:0)
8   SwiftUI                       	0x00000001ae6e3d0d $sxIeghHr_xs5Error_pIegHrzo_s8SendableRzs5NeverORs_r0_lTRyt_Tg5TQ0_ + 1 (<compiler-generated>:0)
9   SwiftUI                       	0x00000001ae6e7b91 $sxIeghHr_xs5Error_pIegHrzo_s8SendableRzs5NeverORs_r0_lTRyt_Tg5TATQ0_ + 1 (<compiler-generated>:0)
10  libswift_Concurrency.dylib    	0x0000000227470081 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1 (Task.cpp:399)

Here is the slightly redacted crashing code:

/// SwiftUI Task modifier that makes async call
public var body: some View {
    VStack {}
    .task {
                await client.preload(id: id)
        }
}

// Async preloading function that uses async let to cache the result of 3 network calls
    func preload(id: String) async {
        print("PRELOADING:", id)
        async let res1: CachedResponse<Response1> = request(id)
        async let res2 CachedResponse<Response2> = request(id)
        async let res3: CachedResponse<Response3> = request(id)

        let _ = try? await res1
        let _ = try? await res2
        let _ = try? await res3
    }

I am unable to repeatedly reproduce it

Hmmm, tricky. Please post a full 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 log below. Hopefully reveals some insight I have missed. Thank you!

It looks like you have a third-party crash reporter in play (see thread 7) which makes it hard to trust this crash report [1]. My best guess is that you’re hitting some sort of memory corruption issue:

  • Looking at crashes like this internally, they were mostly resolved after fixing memory corruption issues.

  • Thread 8 is actively in the process of allocating memory when you crashed (in thread 6, at least according to the crash report :-).

However, that’s really just a guess. My specific advice is that you run your app’s test suite with the standard memory debugging tools to see if that make the crash more reproducible.

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.

Async let calls crashes libswift_Concurrency in background thread with EXC_BAD_ACCESS
 
 
Q