BGTaskScheduler crashes on iOS 18.4

I've been seeing a high number of BGTaskScheduler related crashes, all of them coming from iOS 18.4. I've encountered this myself once on launch upon installing my app, but haven't been able to reproduce it since, even after doing multiple relaunches and reinstalls. Crash report attached at the bottom of this post.

I am not even able to symbolicate the reports despite having the archive on my MacBook:

Does anyone know if this is an iOS 18.4 bug or am I doing something wrong when scheduling the task? Below is my code for scheduling the background task on the view that appears when my app launches:

.onChange(of: scenePhase) { newPhase in
    if newPhase == .active {
        #if !os(macOS)
        let request = BGAppRefreshTaskRequest(identifier: "notifications")
        request.earliestBeginDate = Calendar.current.date(byAdding: .hour, value: 3, to: Date())
        do {
            try BGTaskScheduler.shared.submit(request)
            Logger.notifications.log("Background task scheduled. Earliest begin date: \(request.earliestBeginDate?.description ?? "nil", privacy: .public)")
        } catch let error {
            // print("Scheduling Error \(error.localizedDescription)")
            Logger.notifications.error("Error scheduling background task: \(error.localizedDescription, privacy: .public)")
        }
        #endif
...
}

Incident Identifier: E1E01637-01CB-4F44-8C38-55B8721C0DAA
Distributor ID:      com.apple.TestFlight
Hardware Model:      iPhone14,4
Process:             SubManager [678]
Path:                /private/var/containers/Bundle/Application/0D87594A-A61A-4E74-9ED8-EA9E238C3E1D/SubManager.app/SubManager
Identifier:          com.ip18.SubManager
Version:             1.4.8 (321)
AppStoreTools:       16C7009
AppVariant:          1:iPhone14,4:18
Beta:                YES
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.ip18.SubManager [716]

Date/Time:           2025-02-23 19:53:50.2294 +0000
Launch Time:         2025-02-23 19:53:50.0825 +0000
OS Version:          iPhone OS 18.4 (22E5200s)
Release Type:        Beta
Baseband Version:    4.51.00
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: SIGNAL 6 Abort trap: 6
Terminating Process: SubManager [678]

Triggered by Thread:  8

Last Exception Backtrace:
0   CoreFoundation                	0x19737c364 0x197262000 + 1155940
1   libobjc.A.dylib               	0x194846d0c 0x194814000 + 208140
2   Foundation                    	0x19668e990 0x195ef8000 + 7956880
3   BackgroundTasks               	0x2301856ec -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:] + 448 (BGTaskScheduler.m:0)
4   BackgroundTasks               	0x2301854ec -[BGTaskScheduler registerForTaskWithIdentifier:usingQueue:launchHandler:] + 128 (BGTaskScheduler.m:209)
5   SwiftUI                       	0x19be5fb4c -[BGTaskSchedulerProxy registerForTaskWithIdentifier:launchHandler:] + 220 (BackgroundTask_SPI.m:40)
6   SwiftUI                       	0x19c790028 closure #1 in AppRefreshBackgroundTask.register() + 344 (BackgroundTask.swift:408)
7   SwiftUI                       	0x19bab3e15  + 1
8   SwiftUI                       	0x19bc24239  + 1
9   SwiftUI                       	0x19ba94829  + 1
10  libswift_Concurrency.dylib    	0x1a2dc3cc9 0x1a2d5f000 + 412873

