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
...
}

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

I've recently found that the crash also happens on my old iPad with iOS 16.7.10 when using .backgroundTask(.appRefresh) and attempting to run a shortcut while force closed. This makes it seem like it's an old bug that has returned in iOS 18.4.

Looks like the modifier is no longer an option for me and will have to permanently stick with the old way.

It's not ideal but it took me about 10 minutes to revert it so doesn't seem like the end of the world, at least for most implementations.

Kevin's signature says he is a DTS engineer, that means Developer Technical Support which at least commonly would suggest he's not the person who would be assigned to work on fixing a bug like this but to help developers understand the APIs and work around issues. I appreciate him giving us such detailed information - that's not always the case.

Kevin's signature says he is a DTS engineer, that means Developer Technical Support which at least commonly would suggest he's not the person who would be assigned to work on fixing a bug like this but to help developers understand the APIs and work around issues.

Yes, that's basically the job. I do work closely with the engineering teams helping prioritize bugs and occasionally investigating them, but I'm not directly involved with specifically "fixing" anything.

I'm not going to discuss the bug side of this in any detail, but what I will say that the issue is being taken seriously and not ignored. It wasn't possible to correct the issue in 18.4, but that doesn't mean that the problem isn't important or won't be fixed.

I appreciate him giving us such detailed information - that's not always the case.

Thank you and you're very welcome.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

I'm also running into this issue, my app crashes almost every time I unblock my phone in the background on iOS 18.4... no changes to the codebase between iOS 18.3.2 and the latest iOS 18.4 beta

I'm also utilizing hourly steps for HealthKit background delivery.

Would love to see a fix, filed a bug ticket as FB17063126

Thanks so much for your help in this thread @DTS Engineer.

__

Andreas Ink

Hello everyone, I wrote a comment under one of the above posts, but as the comments are hidden by default, I add it here:

Ah, the dreaded "Efficiency awaits: Background tasks in SwiftUI" talk. I believe part of the features from that talk are still not implemented as of March 2025 (the talk probably describes features Apple hoped to release by WWDC 22, but then scrapped or delayed them).

The .backgroundTask(.urlSession(...)) {} call described in the later part of the talk is entirely non-existent, and the presented has a placeholder code inside that callback, as there is no valid code which would compile there.

Imo the whole talk should be retracted and replaced with a correct info, as I find traces of many developers spending hours and days trying to implement the described solution that doesn't work.

See https://developer.apple.com/forums/thread/726443?answerId=823711022#823711022

The release notes of iOS 18.4.1 only include the generic 'This update provides important bug fixes' but do not mention a fix for this. Does anyone know whether this got fixed or not?

Accepted Answer

Looks like it’s been fixed in iOS 18.5 beta 2

Looks like it’s been fixed in iOS 18.5 beta 2

It's good to see that notification went out on time and, yes, the bug fix was specifically included in iOS 18.5b2.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

BGTaskScheduler crashes on iOS 18.4
 
 
Q