Skip to content

assertion failed: "theap->tld->thread_id == 0 || theap->tld->thread_id == tid" #1231

@hbirler

Description

@hbirler

I was trying the new first class heaps (mimalloc 3.2.8, installed via vcpkg), but I encountered some issues.

I can reliably get the following error in debug mode:

assertion failed: at "alloc.c":136, mi_theap_malloc_small_zero_nonnull
assertion: "theap->tld->thread_id == 0 || theap->tld->thread_id == tid"

In release mode, a SIGSEGV is triggered.

My reproducer

#include <cassert>
#include <cstdlib>
#include <print>
#include <thread>
#include <vector>
#include <mimalloc.h>
#include <barrier>

using namespace std;

// We repeatedly
// * (Destroy previous heap)
// * Create a new heap
// * Allocate from the heap with threads
// This seems to trigger assertions or segfaults in mimalloc
int main()
{
    int numWorkers = 32;
    std::barrier startBar(numWorkers + 1);
    std::barrier doneBar(numWorkers + 1);
    vector<jthread> threads;
    mi_heap_t* heap = nullptr;
    for (int i = 0; i < numWorkers; i++)
    {
        threads.emplace_back([&startBar, &doneBar, &heap](std::stop_token tok)
        {
            while (true)
            {
                startBar.arrive_and_wait();
                if (tok.stop_requested())
                    return;
                mi_heap_t* localHeap = heap;
                if (true)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        assert(localHeap);
                        auto* ptr = mi_heap_malloc(localHeap, 17);
                        mi_free(ptr);
                    }
                }
                doneBar.arrive_and_wait();
            }
        });
    }

    for (size_t j = 0; j < 100; j++)
    {
        std::print(stderr, "Round {}\n", j);
        if (heap)
            mi_heap_destroy(heap);
        heap = mi_heap_new();
        startBar.arrive_and_wait();
        doneBar.arrive_and_wait();
    }

    for (auto& t : threads)
        t.request_stop();

    startBar.arrive_and_wait();

    return 0;
}

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