Thread 0:
0   libsystem_platform.dylib      	0x00000002213db284 0x2213d9000 + 8836
1   libswiftCore.dylib            	0x0000000195d08bc8 0x195940000 + 3967944
2   libswiftCore.dylib            	0x0000000195d11938 0x195940000 + 4004152
3   libswiftCore.dylib            	0x0000000195d08ca8 0x195940000 + 3968168
4   libswiftCore.dylib            	0x0000000195d18880 0x195940000 + 4032640
5   libswiftCore.dylib            	0x0000000195d151c4 0x195940000 + 4018628
6   libswiftCore.dylib            	0x0000000195d10b24 0x195940000 + 4000548
7   libswiftCore.dylib            	0x0000000195d10718 0x195940000 + 3999512
8   libswiftCore.dylib            	0x0000000195d11180 0x195940000 + 4002176
9   libswiftCore.dylib            	0x0000000195d0a638 0x195940000 + 3974712
10  libswiftCore.dylib            	0x0000000195cf319c 0x195940000 + 3879324
11  libswiftCore.dylib            	0x0000000195cf133c 0x195940000 + 3871548
12  SwiftUICore                   	0x0000000256d99f64 0x256d57000 + 274276
13  libswiftCore.dylib            	0x0000000195cf6cec 0x195940000 + 3894508
14  libswiftCore.dylib            	0x0000000195ce3f70 0x195940000 + 3817328
15  SwiftUICore                   	0x0000000256d70e5c 0x256d57000 + 106076
16  SwiftUICore                   	0x0000000256d5f800 0x256d57000 + 34816
17  SwiftUICore                   	0x0000000256e432c4 0x256d57000 + 967364
18  SwiftUICore                   	0x0000000256e42ea8 0x256d57000 + 966312
19  SwiftUI                       	0x000000019bdc0690 static ViewAlias._makeViewList(view:inputs:) + 244 (ViewAlias.swift:44)
20  SwiftUI                       	0x000000019bdc2df4 protocol witness for static View._makeViewList(view:inputs:) in conformance TabViewStyleConfiguration<a>.Content + 88 (:0)
21  SwiftUICore                   	0x0000000256dde224 0x256d57000 + 553508
22  SwiftUICore                   	0x0000000256dc0ad4 0x256d57000 + 432852
23  SwiftUICore                   	0x0000000256e005b0 0x256d57000 + 693680
24  SwiftUICore                   	0x0000000256e03fd8 0x256d57000 + 708568
25  SwiftUICore                   	0x0000000256d89b54 0x256d57000 + 207700
26  SwiftUICore                   	0x0000000256d89780 0x256d57000 + 206720
27  SwiftUICore                   	0x0000000256d85f1c 0x256d57000 + 192284
28  SwiftUICore                   	0x0000000256d85cc8 0x256d57000 + 191688
29  SwiftUICore                   	0x0000000256d85c0c 0x256d57000 + 191500
30  SwiftUICore                   	0x0000000256dc0028 0x256d57000 + 430120
31  SwiftUICore                   	0x0000000256d70b0c 0x256d57000 + 105228
32  SwiftUICore                   	0x0000000256d86628 0x256d57000 + 194088
33  SwiftUICore                   	0x0000000256d88cbc 0x256d57000 + 203964
34  SwiftUICore                   	0x0000000256d88c38 0x256d57000 + 203832
35  SwiftUICore                   	0x0000000256d88b40 0x256d57000 + 203584
36  SwiftUICore                   	0x0000000256d71fa0 0x256d57000 + 110496
37  SwiftUICore                   	0x0000000256dbfe78 0x256d57000 + 429688
38  SwiftUICore                   	0x0000000256dc0528 0x256d57000 + 431400
39  SwiftUICore                   	0x0000000256dbf514 0x256d57000 + 427284
40  SwiftUICore                   	0x0000000256dbefd0 0x256d57000 + 425936
41  SwiftUICore                   	0x0000000256dbed20 0x256d57000 + 425248
42  SwiftUICore                   	0x0000000256dbecdc 0x256d57000 + 425180
43  SwiftUICore                   	0x0000000256dbe264 0x256d57000 + 422500
44  SwiftUICore                   	0x0000000256dbdf28 0x256d57000 + 421672
45  SwiftUICore                   	0x0000000256dbdb40 0x256d57000 + 420672
46  SwiftUICore                   	0x0000000256dbd5f0 0x256d57000 + 419312
47  SwiftUICore                   	0x0000000256dbce08 0x256d57000 + 417288
48  SwiftUICore                   	0x0000000256dbca94 0x256d57000 + 416404
49  SwiftUICore                   	0x0000000256d936d8 0x256d57000 + 247512
50  SwiftUICore                   	0x0000000256d93568 0x256d57000 + 247144
51  SwiftUICore                   	0x0000000256d9184c 0x256d57000 + 239692
52  SwiftUICore                   	0x0000000256d92344 0x256d57000 + 242500
53  SwiftUICore                   	0x0000000256d91c10 0x256d57000 + 240656
54  SwiftUICore                   	0x0000000256dd3900 0x256d57000 + 510208
55  SwiftUICore                   	0x0000000256dd37f4 0x256d57000 + 509940
56  SwiftUICore                   	0x0000000256dd375c 0x256d57000 + 509788
57  SwiftUICore                   	0x0000000256dd33b8 0x256d57000 + 508856
58  SwiftUICore                   	0x0000000256dd24e4 0x256d57000 + 505060
59  SwiftUICore                   	0x0000000256d7e96c 0x256d57000 + 162156
60  AttributeGraph                	0x00000001c4eb1ab0 AG::Graph::UpdateStack::update() + 524 (ag-graph-update.cc:583)
61  AttributeGraph                	0x00000001c4eb0674 AG::Graph::update_attribute(AG::data::ptr, unsigned int) + 420 (ag-graph-update.cc:712)
62  AttributeGraph                	0x00000001c4ebe088 AG::Subgraph::update(unsigned int) + 848 (ag-subgraph.cc:759)
63  SwiftUICore                   	0x0000000256e90fd4 0x256d57000 + 1286100
64  SwiftUICore                   	0x0000000256e8fdc0 0x256d57000 + 1281472
65  SwiftUICore                   	0x0000000256e8f8ec 0x256d57000 + 1280236
66  SwiftUI                       	0x000000019bb0c264 UIHostingViewBase.layoutSubviews() + 324 (UIHostingViewBase.swift:394)
67  SwiftUI                       	0x000000019bb0c108 _UIHostingView.layoutSubviews() + 116 (UIHostingView.swift:701)
68  SwiftUI                       	0x000000019bb0c070 @objc _UIHostingView.layoutSubviews() + 36 (:0)
69  UIKitCore                     	0x0000000199b70360 0x199b54000 + 115552
70  QuartzCore                    	0x0000000198de0444 0x198dde000 + 9284
71  QuartzCore                    	0x0000000198ddffdc 0x198dde000 + 8156
72  QuartzCore                    	0x0000000198eacda0 0x198dde000 + 847264
73  QuartzCore                    	0x0000000198e2e914 0x198dde000 + 330004
74  UIKitCore                     	0x0000000199c3c41c 0x199b54000 + 951324
75  CoreFoundation                	0x00000001972d6440 0x197262000 + 476224
76  CoreFoundation                	0x00000001972d363c 0x197262000 + 464444
77  CoreFoundation                	0x00000001972d2d3c 0x197262000 + 462140
78  CoreFoundation                	0x00000001972f77f0 0x197262000 + 612336
79  GraphicsServices              	0x00000001e454a190 0x1e4549000 + 4496
80  UIKitCore                     	0x0000000199f1e158 0x199b54000 + 3973464
81  UIKitCore                     	0x0000000199f1c388 0x199b54000 + 3965832
82  SwiftUI                       	0x000000019bb7307c closure #1 in KitRendererCommon(_:) + 168 (UIKitApp.swift:68)
83  SwiftUI                       	0x000000019bb72fac runApp</a><a>(_:) + 100 (UIKitApp.swift:16)
84  SwiftUI                       	0x000000019bb72e90 static App.main() + 180 (App.swift:136)
85  SubManager                    	0x0000000105086ebc 0x104fd4000 + 732860
86  dyld                          	0x00000001be8a3a58 start + 5964 (dyldMain.cpp:1443)

