lib: init pthread key before using thread-local storages
Without this, calling flb_stop() causes a segmentation fault in
the main thread, trying to access TLS with an uninitialized key.
The "pthread_once" pattern is required according to Open Group,
because of pthread_key_created() not being idempotent.
#include <mcheck.h>
#endif
+/* thread initializator */
+static pthread_once_t flb_lib_once = PTHREAD_ONCE_INIT;
+
#ifdef FLB_SYSTEM_WINDOWS
static inline int flb_socket_init_win32(void)
{
@@ -440,6 +443,8 @@ int flb_start(flb_ctx_t *ctx)
struct mk_event *event;
struct flb_config *config;
+ pthread_once(&flb_lib_once, flb_thread_prepare);
+
config = ctx->config;
ret = mk_utils_worker_spawn(flb_lib_worker, config, &tid);
if (ret == -1) {
