Hi, recently some users of my app upgrade MacOS to 15, and encounter hang when trying to get screen capture permission. After looking into this issue, I find out a repoducible way. If two processes of a same program both call CGDisplayStreamCreate/CGDisplayImageCreate API, the later one will hang(No need to be at almost same time).
Here are my demo codes.
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>
int main(int argc, const char *argv[]) {
@autoreleasepool {
CGDirectDisplayID displayID = CGMainDisplayID();
CGDisplayStreamRef stream = CGDisplayStreamCreate(
displayID, 1, 1, kCVPixelFormatType_32BGRA, nil,
^(CGDisplayStreamFrameStatus status, uint64_t displayTime,
IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef){
});
if (!stream) {
NSLog(@"Failed to create display stream!");
return 1;
} else {
NSLog(@"Create display stream success!");
CFRelease(stream);
}
}
sleep(10);
return 0;
}
The call of sleep is just to ensure I can run a second process of this program before the first one ends. It turns out that the first process checks the permission correctly and sleeps successfully. However the second process won't be able to print anything because it hangs when calling CGDisplayStreamCreate. Here is the output of sample:
Call graph:
2546 Thread_1315735 DispatchQueue_1: com.apple.main-thread (serial)
+ 2546 start (in dyld) + 2840 [0x196f8f274]
+ 2546 main (in testapp) + 76 [0x100ef7e38]
+ 2546 SLDisplayStreamCreate (in SkyLight) + 316 [0x19d38163c]
+ 2546 SLSDisplayStreamCreateProxying (in ScreenCaptureKit) + 816 [0x231e2d614]
+ 2546 _dispatch_semaphore_wait_slow (in libdispatch.dylib) + 76 [0x19715e278]
+ 2546 _dispatch_sema4_timedwait (in libdispatch.dylib) + 64 [0x19715dc78]
+ 2546 semaphore_timedwait_trap (in libsystem_kernel.dylib) + 8 [0x1972ceda8]
2546 Thread_1315737
2546 start_wqthread (in libsystem_pthread.dylib) + 8 [0x19730b0f0]
2546 _pthread_wqthread (in libsystem_pthread.dylib) + 364 [0x19730c424]
2546 __workq_kernreturn (in libsystem_kernel.dylib) + 8 [0x1972d0a64]
If I copy the program and run two different programs, the second program won't hang. This issue doesn't happen on MacOS early version. Can anybody help?