304e9ab6e3a62d7863b144478fc1ac621623a617
braney
  Wed Jul 29 11:42:01 2026 -0700
Fix data race on the chromAlias caches that was crashing hgTracks in parallel decorator loads.

chromAliasFindNative() and chromAliasFindAliases() lazily created their static
cache hashes and did their first lookup outside the mutex, taking the lock only
for the miss path.  hgTracks loads decorators on real pthreads, so two threads
arriving before a cache existed could race on it and SIGSEGV in hashLookup.
Move the lock above the lazy init and the first lookup so the whole cache
access is inside it.

Both paths of chromAliasFindNative() now return an allocated string rather than
the cache-owned pointer, so document that the caller owns the result, free it in
decorationNativeItem() (called once per decoration), and drop the redundant
outer cloneString() in hgOfficialChromName().

refs #37955

diff --git src/hg/hgTracks/decorator.c src/hg/hgTracks/decorator.c
index 289c9d72d56..a6a4e13ecfa 100644
--- src/hg/hgTracks/decorator.c
+++ src/hg/hgTracks/decorator.c
@@ -855,32 +855,31 @@
 if (firstColon == NULL)
     return cloneString(decoratedItem);   // not in expected format; leave alone
 char *chrom = cloneStringZ(decoratedItem, firstColon - decoratedItem);
 char *native = chromAliasFindNative(chrom);
 char *result;
 if (native != NULL && !sameString(native, chrom))
     {
     struct dyString *dy = dyStringNew(0);
     dyStringAppend(dy, native);
     dyStringAppend(dy, firstColon);       // firstColon still points at the leading ':'
     result = dyStringCannibalize(&dy);
     }
 else
     result = cloneString(decoratedItem);
 freeMem(chrom);
-// note: do not free the chromAliasFindNative() result - on a cache hit it returns
-// the pointer owned by its internal cache, so freeing it would corrupt that cache.
+freeMem(native);
 return result;
 }
 
 
 void addToDecorator(struct decorator *decorator, struct decoration *decoration, char *mouseOverText)
 /* Small function that just gets repeated a lot - add the decoration to the decorator and
  * set its mouseOver text.
  */
 {
 if (decorator == NULL || decoration == NULL)
     return;
 slAddHead(&decorator->decorations, decoration);
 hashAdd(decorator->hash, decoration->decoratedItem, decoration);
 if (isNotEmpty(mouseOverText))
     {