Skip to content

Commit c30c4b5

Browse files
committed
coro: protect first initialization of coroutine with new API (#3055)
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>
1 parent d5e5882 commit c30c4b5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

include/fluent-bit/flb_coro.h

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ static FLB_INLINE void flb_coro_destroy(struct flb_coro *coro)
9191
#define flb_coro_return(th) co_switch(th->caller)
9292

9393
void flb_coro_init();
94+
void flb_coro_thread_init();
95+
9496
struct flb_coro *flb_coro_get();
9597
void flb_coro_set(struct flb_coro *coro);
9698

src/flb_coro.c

+14
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,23 @@
2424

2525
FLB_TLS_DEFINE(struct flb_coro, flb_coro_key);
2626

27+
static pthread_mutex_t coro_mutex_init;
28+
2729
void flb_coro_init()
2830
{
2931
FLB_TLS_INIT(flb_coro_key);
32+
pthread_mutex_init(&coro_mutex_init, NULL);
33+
}
34+
35+
void flb_coro_thread_init()
36+
{
37+
size_t s;
38+
cothread_t th;
39+
40+
pthread_mutex_lock(&coro_mutex_init);
41+
th = co_create(256, NULL, &s);
42+
co_delete(th);
43+
pthread_mutex_unlock(&coro_mutex_init);
3044
}
3145

3246
struct flb_coro *flb_coro_get()

0 commit comments

Comments
 (0)