Skip to content

Commit a4f36b1

Browse files
committed
Fix a potential duplicated completion invoking when task cancel
There seems to be a short period that the callback can be accessed from another thread before the next lock
1 parent 2dd662b commit a4f36b1

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Sources/Networking/SessionDataTask.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ public class SessionDataTask: @unchecked Sendable {
101101
return nil
102102
}
103103

104-
func removeAllCallbacks() -> Void {
104+
@discardableResult
105+
func removeAllCallbacks() -> [TaskCallback] {
105106
lock.lock()
106107
defer { lock.unlock() }
108+
let callbacks = callbacksStore.values
107109
callbacksStore.removeAll()
110+
return Array(callbacks)
108111
}
109112

110113
func resume() {

Sources/Networking/SessionDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ extension SessionDelegate: URLSessionDataDelegate {
263263
guard let sessionTask = self.task(for: task) else {
264264
return
265265
}
266-
sessionTask.onTaskDone.call((result, sessionTask.callbacks))
266+
let callbacks = sessionTask.removeAllCallbacks()
267+
sessionTask.onTaskDone.call((result, callbacks))
267268
remove(sessionTask)
268269
}
269270
}

0 commit comments

Comments
 (0)