Skip to content

Commit

Permalink
Fix a potential duplicated completion invoking when task cancel
Browse files Browse the repository at this point in the history
There seems to be a short period that the callback can be accessed from another thread before the next lock
  • Loading branch information
onevcat committed Nov 15, 2024
1 parent 2dd662b commit a4f36b1
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 a4f36b1

Please sign in to comment.