Skip to content

TSan false positive with dispatch_apply() over an Objective-C NSArray (on macOS) #61262

Open
@seanm

Description

@seanm

On macOS 12.x and 13.x on both x86_64 and Arm64 with Xcode this simple code in a toy app:

NSArray* array = @[@1, @2, @3, @4, @5, @6, @7];

dispatch_apply([array count], DISPATCH_APPLY_AUTO, ^(size_t idx) {
	NSNumber* unused = array[idx]; // TSan says race here?!
	(void)unused;
});

Causes a TSan warning:

WARNING: ThreadSanitizer: data race (pid=33556)
  Read of size 8 at 0x00010b863380 by thread T6:
    #0 __45-[AppDelegate applicationDidFinishLaunching:]_block_invoke <null> (TSanTest:arm64+0x100002d4c)
    #1 __wrap_dispatch_apply_block_invoke <null> (libclang_rt.tsan_osx_dynamic.dylib:arm64+0x7db4c)
    #2 _dispatch_client_callout2 <null> (libdispatch.dylib:arm64e+0x6a10)

  Previous write of size 8 at 0x00010b863380 by main thread:
    #0 __copy_helper_block_e8_32s <null> (TSanTest:arm64+0x100002874)
    #1 _call_copy_helpers_excp <null> (libsystem_blocks.dylib:arm64e+0x2120)
    #2 -[AppDelegate applicationDidFinishLaunching:] <null> (TSanTest:arm64+0x100002c9c)
    #3 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ <null> (CoreFoundation:arm64e+0x7655c)
    #4 <null> <null> (0x0001b16b7e50)

  Location is heap block of size 40 at 0x00010b863360 allocated by main thread:
    #0 malloc <null> (libclang_rt.tsan_osx_dynamic.dylib:arm64+0x54e90)
    #1 _Block_copy <null> (libsystem_blocks.dylib:arm64e+0x1b30)
    #2 -[AppDelegate applicationDidFinishLaunching:] <null> (TSanTest:arm64+0x100002c9c)
    #3 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ <null> (CoreFoundation:arm64e+0x7655c)
    #4 <null> <null> (0x0001b16b7e50)

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

I'm 99% sure there's no bug in my example code above.

I figure this is either:

  1. A TSan false positive, hence this ticket
  2. a macOS bug (I filed FB9995377 too)

(I'd appreciate if anyone knows which.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions