signals raised from dispatch_get_main_queue are not caught when CFRunLoop is used (vs dispatch_main)

Originator:dmaclach
Number:rdar://46008942 Date Originated:12-Nov-2018 04:28 PM
Status:Open Resolved:
Product:macOS + SDK Product Version:10.13.6
Classification:Serious Bug Reproducible:Always
 
Summary:
Signals raised from a block being dispatched on dispatch_get_main_queue are not handled by a signal handler registered via libdispatch if underlying runloop is CFRunLoop. They ARE caught if the underlying runloop is dispatch_main()

I have seen no documentation that would lead me to believe that this is expected.

Signals raised from other global dispatch queues are caught in both cases.

This appears to be due to a difference in how pthread_sigmask is set when the block is executed using CFRunLoop vs dispatch_main().


Steps to Reproduce:
1) Build attached code 
  clang /Users/dmaclach/Desktop/signaltest/signaltest/main.m -framework Foundation
2) Run it
  ./a.out

CFRunLoopRun: 0x7fff94bd8380 SIGUSR1 thread: UNBLOCKED proc: Ignored
Source Registered
Raising From Global Queue
globalQueue: 0x70000fda2000 SIGUSR1 thread: BLOCKED proc: Ignored
Raised From Global Queue
Received Signal
Signal: 0x7fff94bd8380 SIGUSR1 thread: UNBLOCKED proc: Ignored
Raising From Main Queue
mainQueue: 0x7fff94bd8380 SIGUSR1 thread: UNBLOCKED proc: Ignored
Raised From Main Queue

Note that the second exception raised from the main queue was not received

3) Comment out line 61
4) Uncomment line 62
5) Build attached code 
  clang /Users/dmaclach/Desktop/signaltest/signaltest/main.m -framework Foundation
6) Run it
  ./a.out

dispatch_main: 0x7fff94bd8380 SIGUSR1 thread: UNBLOCKED proc: Ignored
Source Registered
Raising From Global Queue
globalQueue: 0x70000c81b000 SIGUSR1 thread: BLOCKED proc: Ignored
Raised From Global Queue
Received Signal
Signal: 0x70000c89e000 SIGUSR1 thread: BLOCKED proc: Ignored
Raising From Main Queue
mainQueue: 0x70000c89e000 SIGUSR1 thread: BLOCKED proc: Ignored
Raised From Main Queue
Received Signal
Signal: 0x70000c89e000 SIGUSR1 thread: BLOCKED proc: Ignored

Note that signal was caught.

Expected Results:
I would expect the signal dispatched from dispatch_get_main_queue() to be caught in both cases, or an exception of some sort to be logged to tell me that this is not supported. Documentation explaining this somewhere would be a bonus!

Actual Results:
Signal is caught when dispatch_main executes it. Signal is not caught when CFRunLoop executes it. Signal Handlers installed using sigaction/signal work as expected in both cases.

Version:
10.13.6

Notes:
GOOGLER:dmaclach

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!