Thread 1:
0   libsystem_pthread.dylib       	0x00000002214859f0 start_wqthread + 0

Thread 2:
0   libsystem_kernel.dylib        	0x00000001e8451e8c 0x1e844c000 + 24204
1   libsystem_malloc.dylib        	0x00000001a7b107fc 0x1a7b0d000 + 14332
2   libsystem_malloc.dylib        	0x00000001a7b13208 0x1a7b0d000 + 25096
3   libsqlite3.dylib              	0x00000001c631e138 0x1c62bf000 + 389432
4   libsqlite3.dylib              	0x00000001c6361a2c 0x1c62bf000 + 666156
5   libsqlite3.dylib              	0x00000001c631b384 0x1c62bf000 + 377732
6   libsqlite3.dylib              	0x00000001c63615b0 0x1c62bf000 + 665008
7   libsqlite3.dylib              	0x00000001c62d40f4 0x1c62bf000 + 86260
8   libsqlite3.dylib              	0x00000001c62f4f50 0x1c62bf000 + 221008
9   libsqlite3.dylib              	0x00000001c62ea29c 0x1c62bf000 + 176796
10  CoreData                      	0x000000019f410e28 0x19f40a000 + 28200
11  CoreData                      	0x000000019f412e28 0x19f40a000 + 36392
12  CoreData                      	0x000000019f4deb40 0x19f40a000 + 871232
13  CoreData                      	0x000000019f6016b0 0x19f40a000 + 2062000
14  CoreData                      	0x000000019f6023b0 0x19f40a000 + 2065328
15  CoreData                      	0x000000019f6dbafc 0x19f40a000 + 2956028
16  CoreData                      	0x000000019f49f358 0x19f40a000 + 611160
17  CoreData                      	0x000000019f476830 0x19f40a000 + 444464
18  CoreData                      	0x000000019f43a828 0x19f40a000 + 198696
19  CoreData                      	0x000000019f43a5a0 0x19f40a000 + 198048
20  libdispatch.dylib             	0x000000019f1e24f8 0x19f1c7000 + 111864
21  libdispatch.dylib             	0x000000019f1d86d8 0x19f1c7000 + 71384
22  CoreData                      	0x000000019f43a488 0x19f40a000 + 197768
23  CoreData                      	0x000000019f43a354 0x19f40a000 + 197460
24  CoreData                      	0x000000019f43a224 0x19f40a000 + 197156
25  CoreData                      	0x000000019f439f0c 0x19f40a000 + 196364
26  CoreData                      	0x000000019f6dae88 0x19f40a000 + 2952840
27  CoreData                      	0x000000019f541c68 0x19f40a000 + 1277032
28  CoreData                      	0x000000019f5f0f8c 0x19f40a000 + 1994636
29  CoreData                      	0x000000019f6e142c 0x19f40a000 + 2978860
30  CoreData                      	0x000000019f5ee3b8 0x19f40a000 + 1983416
31  CoreData                      	0x000000019f68009c 0x19f40a000 + 2580636
32  CoreData                      	0x000000019f67eb04 0x19f40a000 + 2575108
33  libdispatch.dylib             	0x000000019f1c8aac 0x19f1c7000 + 6828
34  libdispatch.dylib             	0x000000019f1e24f8 0x19f1c7000 + 111864
35  libdispatch.dylib             	0x000000019f1d12cc 0x19f1c7000 + 41676
36  libdispatch.dylib             	0x000000019f1d1dc4 0x19f1c7000 + 44484
37  libdispatch.dylib             	0x000000019f1dc178 0x19f1c7000 + 86392
38  libdispatch.dylib             	0x000000019f1db9fc 0x19f1c7000 + 84476
39  libsystem_pthread.dylib       	0x0000000221488660 _pthread_wqthread + 292 (pthread.c:2696)
40  libsystem_pthread.dylib       	0x00000002214859f8 start_wqthread + 8

