0

I ran a test code with ThreadSanitizer, it throws a race condition in the block of xpc_connection_set_event_handler even I set a sync queue to the connection.

 xpc_connection_set_event_handler(conn, ^(xpc_object_t event) {
 xpc_type_t type = xpc_get_type(event);

 if (xpc_get_type(event) == XPC_TYPE_DICTIONARY) {
    // race condition here
    // WARNING: ThreadSanitizer: data race
    // ...
 } else {
     if (event == XPC_ERROR_CONNECTION_INVALID) {
        // Error indicates the peer has closed the connection.
        // Tear down any associated data structures.
     } else {
        // Error indicates that service will terminate soon.
        // Flush all buffers, finish all work, etc.
     }
     xpc_release(peer);
 }
 });

https://developer.apple.com/documentation/xpc/1448786-xpc_connection_set_target_queue?language=objc: "the XPC runtime guarantees that, when the target queue is a serial queue, the event handler block will execute synchronously with respect to other blocks submitted to that same queue. When the target queue is a concurrent queue, the event handler block may run concurrently with other blocks submitted to that queue, but it will never run concurrently with other invocations of itself for the same connection"

looks like there should have no race condition according to the above description, but why I encounter the race condition issue reported by ThreadSanitizer?

Does the block of xpc_connection_set_event_handler need sync to avoid race condition?

  • What do you mean by "I ran code with ThreadSanitizer, it throws there is a race condition in the block of xpc_connection_set_event_handler even I use sync queue"? That's not a grammatically-correct English sentence. – El Tomato Dec 21 '20 at 10:04
  • @ElTomato, I attached the sample code – android2test Dec 21 '20 at 10:19

0 Answers0