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,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;
}
}