Thread 3:
0   libsystem_kernel.dylib        	0x00000001e845191c 0x1e844c000 + 22812
1   libsystem_platform.dylib      	0x00000002213dc0e0 0x2213d9000 + 12512
2   libobjc.A.dylib               	0x000000019481be80 0x194814000 + 32384
3   libobjc.A.dylib               	0x00000001948174e4 0x194814000 + 13540
4   Foundation                    	0x0000000196013310 0x195ef8000 + 1159952
5   StoreKit                      	0x00000001c56e64e0 0x1c55d5000 + 1119456
6   StoreKit                      	0x00000001c56e6b6d 0x1c55d5000 + 1121133
7   libswift_Concurrency.dylib    	0x00000001a2dc3cc9 0x1a2d5f000 + 412873

Thread 4:
0   libsystem_kernel.dylib        	0x00000001e844cbdc 0x1e844c000 + 3036
1   libsystem_kernel.dylib        	0x00000001e8450520 0x1e844c000 + 17696
2   libxpc.dylib                  	0x00000002214f4d54 0x2214cf000 + 154964
3   libxpc.dylib                  	0x00000002214f4da0 0x2214cf000 + 155040
4   libxpc.dylib                  	0x00000002214f4ddc 0x2214cf000 + 155100
5   libxpc.dylib                  	0x00000002214df618 0x2214cf000 + 67096
6   Foundation                    	0x0000000195f81248 0x195ef8000 + 561736
7   Foundation                    	0x00000001960144c8 0x195ef8000 + 1164488
8   Foundation                    	0x0000000196014038 0x195ef8000 + 1163320
9   StoreKit                      	0x00000001c562658c 0x1c55d5000 + 333196
10  StoreKit                      	0x00000001c56511fc 0x1c55d5000 + 508412
11  StoreKit                      	0x00000001c564fa75 0x1c55d5000 + 502389
12  StoreKit                      	0x00000001c565b361 0x1c55d5000 + 549729
13  libswift_Concurrency.dylib    	0x00000001a2dc3cc9 0x1a2d5f000 + 412873

Thread 5:
0   libsystem_pthread.dylib       	0x00000002214859f0 start_wqthread + 0

