Skip to content

Commit e57f2e6

Browse files
CopilotAlexV525
andauthored
Fix iOS PHImageManager crash from QoS background queue access (#1324)
PHImageManager methods were being called from QoS background queues (`QOS_CLASS_USER_INTERACTIVE`, etc.), violating Apple's thread safety requirements and causing crashes with `objc_storeWeak` deadlocks. **Changes** Dispatch all PHImageManager/PHCachingImageManager method calls to main queue: - Image requests: `requestImageForAsset`, `requestAVAssetForVideo` - Cache operations: `startCachingImagesForAssets`, `stopCachingImagesForAllAssets` - Cancellation: `cancelImageRequest` **Example** Before (crashes on background queue): ```objc - (void)fetchThumb:(PHAsset *)asset { PHImageRequestID requestId = [self.cachingManager requestImageForAsset:asset ...]; } ``` After (safe main thread execution): ```objc - (void)fetchThumb:(PHAsset *)asset { dispatch_async(dispatch_get_main_queue(), ^{ PHImageRequestID requestId = [self.cachingManager requestImageForAsset:asset ...]; }); } ``` **Performance Impact** No UI blocking occurs because PHImageManager API calls are non-blocking - they return immediately after submitting the request. Only the lightweight API call submission is dispatched to the main queue; the actual image processing work happens asynchronously on PHImageManager's own background threads. QoS prioritization remains intact—background work still executes on appropriate queues, only the PHImageManager API calls are marshaled to the main thread. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Alex Li <[email protected]>
1 parent 08752fa commit e57f2e6

File tree

2 files changed

+204
-166
lines changed

2 files changed

+204
-166
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ To know more about breaking changes, see the [Migration Guide][].
3030

3131
### Fixes
3232

33+
- Fix PHImageManager crash on iOS by ensuring all PHImageManager/PHCachingImageManager methods are called on the main thread.
34+
This resolves race conditions and deadlocks when thumbnail operations are dispatched to QoS background queues.
3335
- Fix EXC_BAD_ACCESS crash caused by accessing deallocated memory in async blocks on iOS.
3436
- Fixed PHCachingImageManager methods: `fetchThumb`, `exportAssetToFile`, `fetchFullSizeImageFile`.
3537
- Fixed PHAssetResourceManager methods: `fetchVideoResourceToFile`, `fetchOriginImageFile`.

0 commit comments

Comments
 (0)