confluentinc / librdkafka

The Apache Kafka C/C++ library
Other
7.37k stars 3.11k forks source link

when using librdkafka, create a thread will fail. #4654

Closed hnlylyb closed 1 month ago

hnlylyb commented 3 months ago

Description

when using librdkafka, use pthread to create a thread will be failed.

How to reproduce

#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <librdkafka/rdkafka.h>
#include <librdkafka/rdkafkacpp.h>

static void* msec_thread(void*)
{
        return nullptr;
}

static void slack_init()
{
        int rc;
        pthread_attr_t attr;
        pthread_t thr;

        pthread_attr_init(&attr);
        pthread_attr_setstacksize(&attr, 16 * 1024);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        rc = pthread_create(&thr, &attr, msec_thread, NULL);
        pthread_attr_destroy(&attr);
        printf("rc:%d\n", rc);
}

int main(int argc, char **argv) 
{
        slack_init();
        RdKafka::Conf *conf = RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL);
        delete conf;
        return 0;
}

using above code, it cannot create the thread, and print rc:22, but if I comment the code about rdkafka, it will print rc:0.

Checklist

librdkafka version: Operating system: ubuntu22.04

emasab commented 1 month ago

When linking with librdkafka the runtime needs to allocate some thread local storage defined in librdkafka, that is added to all the threads. That is put in the same memory area and increases the stack size, overflowing the limit of 16 KiB, if you put a larger limit, starting from 24 KiB, it creates the thread successfully.