Thread 6:
0   libsystem_kernel.dylib        	0x00000001e844cce4 0x1e844c000 + 3300
1   libsystem_kernel.dylib        	0x00000001e845039c 0x1e844c000 + 17308
2   libsystem_kernel.dylib        	0x00000001e84502b8 0x1e844c000 + 17080
3   libsystem_kernel.dylib        	0x00000001e8450100 0x1e844c000 + 16640
4   CoreFoundation                	0x00000001972d37b0 0x197262000 + 464816
5   CoreFoundation                	0x00000001972d2e90 0x197262000 + 462480
6   CoreFoundation                	0x00000001972f77f0 0x197262000 + 612336
7   Foundation                    	0x0000000195f6c728 0x195ef8000 + 476968
8   Foundation                    	0x0000000195f6b558 0x195ef8000 + 472408
9   UIKitCore                     	0x000000019a011354 0x199b54000 + 4969300
10  Foundation                    	0x000000019600df40 0x195ef8000 + 1138496
11  libsystem_pthread.dylib       	0x0000000221485afc _pthread_start + 136 (pthread.c:931)
12  libsystem_pthread.dylib       	0x0000000221485a04 thread_start + 8

Thread 7:
0   libsystem_pthread.dylib       	0x00000002214859f0 start_wqthread + 0

Thread 8 Crashed:
0   libsystem_kernel.dylib        	0x00000001e84571dc 0x1e844c000 + 45532
1   libsystem_pthread.dylib       	0x000000022148bb40 pthread_kill + 268 (pthread.c:1721)
2   libsystem_c.dylib             	0x000000019f2841a4 abort + 124 (abort.c:122)
3   libc++abi.dylib               	0x00000002213b55a0 0x22139f000 + 91552
4   libc++abi.dylib               	0x00000002213a3f10 0x22139f000 + 20240
5   libobjc.A.dylib               	0x0000000194848e48 0x194814000 + 216648
6   libc++abi.dylib               	0x00000002213b48b4 0x22139f000 + 88244
7   libc++abi.dylib               	0x00000002213b7e1c 0x22139f000 + 101916
8   libc++abi.dylib               	0x00000002213b7dc4 0x22139f000 + 101828
9   libobjc.A.dylib               	0x0000000194846e74 0x194814000 + 208500
10  Foundation                    	0x000000019668e990 0x195ef8000 + 7956880
11  BackgroundTasks               	0x00000002301856ec -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:] + 448 (BGTaskScheduler.m:0)
12  BackgroundTasks               	0x00000002301854ec -[BGTaskScheduler registerForTaskWithIdentifier:usingQueue:launchHandler:] + 128 (BGTaskScheduler.m:209)
13  SwiftUI                       	0x000000019be5fb4c -[BGTaskSchedulerProxy registerForTaskWithIdentifier:launchHandler:] + 220 (BackgroundTask_SPI.m:40)
14  SwiftUI                       	0x000000019c790028 closure #1 in AppRefreshBackgroundTask.register() + 344 (BackgroundTask.swift:408)
15  SwiftUI                       	0x000000019bab3e15  + 1
16  SwiftUI                       	0x000000019bc24239  + 1
17  SwiftUI                       	0x000000019ba94829  + 1
18  libswift_Concurrency.dylib    	0x00000001a2dc3cc9 0x1a2d5f000 + 412873

Thread 9:
0   libsystem_pthread.dylib       	0x00000002214859f0 start_wqthread + 0

Thread 8 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x00000002213b9fdb   x5: 0x000000016b285b70   x6: 0x000000000000006e   x7: 0x38e9fa0854b0a59a
    x8: 0x689bd3ba61a11659   x9: 0x689bd3bb0a896659  x10: 0x0000000000000051  x11: 0x000000000000000b
   x12: 0x000000000000000b  x13: 0x000000019771c48c  x14: 0x0000000000000001  x15: 0xffffffffb00007ff
   x16: 0x0000000000000148  x17: 0x000000016b287000  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000005903  x21: 0x000000016b2870e0  x22: 0x0000000200c3c000  x23: 0x00000002052cde08
   x24: 0x00000001090917c0  x25: 0x0000000000000001  x26: 0x0000000000000000  x27: 0x00000000fff0ffff
   x28: 0x0000000000000000   fp: 0x000000016b285ae0   lr: 0x000000022148bb40
    sp: 0x000000016b285ac0   pc: 0x00000001e84571dc cpsr: 0x40001000
   esr: 0x56000080  Address size fault


