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 racing on data reading in session #2327

Merged
merged 1 commit into from
Dec 1, 2024
Merged
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
13 changes: 10 additions & 3 deletions Sources/Networking/SessionDataTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ public class SessionDataTask: @unchecked Sendable {
let options: KingfisherParsedOptionsInfo
}

private var _mutableData: Data
/// The downloaded raw data of the current task.
public private(set) var mutableData: Data
public var mutableData: Data {
lock.lock()
defer { lock.unlock() }
return _mutableData
}

// This is a copy of `task.originalRequest?.url`. It is for obtaining race-safe behavior for a pitfall on iOS 13.
// Ref: https://github.com/onevcat/Kingfisher/issues/1511
Expand Down Expand Up @@ -80,7 +85,7 @@ public class SessionDataTask: @unchecked Sendable {
init(task: URLSessionDataTask) {
self.task = task
self.originalURL = task.originalRequest?.url
mutableData = Data()
_mutableData = Data()
}

func addCallback(_ callback: TaskCallback) -> CancelToken {
Expand Down Expand Up @@ -130,6 +135,8 @@ public class SessionDataTask: @unchecked Sendable {
}

func didReceiveData(_ data: Data) {
mutableData.append(data)
lock.lock()
defer { lock.unlock() }
_mutableData.append(data)
}
}