From 171b6e12429702e86d472cf0f4b4e2efdda91b5e Mon Sep 17 00:00:00 2001 From: estebanpavansarquis <11792494+estebanpavansarquis@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:57:57 -0300 Subject: [PATCH 1/3] output: create the thread pool once per instance For proxy (Go) output plugins with workers > 0, flb_output_enable_multi_threading() runs twice for the same instance: from flb_plugin_proxy_output_init() and again from flb_output_init_all() after cb_init. flb_output_thread_pool_create() overwrites ins->tp, so the workers of the first pool are never stopped or joined by flb_output_thread_pool_destroy(). With hot_reload on, those workers keep running after a reload frees the old config and log context. Their timers then read the freed log context and Fluent Bit crashes with SIGSEGV. Return early when the instance already has a thread pool. ins->tp is NULL on a new instance and is reset to NULL when the pool is destroyed. Signed-off-by: estebanpavansarquis <11792494+estebanpavansarquis@users.noreply.github.com> --- src/flb_output.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/flb_output.c b/src/flb_output.c index 73b26358f2b..2c4bd2de771 100644 --- a/src/flb_output.c +++ b/src/flb_output.c @@ -1842,6 +1842,16 @@ int flb_output_enable_multi_threading(struct flb_output_instance *ins, struct fl { /* Multi-threading enabled ? (through 'workers' property) */ if (ins->tp_workers > 0) { + /* + * Proxy (Go) outputs reach this function twice: from the proxy + * cb_init and from flb_output_init_all(). The pool is created once + * per instance, so flb_output_thread_pool_destroy() stops and joins + * every worker thread. + */ + if (ins->tp) { + return 0; + } + if(flb_output_thread_pool_create(config, ins) != 0) { flb_output_instance_destroy(ins); return -1; From f480d7dc15355e1d13424e1497f4a64b57295fc5 Mon Sep 17 00:00:00 2001 From: estebanpavansarquis <11792494+estebanpavansarquis@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:25:23 -0300 Subject: [PATCH 2/3] tests: runtime_shell: cover Go output workers with hot reload Run two Go proxy outputs with workers 1 and hot_reload on, reload twice and stop Fluent Bit. The test checks that each output instance starts one worker per context, that Fluent Bit keeps running after each reload, that it exits cleanly, and that every started worker is stopped. Signed-off-by: estebanpavansarquis <11792494+estebanpavansarquis@users.noreply.github.com> --- .../conf/proxy_logs_workers_reload.conf | 22 +++++ tests/runtime_shell/proxy_logs_expect.sh | 93 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/runtime_shell/conf/proxy_logs_workers_reload.conf diff --git a/tests/runtime_shell/conf/proxy_logs_workers_reload.conf b/tests/runtime_shell/conf/proxy_logs_workers_reload.conf new file mode 100644 index 00000000000..b0f904b0d61 --- /dev/null +++ b/tests/runtime_shell/conf/proxy_logs_workers_reload.conf @@ -0,0 +1,22 @@ +[SERVICE] + Flush 1 + Grace 2 + Log_Level info + Daemon Off + Hot_Reload On + +[INPUT] + Name dummy + Dummy {"message": "test log entry", "level": "info"} + Rate 5 + Tag test.logs + +[OUTPUT] + Name test_logs_go + Match test.logs + Workers 1 + +[OUTPUT] + Name test_logs_go + Match test.logs + Workers 1 diff --git a/tests/runtime_shell/proxy_logs_expect.sh b/tests/runtime_shell/proxy_logs_expect.sh index 37dd8ef58ed..20ede3e7b12 100755 --- a/tests/runtime_shell/proxy_logs_expect.sh +++ b/tests/runtime_shell/proxy_logs_expect.sh @@ -62,5 +62,98 @@ test_proxy_logs_compatibility() { return "$FLB_STATUS" } +# Count the lines in a file that contain a fixed string. +count_lines() { + grep -Fc "$1" "$2" || : +} + +# Wait until a fixed string appears a number of times in a file, while the +# process is alive. Return 1 on timeout or when the process exits. +wait_for_count() { + WAIT_PATTERN=$1 + WAIT_COUNT=$2 + WAIT_FILE=$3 + WAIT_PID=$4 + WAIT_SECONDS=0 + while [ "$(count_lines "$WAIT_PATTERN" "$WAIT_FILE")" -lt "$WAIT_COUNT" ]; do + if ! kill -0 "$WAIT_PID" 2>/dev/null || [ "$WAIT_SECONDS" -ge 30 ]; then + return 1 + fi + WAIT_SECONDS=$((WAIT_SECONDS + 1)) + sleep 1 + done + return 0 +} + +# Go outputs with workers get one thread pool per instance, and hot reload +# stops every worker of the old context before it is freed. +test_proxy_output_workers_reload() { + # build_test_plugins.sh enables errexit, and a failed shunit2 assertion + # returns non-zero. Run without it so every assertion reports and + # Fluent Bit is always stopped. + set +e + + STDOUT_OUTPUT_FILE="/tmp/test_logs_workers_reload_$$.txt" + OUTPUTS=2 + RELOADS=2 + STARTED="worker #0 started" + STOPPED="thread worker #0 stopped" + RELOADED="successful reload done" + + : > "$STDOUT_OUTPUT_FILE" + + "$FLB_BIN" -e "$FLB_ROOT/build/test_logs_go.so" -c "$FLB_RUNTIME_SHELL_CONF/proxy_logs_workers_reload.conf" > "$STDOUT_OUTPUT_FILE" 2>&1 & + FLB_PID=$! + + if ! wait_for_count "$STARTED" "$OUTPUTS" "$STDOUT_OUTPUT_FILE" "$FLB_PID"; then + fail "Go output workers did not start" + fi + # Let every thread pool of the first context report its workers. + sleep 2 + assertEquals "one worker per Go output instance after startup" \ + "$OUTPUTS" "$(count_lines "$STARTED" "$STDOUT_OUTPUT_FILE")" + + RELOAD=1 + while [ "$RELOAD" -le "$RELOADS" ]; do + kill -HUP "$FLB_PID" 2>/dev/null + if ! wait_for_count "$RELOADED" "$RELOAD" "$STDOUT_OUTPUT_FILE" "$FLB_PID"; then + fail "reload $RELOAD did not complete" + fi + # Give the timers of any worker left from the old context time to run. + sleep 3 + kill -0 "$FLB_PID" 2>/dev/null + assertEquals "Fluent Bit is running after reload $RELOAD" 0 "$?" + RELOAD=$((RELOAD + 1)) + done + + kill -TERM "$FLB_PID" 2>/dev/null + SHUTDOWN_WAIT=0 + while kill -0 "$FLB_PID" 2>/dev/null; do + if [ "$SHUTDOWN_WAIT" -ge 15 ]; then + fail "Fluent Bit shutdown exceeded 15 seconds" + kill -KILL "$FLB_PID" 2>/dev/null + break + fi + SHUTDOWN_WAIT=$((SHUTDOWN_WAIT + 1)) + sleep 1 + done + wait "$FLB_PID" + FLB_STATUS=$? + STARTED_COUNT=$(count_lines "$STARTED" "$STDOUT_OUTPUT_FILE") + STOPPED_COUNT=$(count_lines "$STOPPED" "$STDOUT_OUTPUT_FILE") + EXPECTED_COUNT=$((OUTPUTS * (RELOADS + 1))) + + assertEquals "Fluent Bit exits cleanly" 0 "$FLB_STATUS" + assertEquals "one worker per Go output instance per context" \ + "$EXPECTED_COUNT" "$STARTED_COUNT" + assertEquals "every started worker is stopped" \ + "$STARTED_COUNT" "$STOPPED_COUNT" + + # Print the Fluent Bit log without the records written by the Go output. + grep -v '^map\[' "$STDOUT_OUTPUT_FILE" + + rm -f "$STDOUT_OUTPUT_FILE" +} + # Load the runtime shell environment . "$FLB_RUNTIME_SHELL_PATH/runtime_shell.env" From 5197e8fc7666beab0c0b4dfae245246fbce762bc Mon Sep 17 00:00:00 2001 From: estebanpavansarquis <11792494+estebanpavansarquis@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:44:04 -0300 Subject: [PATCH 3/3] tests: runtime_shell: create the workers reload log with mktemp Signed-off-by: estebanpavansarquis <11792494+estebanpavansarquis@users.noreply.github.com> --- tests/runtime_shell/proxy_logs_expect.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/runtime_shell/proxy_logs_expect.sh b/tests/runtime_shell/proxy_logs_expect.sh index 20ede3e7b12..f957f19427f 100755 --- a/tests/runtime_shell/proxy_logs_expect.sh +++ b/tests/runtime_shell/proxy_logs_expect.sh @@ -93,15 +93,16 @@ test_proxy_output_workers_reload() { # Fluent Bit is always stopped. set +e - STDOUT_OUTPUT_FILE="/tmp/test_logs_workers_reload_$$.txt" + if ! STDOUT_OUTPUT_FILE=$(mktemp "${TMPDIR:-/tmp}/test_logs_workers_reload.XXXXXX"); then + fail "could not create the Fluent Bit log file" + return 1 + fi OUTPUTS=2 RELOADS=2 STARTED="worker #0 started" STOPPED="thread worker #0 stopped" RELOADED="successful reload done" - : > "$STDOUT_OUTPUT_FILE" - "$FLB_BIN" -e "$FLB_ROOT/build/test_logs_go.so" -c "$FLB_RUNTIME_SHELL_CONF/proxy_logs_workers_reload.conf" > "$STDOUT_OUTPUT_FILE" 2>&1 & FLB_PID=$!