Thread Sanitizer flags singleton as data race

Hi,


I get a data race error for a singleton.

It is implemented exactly as described in the WWDC session about thread sanitizer as the "correct implementation":

How can I fix it?


+ (SomeSingleton *)sharedSessionContext {
    static SomeSingleton *sharedInstance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        sharedInstance = [[SomeSingleton alloc] initSingleton];
    });
    return sharedInstance;
}


The Thread sanitizer emits:


WARNING: ThreadSanitizer: data race (pid=16374)
  Write of size 8 at 0x0001105d9818 by thread T13:
    #0 +[SomeSingleton sharedSessionContext] <null> (MyApp:x86_64+0x10226e826)
    #1 -[SomeView loadLogoLoggedIn] <null> (MyApp:x86_64+0x10098e863)
    #2 -[SomeView loadData] <null> (MyApp:x86_64+0x100989b6a)
    #3 __NSThread__start__ <null> (Foundation:x86_64+0xc024a)


  Previous write of size 8 at 0x0001105d9818 by thread T6:
    #0 +[SomeSingleton sharedSessionContext] <null> (MyApp:x86_64+0x10226e826)
    [...]
    #7 __116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke_2 <null> (AFNetworking:x86_64+0xb14d)
    #8 __72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2.123 <null> (AFNetworking:x86_64+0x4c7e1)
    #9 __wrap_dispatch_group_async_block_invoke <null> (libclang_rt.tsan_iossim_dynamic.dylib:x86_64+0x6b627)
    #10 _dispatch_client_callout <null> (libdispatch.dylib:x86_64+0x3db4)


  Location is global '__llvm_gcov_ctr.2343' at 0x0001105d9818 (MyApp+0x000107c8c818)


  Thread T13 (tid=252686, running) created by main thread at:
    #0 pthread_create <null> (libclang_rt.tsan_iossim_dynamic.dylib:x86_64+0x2912d)
    #1 -[NSThread start] <null> (Foundation:x86_64+0xbfd66)
  [...]
    #7 -[MyViewController viewWillAppear:] <null> (Base:x86_64+0x1a658e)
    #8 -[UIViewController _setViewAppearState:isAnimating:] <null> (UIKitCore:x86_64+0x3530f2)
    #9 start <null> (libdyld.dylib:x86_64+0x1540)


  Thread T6 (tid=252621, running) is a GCD worker thread


SUMMARY: ThreadSanitizer: data race ... in +[SomeSingleton sharedSessionContext]

Replies

Aren’t we already discuss this over here?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Sorry, I couldn't re-find my question there.

And yes, the problem is due to conficting buid settings (thread sanitizer vs. generate coverage information)


- Thanks, Thomas