Binary Images:
        0x104fd4000 -         0x10519bfff SubManager arm64  <4aeebcac9cb03109ae8421318b46963b> /var/containers/Bundle/Application/0D87594A-A61A-4E74-9ED8-EA9E238C3E1D/SubManager.app/SubManager
        0x105354000 -         0x10535ffff libobjc-trampolines.dylib arm64e   /private/preboot/Cryptexes/OS/usr/lib/libobjc-trampolines.dylib
        0x194814000 -         0x194865d73 libobjc.A.dylib arm64e   /usr/lib/libobjc.A.dylib
        0x195940000 -         0x195e9ff1f libswiftCore.dylib arm64e   /usr/lib/swift/libswiftCore.dylib
        0x195ef8000 -         0x196b640df Foundation arm64e  <7167d9540f1939ed96a6577ecad74a5c> /System/Library/Frameworks/Foundation.framework/Foundation
        0x197262000 -         0x1977ddfff CoreFoundation arm64e  <992897955e433ccca76d93489fef7fdd> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
        0x198dde000 -         0x199197e9f QuartzCore arm64e  <804c69b39a57381c8b34737a7fee8895> /System/Library/Frameworks/QuartzCore.framework/QuartzCore
        0x199b54000 -         0x19ba8e4df UIKitCore arm64e  <89217405b3323bd6ab460d53ed8de3bf> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
        0x19ba8f000 -         0x19cd08f9f SwiftUI arm64e   /System/Library/Frameworks/SwiftUI.framework/SwiftUI
        0x19f1c7000 -         0x19f20ca3f libdispatch.dylib arm64e  <2d4e905fea9d3209b037a12df57c6a09> /usr/lib/system/libdispatch.dylib
        0x19f20d000 -         0x19f28c70f libsystem_c.dylib arm64e   /usr/lib/system/libsystem_c.dylib
        0x19f40a000 -         0x19f80165f CoreData arm64e  <9c50dc8db766371486d6cc49fe6f8e2b> /System/Library/Frameworks/CoreData.framework/CoreData
        0x1a2d5f000 -         0x1a2dddd4f libswift_Concurrency.dylib arm64e  <8e811830512e33cd9bdb6377aade00d3> /usr/lib/swift/libswift_Concurrency.dylib
        0x1a7b0d000 -         0x1a7b4cc07 libsystem_malloc.dylib arm64e  <31077a87998d32f3b09ff819bfbed92b> /usr/lib/system/libsystem_malloc.dylib
        0x1b3f9f000 -         0x1b472a75f CoreML arm64e  <844abca32e0633d78c6bfcb0f629325e> /System/Library/Frameworks/CoreML.framework/CoreML
        0x1be893000 -         0x1be92cf0f dyld arm64e  <5b719cbea7093fbc8824c5d21636d2bd> /usr/lib/dyld
        0x1c4ead000 -         0x1c4eefb1f AttributeGraph arm64e  <8ab7a82a879938a9b3f5516c5373d92d> /System/Library/PrivateFrameworks/AttributeGraph.framework/AttributeGraph
        0x1c55d5000 -         0x1c5783ddf StoreKit arm64e  <91f6358d537833428d902a8f25ae80ae> /System/Library/Frameworks/StoreKit.framework/StoreKit
        0x1c62bf000 -         0x1c643f73f libsqlite3.dylib arm64e  <87a4a93d335937a38499ad5930d2fda9> /usr/lib/libsqlite3.dylib
        0x1e4549000 -         0x1e4551c5f GraphicsServices arm64e  <6b11b9c1f1003dbb89bd24f20a185c7e> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
        0x1e844c000 -         0x1e8485b87 libsystem_kernel.dylib arm64e  <88a884af489138e6bd01bc71af4c7477> /usr/lib/system/libsystem_kernel.dylib
        0x22139f000 -         0x2213bcfff libc++abi.dylib arm64e  <2dbdca052baa3bfe9ef315a157d84058> /usr/lib/libc++abi.dylib
        0x2213d9000 -         0x2213e0627 libsystem_platform.dylib arm64e  <33943e1731ad350bbb229aed52a0c814> /usr/lib/system/libsystem_platform.dylib
        0x221484000 -         0x2214903fb libsystem_pthread.dylib arm64e  <0d89e20803853ce68691b69e35601e52> /usr/lib/system/libsystem_pthread.dylib
        0x2214cf000 -         0x221516dbf libxpc.dylib arm64e  <9c4b399c656a390d8afa4e7187aa58c9> /usr/lib/system/libxpc.dylib
        0x230184000 -         0x23019147f BackgroundTasks arm64e  <4aa4726a0519382ab80019786438277f> /System/Library/Frameworks/BackgroundTasks.framework/BackgroundTasks
        0x256d57000 -         0x2578b4c3f SwiftUICore arm64e   /System/Library/Frameworks/SwiftUICore.framework/SwiftUICore

