Skip to content

Data race in lazy init of theap in heap.c line 52 #1230

@AnneJanB

Description

@AnneJanB

In a multithreaded environment we encountered a data race condition for the lazy init of theap (in heap.c line 52).
In our case this caused an access violation while reading memory.
Suggestion:
The [heap->theap]field should either be:

  • atomic or
  • protected by [heap->theaps_lock] or
  • the init should be done in the mi_heap_new() factory.

The race is narrow but real in thread-pool scenarios where multiple threads start allocating from a shared heap simultaneously.
The issue can be circumvented by allocating some memory when the mi_heap is created. E.g.
m_mi_heap = mi_heap_new(); if (m_mi_heap) { // Force theap TLS key initialization on the creating thread // to avoid a race when workers first use this heap concurrently void* dummy = mi_heap_malloc(m_mi_heap, 1); mi_free(dummy); }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions