avahi icon indicating copy to clipboard operation
avahi copied to clipboard

libdns_sd: Avoid the race reported by ThreadSanitizer

Open arrowd opened this issue 4 weeks ago • 5 comments

The thread_running field is set to 1 twice - from the main thread after calling pthread_create() and from the spawned thread. This isn't really a race, because we're setting it to the same value, but these concurrent writes confuse ThreadSanitizer.

arrowd avatar Nov 30 '25 16:11 arrowd

Let's discuss the fate of those libraries in https://github.com/avahi/avahi/pull/779 first. It should be decided whether they are properly maintained or they are abandoned.

Other than that it still fails under TSan

==================                                                                                                                                            
WARNING: ThreadSanitizer: data race (pid=436253)                                                                                                              
  Read of size 8 at 0x724000000018 by main thread:                                                                                                            
    #0 sdref_free /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:426:9 (null-test+0x4bdc33) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)          
    #1 sdref_unref /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:467:9 (null-test+0x4b97b9) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)         
    #2 DNSServiceRefDeallocate /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:526:9 (null-test+0x4b9801) (BuildId: 38bda71bab59576f4721970f13e4bed715cf97
9a)                                                                                                                                                           
    #3 DNSServiceRegister /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:1204:9 (null-test+0x4bc552) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a) 
    #4 main /home/vagrant/avahi/avahi-compat-libdns_sd/null-test.c:48:5 (null-test+0x4be766) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)              
                                                                                                                                                              
  Previous write of size 8 at 0x724000000018 by thread T1:                                                                                                    
    #0 thread_func /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:312:19 (null-test+0x4be0db) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)        
                                                                                                                                                              
  Location is heap block of size 248 at 0x724000000000 allocated by main thread:                                                                              
    #0 calloc <null> (null-test+0x4336fb) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)                                                                 
    #1 xcalloc /home/vagrant/avahi/avahi-common/malloc.c:95:15 (libavahi-common.so.3+0xcfd) (BuildId: 2fb2b0799bbba3c5309c6e930f45cb957e3590dc)               
    #2 avahi_malloc0 /home/vagrant/avahi/avahi-common/malloc.c:120:16 (libavahi-common.so.3+0xb94) (BuildId: 2fb2b0799bbba3c5309c6e930f45cb957e3590dc)        
    #3 avahi_new0_internal /home/vagrant/avahi/avahi-compat-libdns_sd/../avahi-common/malloc.h:59:12 (null-test+0x4bdf2e) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)
    #4 sdref_new /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:368:19 (null-test+0x4ba0ee) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)
    #5 DNSServiceRegister /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:1127:19 (null-test+0x4bbe7b) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)
    #6 main /home/vagrant/avahi/avahi-compat-libdns_sd/null-test.c:48:5 (null-test+0x4be766) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)

  Thread T1 (tid=436268, finished) created by main thread at:
    #0 pthread_create <null> (null-test+0x43531e) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)
    #1 sdref_new /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:405:9 (null-test+0x4ba47c) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)
    #2 DNSServiceRegister /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:1127:19 (null-test+0x4bbe7b) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)
    #3 main /home/vagrant/avahi/avahi-compat-libdns_sd/null-test.c:48:5 (null-test+0x4be766) (BuildId: 38bda71bab59576f4721970f13e4bed715cf979a)

SUMMARY: ThreadSanitizer: data race /home/vagrant/avahi/avahi-compat-libdns_sd/compat.c:426:9 in sdref_free
==================

evverx avatar Nov 30 '25 17:11 evverx

That's a different one, so this PR actually fixes a problem. But since you're reluctant to get these in, I'll stop there.

arrowd avatar Nov 30 '25 17:11 arrowd

you're reluctant to get these in

@pemensik can have a different opinion so let's keep these PRs open anyway. In my opinion in its current form those things should be left alone and declared unmaintained.

evverx avatar Nov 30 '25 17:11 evverx

Hmm, this is a strange change. It removes assigning running mark from thread, but does not move it to any better place. Does it make indication of running non-functional completely? In that case it should remove the variable completely.

pemensik avatar Dec 01 '25 09:12 pemensik

It is already in a better place: https://github.com/arrowd/avahi/blob/7c7ecca8066f0ac3e9de118cb0217dcc96ec2761/avahi-compat-libdns_sd/compat.c#L408

Before this change this field was being assigned twice.

arrowd avatar Dec 01 '25 09:12 arrowd