EOF
</a>
Answered by DTS Engineer in 826562022

Submitted a bug report: FB16595418

Looking the data over, I think this is bug on our side, as the crash is actually coming from SwiftUI's background task integration, not your own code. It's possible there is a timing issue between your usage and SwiftUI, but that would still mean that SwiftUI changed "something" that altered the timing of activity.

When do you call "BGTaskScheduler.register(forTaskWithIdentifier:using:launchHandler:)"? That's the one behavior you have control over which could be a factor in this crash.

Having said all that, please replicate the issue a few time, collect a sysdiagnose, upload it to your bug, and then let me know here when all of that is done. The sysdiagnose should clarify exactly what's going wrong.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Our engineering teams need to investigate this issue, as this might indicate an issue with iOS 18.4 which is still in Beta.

We'd greatly appreciate it if you could open a bug report, include crash logs and sample code or models that reproduce the issue, and post the FB number here once you do.

Bug Reporting: How and Why? has tips on creating a successful bug report.

Same here. Happens only on iOS 18.4

Submitted a bug report: FB16595418

Same. Submitted logs in FB16610879

Submitted a bug report: FB16595418

Looking the data over, I think this is bug on our side, as the crash is actually coming from SwiftUI's background task integration, not your own code. It's possible there is a timing issue between your usage and SwiftUI, but that would still mean that SwiftUI changed "something" that altered the timing of activity.

When do you call "BGTaskScheduler.register(forTaskWithIdentifier:using:launchHandler:)"? That's the one behavior you have control over which could be a factor in this crash.

Having said all that, please replicate the issue a few time, collect a sysdiagnose, upload it to your bug, and then let me know here when all of that is done. The sysdiagnose should clarify exactly what's going wrong.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Hi Kevin,

There's no usage of BGTaskScheduler.register anywhere in my app. Am I supposed to call this method at some point?

I use the background tasks API in two places currently:

  1. Calling BGTaskScheduler.shared.submit during .onChange(of: scenePhase) when newPhase == .active, on the view that appears when my app is launched. It schedules the background task whenever my app is brought to the foreground.

  2. .backgroundTask(.appRefresh("notifications")) attached to my WindowGroup. I attached the code snippet of this in my initial bug report.

I have just managed to reproduce the crash after quitting and relaunching the app about 100 times. The bug report has been updated with the sysdiagnose and a copy of the crash report.

Also happening here. I use .backgroundTask modifier on WindowGroup. Only an issue with 18.4.

There's no usage of BGTaskScheduler.register anywhere in my app. Am I supposed to call this method at some point?

Yes, very much so. Looking thing over a bit more closely, I think your app may basically have been working by "accident". What you're supposed to do is:

Failing to do so SHOULD crash your app with exactly this crash:

Fatal Exception: NSInternallnconsistencyException
All launch handlers must be registered before application finishes launching

With all that background, there are two more points:

1) Why you didn't crash before

My guess is that the generic "notifications" ID was actually being registered and used by something else in the system.

 let request = BGAppRefreshTaskRequest(identifier: "notifications")
 

Using an short ID like that was a mistake in our part and correcting that mistake would have created the crash you're currently seeing.

2) Your code isn't actually doing anything (sort of).

All this call does:

try BGTaskScheduler.shared.submit(request)

...is tell the system "I'd like to run this work at some point in the future, so let me know when I should run". The way that work is actually supposed to run is through the block you setup in "register(forTaskWithIdentifier:using:launchHandler:)", which we'll call when it's time for your to actually do that work.

The "sort of" part is that it's possible this was working because this is specifically for app refresh. If the old background app refresh delegate was in place, it's likely that it fired as well, masking the issue.

In any case, if you set everything up properly (see above), then I think this crash will go away.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Thanks for the response. I am still a bit confused however. My understanding is that the .backgroundTask modifier is the replacement for BGTaskScheduler.register in SwiftUI. And the code that runs during the background task is part of that modifier. As per the WWDC22 video Efficiency awaits: Background tasks in SwiftUI, it is used to register the handler.

Also worth noting that the video uses a short ID similar to what I did.

If I take out the modifier, the app crashes with this message:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No launch handler registered for task with identifier notifications'

Which indicates that the modifier is indeed used for registering the handler.

There's no difference to this behaviour whether I use the short ID notifications or the reverse DNS com.ip18.SubManager.notifications. If the modifier is not there then the app crashes.

Interestingly when I simply killed and ran my app from Xcode multiple times, I eventually came across the All launch handlers must be registered before application finishes launching crash in one of those launches. Which is probably what the crash reports are about.

