Skip to content

Commit

Permalink
coro: protect first initialization of coroutine with new API (#3055)
Browse files Browse the repository at this point in the history
When libco starts, it might enter in a race condition if multiple
threads are trying to initialize the 'co_swap' function, this check
is done on every coroutine creation:

  ==346246== Possible data race during read of size 8 at 0x5CA890 by thread #5
  ==346246== Locks held: none
  ==346246==    at 0x48EFAE: co_create (amd64.c:132)
  ==346246==    by 0x173035: flb_output_coro_create (flb_output.h:511)
  ==346246==    by 0x173035: output_thread (flb_output_thread.c:281)
  ==346246==    by 0x1889BE: step_callback (flb_worker.c:44)
  ==346246==    by 0x4843B1A: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_helgrind-amd64-linux.so)
  ==346246==    by 0x487E58F: start_thread (pthread_create.c:463)
  ==346246==    by 0x4F47222: clone (clone.S:95)
  ==346246==
  ==346246== This conflicts with a previous write of size 8 by thread #4
  ==346246== Locks held: none
  ==346246==    at 0x48EFCB: co_create (amd64.c:134)
  ==346246==    by 0x173035: flb_output_coro_create (flb_output.h:511)
  ==346246==    by 0x173035: output_thread (flb_output_thread.c:281)
  ==346246==    by 0x1889BE: step_callback (flb_worker.c:44)
  ==346246==    by 0x4843B1A: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_helgrind-amd64-linux.so)
  ==346246==    by 0x487E58F: start_thread (pthread_create.c:463)
  ==346246==    by 0x4F47222: clone (clone.S:95)
  ==346246==  Address 0x5ca890 is 0 bytes inside data symbol "co_swap"

This patch introduce a new API for flb_coro interface that aims to
be called inside every worker thread. The access to this first
initialization is protected.

No more race conditions on that piece of code has been seen with valgrind
after the usage of this new function (next patches).

Signed-off-by: Eduardo Silva <eduardo@treasure-data.com>
  • Loading branch information
edsiper committed Mar 2, 2021
1 parent d5e5882 commit c30c4b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/fluent-bit/flb_coro.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ static FLB_INLINE void flb_coro_destroy(struct flb_coro *coro)
#define flb_coro_return(th) co_switch(th->caller)

void flb_coro_init();
void flb_coro_thread_init();

struct flb_coro *flb_coro_get();
void flb_coro_set(struct flb_coro *coro);

Expand Down
14 changes: 14 additions & 0 deletions src/flb_coro.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,23 @@

FLB_TLS_DEFINE(struct flb_coro, flb_coro_key);

static pthread_mutex_t coro_mutex_init;

void flb_coro_init()
{
FLB_TLS_INIT(flb_coro_key);
pthread_mutex_init(&coro_mutex_init, NULL);
}

void flb_coro_thread_init()
{
size_t s;
cothread_t th;

pthread_mutex_lock(&coro_mutex_init);
th = co_create(256, NULL, &s);
co_delete(th);
pthread_mutex_unlock(&coro_mutex_init);
}

struct flb_coro *flb_coro_get()
Expand Down

0 comments on commit c30c4b5

Please sign in to comment.