Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a potential duplicated completion invoking when task cancel #2319

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}