Attached a symbolicated crash report from one of those Xcode sessions to FB16595418. If I'm understanding this right, it looks like BGTaskScheduler registerForTaskWithIdentifier is already being called internally by the modifier.

Thread 2 Crashed:
0   libsystem_kernel.dylib        	       0x1ef16f1dc __pthread_kill + 8
1   libsystem_pthread.dylib       	       0x2281a3b40 pthread_kill + 268
2   libsystem_c.dylib             	       0x1a5f9c234 __abort + 132
3   libsystem_c.dylib             	       0x1a5f9c1b0 abort + 136
4   libc++abi.dylib               	       0x2280cd5a0 abort_message + 132
5   libc++abi.dylib               	       0x2280bbf10 demangling_terminate_handler() + 344
6   libobjc.A.dylib               	       0x19b560e48 _objc_terminate() + 156
7   libc++abi.dylib               	       0x2280cc8b4 std::__terminate(void (*)()) + 16
8   libc++abi.dylib               	       0x2280cfe1c __cxxabiv1::failed_throw(__cxxabiv1::__cxa_exception*) + 88
9   libc++abi.dylib               	       0x2280cfdc4 __cxa_throw + 92
10  libobjc.A.dylib               	       0x19b55ee74 objc_exception_throw + 448
11  Foundation                    	       0x19d3a6990 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 288
12  BackgroundTasks               	       0x236e9d6ec -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:] + 448
13  BackgroundTasks               	       0x236e9d4ec -[BGTaskScheduler registerForTaskWithIdentifier:usingQueue:launchHandler:] + 128
14  SwiftUI                       	       0x1a2b77b4c -[BGTaskSchedulerProxy registerForTaskWithIdentifier:launchHandler:] + 220
15  SwiftUI                       	       0x1a34a8028 closure #1 in AppRefreshBackgroundTask.register() + 344
16  SwiftUI                       	       0x1a27cbe15 <deduplicated_symbol> + 1
17  SwiftUI                       	       0x1a293c239 <deduplicated_symbol> + 1
18  SwiftUI                       	       0x1a27ac829 <deduplicated_symbol> + 1
19  libswift_Concurrency.dylib    	       0x1a9adbcc9 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1

My understanding is that the .backgroundTask modifier is the replacement for BGTaskScheduler.register in SwiftUI.

Sigh... yes, I'd forgotten about that API.

If I'm understanding this right, it looks like BGTaskScheduler registerForTaskWithIdentifier is already being called internally by the modifier.

Yes. More specifically, "_unsafe_registerForTaskWithIdentifier" does two different checks when it runs:

  1. It checks how far the app is into the launch process and, if it's to late in that process, it crashes with "All launch handlers must be registered before application finishes launching".

  2. It checks if the ID has already been registered and, if it has, it crashes with "Launch handler for task with identifier <identifier> has already been registered".

...and you're obviously crashing on #1. Moving to here:

Interestingly when I simply killed and ran my app from Xcode multiple times, I eventually came across the All launch handlers must be registered before application finishes launching crash in one of those launches. Which is probably what the crash reports are about.

Based on the evidence at hand, I suspect the issue is that SwiftUI is sometimes registering the crash later than it should, triggering #1. That leads to the most direct solution (assuming you won't want to wait for a bug fix), which would be to stop using ".backgroundTask" and directly use BGTaskScheduler.register.

Also, as a quick clarification here:

.backgroundTask modifier is the replacement for BGTaskScheduler.register in SwiftUI.

Thinking of it as a replacement is somewhat misleading. SwiftUI's architecture is actually trying to provide a unified entry point across multiple frameworks for use cases that are particularly relevant to the interface. It does support BGAppRefreshTask, but it does NOT support the other task types (BGProcessingTask and BGHealthResearchTaskRequest) and, more to the point, all of other task types are totally unrelated to the BackgroundTask framework.

The reason for this is that it isn't actually trying to provide a true replacement. It support BGAppRefreshTask because the primary use case for BGAppRefreshTask is to update the user interface so that it remains current. That's the same reason it DOESN'T support BGProcessingTask. BGProcessingTask is intended to be used for work which requires extended runtime ("several minutes"), which is exactly the kind of work that wouldn't/shouldn't be directly tied to the user interface. Similarly, if your app refresh work is primarily tied to the "backend" of your app, it can be better to use the original register(forTaskWithIdentifier:using:launchHandler:) API instead of artificially creating a connection in SwiftUI that you wouldn't otherwise need.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

BGTaskScheduler crashes on iOS 18.4
 
 
Q