# TaskGroup-Related Crash

**URL:** <https://forums.swift.org/t/taskgroup-related-crash/56425>\
**Category:** Using Swift\
**Tags:** concurrency, crash, task\
**Created:** [March 31, 2022, 10:36pm UTC](https://forums.swift.org/t/taskgroup-related-crash/56425 "2022-03-31T22:36:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![danhalliday](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danhalliday/32/18264_2.png) [@danhalliday](https://forums.swift.org/u/danhalliday)\
**Post date:** [March 31, 2022, 10:36pm UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/1 "2022-03-31T22:36:42Z")

</div>

I’m seeing a crash relating to `TaskGroup`, but unfortunately it’s only coming up in production and I have limited information on it (via Crashlytics). I’m guessing I’m abusing `TaskGroup` one way or another. Any advice as to where to start investigating an issue like this? Here’s the offending thread. I’ve tried at length to reproduce it locally but no luck.

```swift
Crashed: com.apple.root.user-initiated-qos.cooperative
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000040

Crashed: com.apple.root.user-initiated-qos.cooperative
0 libswift_Concurrency.dylib 0x31440 swift::TaskGroup::offer(swift::AsyncTask*, swift::AsyncContext*) + 504
1 libswift_Concurrency.dylib 0x2dc5c swift::AsyncTask::completeFuture(swift::AsyncContext*) + 132
2 libswift_Concurrency.dylib 0x2f4d0 completeTaskAndRelease(swift::AsyncContext*, swift::SwiftError*) + 128
3 libswift_Concurrency.dylib 0x2b0f0 swift::runJobInEstablishedExecutorContext(swift::Job*) + 132
4 libswift_Concurrency.dylib 0x2b9f8 swift_job_runImpl(swift::Job*, swift::ExecutorRef) + 72
5 libdispatch.dylib 0x1343c _dispatch_root_queue_drain + 340
6 libdispatch.dylib 0x13c38 _dispatch_worker_thread2 + 172
7 libsystem_pthread.dylib 0x4e48 _pthread_wqthread + 224
8 libsystem_pthread.dylib 0x49f0 start_wqthread + 8

```

I have only one use of `TaskGroup` in my project, and it seems pretty trivial.

```swift
// Function that processes a bunch of 'action triggers' and produces a stream of actions
func run(action: Action, find: @escaping (Model.Type) -> Model?) -> AsyncStream<Action> {
    AsyncStream { continuation in
        Task {
            await withTaskGroup(of: Void.self) { group in
                for trigger in triggers {
                    group.addTask {
                        for await result in trigger(action, find) {
                            if result is VoidAction { continue }
                            continuation.yield(result)
                        }
                    }
                }

                await group.waitForAll()
                continuation.finish()
            }
        }
    }
}

// Later, from an async context. Process an action and dispatch its output.
Task {
    for await output in run(action: action, find: { store.find($0) }) {
        try await store.dispatch(output)
    }
}

```

Is there any way to dig into this without a more complete crash report?

---

<div class="post-metadata">

**Author:** ![Ray\_Fix](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ray_fix/32/1479_2.png) [@Ray\_Fix](https://forums.swift.org/u/Ray_Fix)\
**Post date:** [April 2, 2022, 7:06am UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/2 "2022-04-02T07:06:23Z")

</div>

Thanks for sharing your code. Hopefully someone more knowledgable than myself can reply. I was hitting a similar (but not the same) problem where my `store.dispatch` contained some Core Data related code that was not following proper managed object thread containment rules. (It mostly worked, except for sometimes. 😬 😅)

A random thought I had. Is it okay for the different async child tasks to use the `continuation` variable concurrently? IOW, is `continuation.yield` re-enterant?

---

<div class="post-metadata">

**Author:** ![ZfHxFr](https://avatars.discourse-cdn.com/v4/letter/z/b19c9b/32.png) [@ZfHxFr](https://forums.swift.org/u/ZfHxFr)\
**Post date:** [April 8, 2022, 4:35am UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/3 "2022-04-08T04:35:12Z")

</div>

On which iOS version? I met similar crashes.  
But after upgrading my device to latest iOS version,  
the bug seems to be fixed.

---

<div class="post-metadata">

**Author:** ![danhalliday](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/danhalliday/32/18264_2.png) [@danhalliday](https://forums.swift.org/u/danhalliday)\
**Post date:** [April 9, 2022, 8:47pm UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/4 "2022-04-09T20:47:56Z")

</div>

I eventually narrowed this down to iOS 15.3, and older (iPhone 6s, 7, 8, X) devices. It seems to be fixed on iOS 15.4. I also fixed it by marking the two inner closures (withTaskGroup, addTask) @MainActor.

---

<div class="post-metadata">

**Author:** ![antonvmironov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/antonvmironov/32/22862_2.png) [@antonvmironov](https://forums.swift.org/u/antonvmironov)\
**Post date:** [April 14, 2022, 3:02pm UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/5 "2022-04-14T15:02:08Z")

</div>

I've also seen [this crash](https://bugs.swift.org/browse/SR-15963). I can confirm it is reproducible on versions prior to 15.4 which is a blocker for adopting structured concurrency at my company.  
I had hope Swift 5.6 would fix this issue. It seemingly did but [a library responsible for the crash](https://forums.swift.org/t/new-xcode-old-libswift-concurrency-dylib/56754) was for some reason not updated in Xcode 13.3 toolchain.

---

<div class="post-metadata">

**Author:** ![hyouuu](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hyouuu/32/7208_2.png) [@hyouuu](https://forums.swift.org/u/hyouuu)\
**Post date:** [June 19, 2022, 7:21pm UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/6 "2022-06-19T19:21:52Z")

</div>

I got this failure on iOS 15.5 - could someone help check if it's the same error?

Crashed: com.apple.root.user-initiated-qos.cooperative  
0 libswiftCore.dylib 0x39b74 _assertionFailure(_:\_:file:line:flags:) + 308  
1 libswift\_Concurrency.dylib 0x5ed8 CheckedContinuation.resume(returning:) + 560  
2 [AppName] 0x1fe0dc (1) await resume partial function for closure #2 in closure #2 in closure #1 in closure #1 in MsgRepo.fetchMsgs(storyId:updatedAfter:) + 4341768412 (:4341768412)  
3 libswift\_Concurrency.dylib 0x3b7cc swift::runJobInEstablishedExecutorContext(swift::Job\*) + 244  
4 libswift\_Concurrency.dylib 0x3c1e8 swift\_job\_runImpl(swift::Job\*, swift::ExecutorRef) + 72  
5 libdispatch.dylib 0x15164 \_dispatch\_root\_queue\_drain + 396  
6 libdispatch.dylib 0x1596c \_dispatch\_worker\_thread2 + 164  
7 libsystem\_pthread.dylib 0x1080 \_pthread\_wqthread + 228  
8 libsystem\_pthread.dylib 0xe5c start\_wqthread + 8

---

<div class="post-metadata">

**Author:** ![LilaQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lilaq/32/24963_2.png) [@LilaQ](https://forums.swift.org/u/LilaQ)\
**Post date:** [August 26, 2022, 4:04am UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/7 "2022-08-26T04:04:21Z")

</div>

@hyouuu Did you find a solution to this? I'm facing a very similar issue right now, and don't know what's triggering it.

---

<div class="post-metadata">

**Author:** ![hyouuu](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hyouuu/32/7208_2.png) [@hyouuu](https://forums.swift.org/u/hyouuu)\
**Post date:** [August 26, 2022, 6:53am UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/8 "2022-08-26T06:53:00Z")

</div>

Unfortunately no I haven't found the cause

---

<div class="post-metadata">

**Author:** ![lukeh](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukeh/32/31648_2.png) [@lukeh](https://forums.swift.org/u/lukeh)\
**Post date:** [January 18, 2024, 7:34am UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/9 "2024-01-18T07:34:04Z")

</div>

Something similar here.

```swift
💣 Program crashed: Bus error at 0x0005000200020062

Thread 0 crashed:

0 0x0000ffff8367aadc swift_task_cancel + 28 in libswift_Concurrency.so
1 0x0000ffff8367b508 swift_taskGroup_cancelAll + 47 in libswift_Concurrency.so
2 InfernoDevice.shutdown() + 551 in infernod at /build/inferno/control/InfernoAES70/Sources/InfernoDevice/Device.swift:490:27

   488│ 
   489│ if let ocp1TaskGroup {
   490│ ocp1TaskGroup.cancelAll()                                                                                                                                                  
      │ ▲
   491│ }
   492│

```

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [January 18, 2024, 7:35am UTC](https://forums.swift.org/t/taskgroup-related-crash/56425/10 "2024-01-18T07:35:14Z")

</div>

Please file a bug with details about versions and best if able to provide a reproducer. It’s not so easy to follow up on such without context code snippet.
