Skip to content

Commit 4787ff0

Browse files
author
Anton Pogonets
committed
Dont crash on missing URLSessionTask.behaviour
1 parent e758362 commit 4787ff0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Foundation/URLSession/TaskRegistry.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ extension URLSession._TaskRegistry {
109109
///
110110
/// - Note: It is a programming error to pass a task that isn't registered.
111111
/// - Note: This must **only** be accessed on the owning session's work queue.
112-
func behaviour(for task: URLSessionTask) -> _Behaviour {
113-
guard let b = behaviours[task.taskIdentifier] else {
114-
fatalError("Trying to access a behaviour for a task that in not in the registry.")
115-
}
116-
return b
112+
func behaviour(for task: URLSessionTask) -> _Behaviour? {
113+
return behaviours[task.taskIdentifier]
117114
}
118115
}

Foundation/URLSession/URLSession.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,16 @@ internal extension URLSession {
557557

558558
func behaviour(for task: URLSessionTask) -> _TaskBehaviour {
559559
switch taskRegistry.behaviour(for: task) {
560-
case .dataCompletionHandler(let c): return .dataCompletionHandler(c)
561-
case .downloadCompletionHandler(let c): return .downloadCompletionHandler(c)
562-
case .callDelegate:
560+
case .dataCompletionHandler(let c)?: return .dataCompletionHandler(c)
561+
case .downloadCompletionHandler(let c)?: return .downloadCompletionHandler(c)
562+
case .callDelegate?:
563563
guard let d = delegate as? URLSessionTaskDelegate else {
564564
return .noDelegate
565565
}
566566
return .taskDelegate(d)
567+
case .none:
568+
NSLog("Trying to access a behaviour for a task that in not in the registry.")
569+
return .noDelegate
567570
}
568571
}
569572
}

0 commit comments

Comments
 (0)