Skip to content

Commit

Permalink
Fix a racing on data reading in session
Browse files Browse the repository at this point in the history
  • Loading branch information
onevcat committed Dec 1, 2024
1 parent b6256f1 commit aa5996c
Showing 1 changed file with 10 additions and 3 deletions.
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)
}
}

0 comments on commit aa5996c

Please sign in to comment.