From aa5996c2cc29cea4caec78c9de1bd76a82863f56 Mon Sep 17 00:00:00 2001 From: onevcat Date: Sun, 1 Dec 2024 22:19:28 +0900 Subject: [PATCH] Fix a racing on data reading in session --- Sources/Networking/SessionDataTask.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/Networking/SessionDataTask.swift b/Sources/Networking/SessionDataTask.swift index ae5448e22..520c51c52 100644 --- a/Sources/Networking/SessionDataTask.swift +++ b/Sources/Networking/SessionDataTask.swift @@ -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 @@ -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 { @@ -130,6 +135,8 @@ public class SessionDataTask: @unchecked Sendable { } func didReceiveData(_ data: Data) { - mutableData.append(data) + lock.lock() + defer { lock.unlock() } + _mutableData.append(data) } }