From 7c8f37f83f1e0a3ebccd65d6bcc2956bd11573e7 Mon Sep 17 00:00:00 2001 From: Shubhransh Gupta <54713516+shubhransh-gupta@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:19:52 +0530 Subject: [PATCH] Fix data races in progress callback handling Mutating `indicatorStatus.progress` and reading `progressBlock` directly on downloader background threads caused data races with SwiftUI view evaluations in `WebImage.setupManager()`. Dispatch progress mutations and invocations to `DispatchQueue.main.async` in both `ImageManager.load` and `AnimatedImage`, ensuring thread safety. --- SDWebImageSwiftUI/Classes/AnimatedImage.swift | 2 +- SDWebImageSwiftUI/Classes/ImageManager.swift | 12 +++---- Tests/ImageManagerTests.swift | 33 +++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/SDWebImageSwiftUI/Classes/AnimatedImage.swift b/SDWebImageSwiftUI/Classes/AnimatedImage.swift index 4e5a7a75..b64806d8 100644 --- a/SDWebImageSwiftUI/Classes/AnimatedImage.swift +++ b/SDWebImageSwiftUI/Classes/AnimatedImage.swift @@ -262,8 +262,8 @@ public struct AnimatedImage : PlatformViewRepresentable { } DispatchQueue.main.async { context.coordinator.imageLoading.progress = progress + self.imageHandler.progressBlock?(receivedSize, expectedSize) } - self.imageHandler.progressBlock?(receivedSize, expectedSize) }) { (image, data, error, cacheType, finished, _) in context.coordinator.imageLoading.image = image context.coordinator.imageLoading.isLoading = false diff --git a/SDWebImageSwiftUI/Classes/ImageManager.swift b/SDWebImageSwiftUI/Classes/ImageManager.swift index 045bfa37..94c20c86 100644 --- a/SDWebImageSwiftUI/Classes/ImageManager.swift +++ b/SDWebImageSwiftUI/Classes/ImageManager.swift @@ -86,20 +86,18 @@ public final class ImageManager : ObservableObject { self.indicatorStatus.progress = 0 currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in // This block may be called in non-main thread - guard let self = self else { - return - } let progress: Double if (expectedSize > 0) { progress = Double(receivedSize) / Double(expectedSize) } else { progress = 0 } - self.indicatorStatus.progress = progress - if let progressBlock = self.progressBlock { - DispatchQueue.main.async { - progressBlock(receivedSize, expectedSize) + DispatchQueue.main.async { [weak self] in + guard let self = self else { + return } + self.indicatorStatus.progress = progress + self.progressBlock?(receivedSize, expectedSize) } }) { [weak self] (image, data, error, cacheType, finished, _) in guard let self = self else { diff --git a/Tests/ImageManagerTests.swift b/Tests/ImageManagerTests.swift index 8222f5b2..79a77867 100644 --- a/Tests/ImageManagerTests.swift +++ b/Tests/ImageManagerTests.swift @@ -40,4 +40,37 @@ class ImageManagerTests: XCTestCase { sub.cancel() self.waitForExpectations(timeout: 10, handler: nil) } + + func testImageManagerProgressDispatchesToMainThread() throws { + let expectation = self.expectation(description: "ImageManager progress on main thread") + let imageUrl = URL(string: "https://example.com/test.png") + let imageManager = ImageManager() + + class MockLoader: NSObject, SDImageLoader { + func canRequestImage(for url: URL?) -> Bool { true } + func requestImage(with url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]?, progress: SDImageLoaderProgressBlock?, completed: SDImageLoaderCompletedBlock? = nil) -> SDWebImageOperation? { + DispatchQueue.global(qos: .userInitiated).async { + progress?(50, 100, url) + DispatchQueue.global(qos: .userInitiated).async { + completed?(nil, nil, nil, true) + } + } + return nil + } + func shouldBlockFailedURL(with url: URL, error: Error) -> Bool { false } + } + + let mockManager = SDWebImageManager(cache: SDImageCache.shared, loader: MockLoader()) + + imageManager.setOnProgress { receivedSize, expectedSize in + XCTAssertTrue(Thread.isMainThread) + XCTAssertEqual(imageManager.indicatorStatus.progress, 0.5) + XCTAssertEqual(receivedSize, 50) + XCTAssertEqual(expectedSize, 100) + expectation.fulfill() + } + + imageManager.load(url: imageUrl, context: [.customManager: mockManager]) + self.waitForExpectations(timeout: 5, handler: nil) + } }