Skip to content

Commit

Permalink
Merge pull request #2319 from onevcat/fix/duplicated-completion-call
Browse files Browse the repository at this point in the history
Fix a potential duplicated completion invoking when task cancel
  • Loading branch information
onevcat authored Nov 19, 2024
2 parents 2dd662b + a4f36b1 commit 6d8b529
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Sources/Networking/SessionDataTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ public class SessionDataTask: @unchecked Sendable {
return nil
}

func removeAllCallbacks() -> Void {
@discardableResult
func removeAllCallbacks() -> [TaskCallback] {
lock.lock()
defer { lock.unlock() }
let callbacks = callbacksStore.values
callbacksStore.removeAll()
return Array(callbacks)
}

func resume() {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Networking/SessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ extension SessionDelegate: URLSessionDataDelegate {
guard let sessionTask = self.task(for: task) else {
return
}
sessionTask.onTaskDone.call((result, sessionTask.callbacks))
let callbacks = sessionTask.removeAllCallbacks()
sessionTask.onTaskDone.call((result, callbacks))
remove(sessionTask)
}
}

0 comments on commit 6d8b529

Please sign in to comment.