• Michał Kępień's avatar
    Move xmlInitThreads()/xmlCleanupThreads() calls · b425b5d5
    Michał Kępień authored
    xmlInitThreads() and xmlCleanupThreads() are called from within
    named_statschannels_configure() and named_statschannels_shutdown(),
    respectively.  Both of these functions are executed by worker threads,
    not the main named thread.  This causes ASAN to report memory leaks like
    the following one upon shutdown (as long as named is asked to produce
    any XML output over its configured statistics channels during its
    lifetime):
    
        Direct leak of 968 byte(s) in 1 object(s) allocated from:
            #0 0x7f677c249cd8 in __interceptor_calloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:153
            #1 0x7f677bc1838f in xmlGetGlobalState (/usr/lib/libxml2.so.2+0xa838f)
    
    The data mentioned in the above report is a libxml2 state structure
    stored as thread-specific data.  Such chunks of memory are automatically
    released (by a destructor passed to pthread_key_create() by libxml2)
    whenever a thread that allocated a given chunk exits.  However, if
    xmlCleanupThreads() is called by a given thread before it exits, the
    destructor will not be invoked (due to xmlCleanupThreads() calling
    pthread_key_delete()) and ASAN will report a memory leak.  Thus,
    xmlInitThreads() and xmlCleanupThreads() must not be called from worker
    threads.  Since xmlInitThreads() must be called on Windows in order for
    libxml2 to work at all, move xmlInitThreads() and xmlCleanupThreads()
    calls to the main named thread (which does not produce any XML output
    itself) in order to prevent the memory leak from being reported by ASAN.
    b425b5d5
main.c 41.6 KB