Fix CFNetwork SIGABRT crash when creating download tasks on invalidated session

The background URLSession could be in an invalid state when downloadTask(with:)
is called, because invalidateAndCancel() is asynchronous internally. This adds
an ObjC exception handler to catch NSExceptions from CFNetwork, nil guards on
the session, and safer session lifecycle management (nil after invalidation,
finishTasksAndInvalidate for cellular toggle).
This commit is contained in:
Arkadiusz Fal
2026-02-19 17:25:14 +01:00
parent 357852fbd9
commit 13614e7fa0
5 changed files with 95 additions and 5 deletions

View File

@@ -0,0 +1,17 @@
//
// ObjCExceptionHandler.h
// Yattee
//
// Catches ObjC NSExceptions that Swift cannot handle natively.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/// Executes a block and catches any NSException thrown.
/// Returns YES if the block executed without throwing, NO if an exception was caught.
/// If an exception is caught, it is returned via the outException parameter.
BOOL tryCatchObjCException(void (NS_NOESCAPE ^block)(void), NSException *_Nullable *_Nullable outException);
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,20 @@
//
// ObjCExceptionHandler.m
// Yattee
//
// Catches ObjC NSExceptions that Swift cannot handle natively.
//
#import "ObjCExceptionHandler.h"
BOOL tryCatchObjCException(void (NS_NOESCAPE ^block)(void), NSException *_Nullable *_Nullable outException) {
@try {
block();
return YES;
} @catch (NSException *exception) {
if (outException) {
*outException = exception;
}
return NO;
}
}

View File

@@ -99,10 +99,41 @@ extension DownloadManager {
resumeData: Data?, resumeData: Data?,
httpHeaders: [String: String]? = nil httpHeaders: [String: String]? = nil
) { ) {
guard urlSession != nil else {
LoggingService.shared.logDownloadError(
"[Downloads] URLSession is nil in startStreamDownload (\(phase))",
error: DownloadError.downloadFailed("URLSession not available - session may be invalidated")
)
handleDownloadError(
downloadID: downloadID,
phase: phase,
error: DownloadError.downloadFailed("URLSession not available")
)
return
}
let task: URLSessionDownloadTask let task: URLSessionDownloadTask
if let resumeData { if let resumeData {
task = urlSession.downloadTask(withResumeData: resumeData) var caughtException: NSException?
var resumeTask: URLSessionDownloadTask?
let success = tryCatchObjCException({
resumeTask = self.urlSession.downloadTask(withResumeData: resumeData)
}, &caughtException)
guard success, let resumeTask else {
LoggingService.shared.logDownloadError(
"[Downloads] NSException creating resume task (\(phase))",
error: DownloadError.downloadFailed("CFNetwork exception: \(caughtException?.reason ?? "unknown")")
)
handleDownloadError(
downloadID: downloadID,
phase: phase,
error: DownloadError.downloadFailed("Failed to create download task: \(caughtException?.reason ?? "unknown")")
)
return
}
task = resumeTask
} else { } else {
// Starting fresh without resumeData - reset progress for this phase // Starting fresh without resumeData - reset progress for this phase
// to avoid jumping when saved progress conflicts with new URLSession progress // to avoid jumping when saved progress conflicts with new URLSession progress
@@ -128,7 +159,26 @@ extension DownloadManager {
request.setValue(value, forHTTPHeaderField: key) request.setValue(value, forHTTPHeaderField: key)
} }
} }
task = urlSession.downloadTask(with: request)
var caughtException: NSException?
var newTask: URLSessionDownloadTask?
let success = tryCatchObjCException({
newTask = self.urlSession.downloadTask(with: request)
}, &caughtException)
guard success, let newTask else {
LoggingService.shared.logDownloadError(
"[Downloads] NSException creating download task (\(phase))",
error: DownloadError.downloadFailed("CFNetwork exception: \(caughtException?.reason ?? "unknown")")
)
handleDownloadError(
downloadID: downloadID,
phase: phase,
error: DownloadError.downloadFailed("Failed to create download task: \(caughtException?.reason ?? "unknown")")
)
return
}
task = newTask
} }
task.taskDescription = "\(downloadID.uuidString):\(phase.rawValue)" task.taskDescription = "\(downloadID.uuidString):\(phase.rawValue)"

View File

@@ -148,8 +148,9 @@ final class DownloadManager: NSObject {
self.downloadSettings = settings self.downloadSettings = settings
// Invalidate old session before creating new one with correct settings // Invalidate old session before creating new one with correct settings
urlSession?.invalidateAndCancel() urlSession?.invalidateAndCancel()
urlSession = nil
setupSession() setupSession()
// Resume interrupted downloads only on initial setup // Resume interrupted downloads only on initial setup
if isInitialSetup { if isInitialSetup {
resumeInterruptedDownloads() resumeInterruptedDownloads()
@@ -186,8 +187,9 @@ final class DownloadManager: NSObject {
} }
} }
// 3. Invalidate the old session // 3. Invalidate the old session (use finishTasksAndInvalidate since downloads are already paused)
urlSession.invalidateAndCancel() urlSession?.finishTasksAndInvalidate()
urlSession = nil
// 4. Create new session with updated cellular config // 4. Create new session with updated cellular config
setupSession() setupSession()

View File

@@ -8,4 +8,5 @@
#ifndef Yattee_Bridging_Header_h #ifndef Yattee_Bridging_Header_h
#define Yattee_Bridging_Header_h #define Yattee_Bridging_Header_h
#import "SMBBridge.h" #import "SMBBridge.h"
#import "ObjCExceptionHandler.h"
#endif /* Yattee_Bridging_Header_h */ #endif /* Yattee_Bridging_Header_h */