confluentinc / librdkafka

The Apache Kafka C/C++ library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

when using librdkafka, create a thread will fail.

hnlylyb opened this issue · comments

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

  • librdkafka version (release number or git tag): v2.3.0
  • Apache Kafka version: N/A
  • librdkafka client configuration: N/A
  • Operating system: ubuntu 22.04
  • Provide logs (with debug=.. as necessary) from librdkafka
  • Provide broker log excerpts
  • Critical issue

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.