Skip to content

tests: runtime: stabilize http client chunked test#11723

Closed
edsiper wants to merge 1 commit intomasterfrom
fix-http-client-test
Closed

tests: runtime: stabilize http client chunked test#11723
edsiper wants to merge 1 commit intomasterfrom
fix-http-client-test

Conversation

@edsiper
Copy link
Copy Markdown
Member

@edsiper edsiper commented Apr 16, 2026


Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • Tests
    • Improved HTTP client test suite reliability by strengthening networking/DNS and chunked-transfer handling, and by disabling fragile async behavior during tests.
    • Added robust server-thread synchronization and lifecycle management to prevent race conditions and resource leaks.
    • Removed flaky assertions around certain response headers to reduce nondeterministic failures across platforms.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 16, 2026

📝 Walkthrough

Walkthrough

Added Fluent Bit network/DNS setup to the HTTP client test context, disabled upstream async mode, introduced server-thread mutex/cond synchronization to signal readiness before accept(), hardened server read/close behavior, and added proper init/cleanup of synchronization primitives in the chunked-response test.

Changes

Cohort / File(s) Summary
HTTP client chunked test
tests/runtime/http_client_chunked.c
Extended runtime_http_client_ctx with DNS/net setup and teardown; disabled upstream async mode; added mutex/condition and accepting flag to server context and signalled readiness before accept(); added read-path early-exit on read() <= 0; removed some response-header assertions; ensured init/destroy of sync primitives on all paths.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant ServerThread as Server Thread
    participant ClientCtx as HTTP Client Context
    participant NetDNS as Net/DNS

    Test->>ServerThread: start thread
    ServerThread->>ServerThread: init mutex/cv, set accepting=false
    ServerThread-->>Test: signal "ready_to_accept"
    Test->>ClientCtx: create client context
    ClientCtx->>NetDNS: flb_net_ctx_init / flb_net_dns_ctx_init / flb_net_dns_ctx_set
    ClientCtx->>ClientCtx: flb_stream_disable_async_mode
    Test->>ServerThread: connect to server (after waiting)
    ServerThread->>ServerThread: accept() -> connection
    ServerThread->>ServerThread: read request (if read() <= 0 -> close & exit)
    ServerThread->>ClientCtx: send chunked response
    ClientCtx->>Test: receive/process response
    Test->>ClientCtx: destroy client context (flb_net_dns_lookup_context_cleanup, flb_net_dns_ctx_set(NULL))
    ServerThread->>ServerThread: cleanup mutex/cv
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • cosmo0920
  • pwhelan

Poem

🐰 A rabbit's ode to threads and nets,
I signal readiness, no more frets,
DNS laid out, async tamed,
Reads now fail-safe, tests reclaimed,
Hooray — cleanup hops without regrets! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately and concisely describes the main objective: stabilizing the HTTP client chunked test through synchronization, DNS cleanup, and hardening changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-http-client-test

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Signed-off-by: Eduardo Silva <eduardo@chronosphere.io>
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
tests/runtime/http_client_chunked.c (2)

305-330: ⚠️ Potential issue | 🟠 Major

Keep trailer parsing asserted in this chunked-response test.

The server still sends Trailer: Expires, X-Trace, but the test now stops at payload verification. That turns this into a payload reassembly check only, so a regression in chunked trailer parsing can slip through unnoticed. If the old header assertions were the flaky part, I'd split them into a separate focused trailer test rather than dropping them.

As per coding guidelines, tests/**/*.{c,cpp,py}: Add or update tests for behavior changes, especially protocol parsing and encoder/decoder paths; Validate both success and failure paths in tests: invalid payloads, boundary sizes, null/missing fields.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/runtime/http_client_chunked.c` around lines 305 - 330, The test
currently only verifies payload reassembly after calling flb_http_client and
flb_http_do (using client and client->resp) and drops assertions that the
chunked response trailers were parsed; restore assertions that validate the
trailers (e.g., check client->resp.trailer or client->resp.headers for "Expires"
and "X-Trace" and their values) so the test covers trailer parsing as well as
payload reassembly, or if those header checks were flaky, move them into a new
focused test that exercises trailer parsing separately while keeping this test
focused on payload reassembly.

191-219: ⚠️ Potential issue | 🟠 Major

Add DNS context cleanup to all failure paths after line 184.

After flb_net_dns_ctx_set(&ctx->dns_ctx) on line 184, the DNS context is active and must be cleaned up on every error return. Currently:

  • Line 188 has flb_net_dns_ctx_set(NULL) but missing flb_net_dns_lookup_context_cleanup()
  • Lines 197–202 (upstream_create failure) are missing both cleanup calls
  • Lines 209–214 (upstream_conn_get failure) are missing both cleanup calls

Production code (flb_engine.c, flb_input_thread.c, flb_output_thread.c) consistently pairs both functions. Add them to all three failure paths:

Suggested fix
     ctx->config = flb_config_init();
     if (!TEST_CHECK(ctx->config != NULL)) {
+        flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
         flb_net_dns_ctx_set(NULL);
         mk_event_loop_destroy(ctx->evl);
         flb_free(ctx);
         return NULL;
     }

     ctx->u = flb_upstream_create(ctx->config, "127.0.0.1", port, 0, NULL);
     if (!TEST_CHECK(ctx->u != NULL)) {
+        flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
+        flb_net_dns_ctx_set(NULL);
         flb_config_exit(ctx->config);
         mk_event_loop_destroy(ctx->evl);
         flb_free(ctx);
         return NULL;
     }

     ctx->u_conn = flb_upstream_conn_get(ctx->u);
     if (!TEST_CHECK(ctx->u_conn != NULL)) {
         flb_upstream_destroy(ctx->u);
+        flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
+        flb_net_dns_ctx_set(NULL);
         flb_config_exit(ctx->config);
         mk_event_loop_destroy(ctx->evl);
         flb_free(ctx);
         return NULL;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/runtime/http_client_chunked.c` around lines 191 - 219, The DNS lookup
context set by flb_net_dns_ctx_set(&ctx->dns_ctx) must be cleaned up on every
early return; add calls to flb_net_dns_lookup_context_cleanup() followed by
flb_net_dns_ctx_set(NULL) in the failure branches for TEST_CHECK(ctx->config !=
NULL), the flb_upstream_create failure (ctx->u == NULL), and the
flb_upstream_conn_get failure (ctx->u_conn == NULL), ensuring you perform the
cleanup before destroying the event loop and freeing ctx to mirror the pattern
used in flb_engine.c/flb_input_thread.c.
🧹 Nitpick comments (1)
tests/runtime/http_client_chunked.c (1)

282-287: Route the pthread_create() failure through the shared teardown.

If Line 282 fails, the function returns after closing the socket but skips destroying server.cv and server.lock. Reusing cleanup: here would keep the new init/destroy pairs balanced and avoid another special-case exit.

Suggested fix
     ret = pthread_create(&server.thread, NULL, chunked_server_thread, &server);
     TEST_CHECK(ret == 0);
     if (ret != 0) {
         close(server.listen_fd);
-        return;
+        server.listen_fd = -1;
+        goto cleanup;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/runtime/http_client_chunked.c` around lines 282 - 287, The
pthread_create() failure handling in chunked_server_thread setup closes
server.listen_fd and returns early, skipping destruction of server.cv and
server.lock; change the early-return path (the block after
pthread_create(&server.thread, NULL, chunked_server_thread, &server)) to route
through the existing cleanup: label instead of returning directly — e.g., set an
error/ret value if needed and goto cleanup so the teardown that destroys
server.cv and server.lock always runs; reference pthread_create, server.thread,
chunked_server_thread, server.cv, server.lock and the cleanup: label when making
the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@tests/runtime/http_client_chunked.c`:
- Around line 305-330: The test currently only verifies payload reassembly after
calling flb_http_client and flb_http_do (using client and client->resp) and
drops assertions that the chunked response trailers were parsed; restore
assertions that validate the trailers (e.g., check client->resp.trailer or
client->resp.headers for "Expires" and "X-Trace" and their values) so the test
covers trailer parsing as well as payload reassembly, or if those header checks
were flaky, move them into a new focused test that exercises trailer parsing
separately while keeping this test focused on payload reassembly.
- Around line 191-219: The DNS lookup context set by
flb_net_dns_ctx_set(&ctx->dns_ctx) must be cleaned up on every early return; add
calls to flb_net_dns_lookup_context_cleanup() followed by
flb_net_dns_ctx_set(NULL) in the failure branches for TEST_CHECK(ctx->config !=
NULL), the flb_upstream_create failure (ctx->u == NULL), and the
flb_upstream_conn_get failure (ctx->u_conn == NULL), ensuring you perform the
cleanup before destroying the event loop and freeing ctx to mirror the pattern
used in flb_engine.c/flb_input_thread.c.

---

Nitpick comments:
In `@tests/runtime/http_client_chunked.c`:
- Around line 282-287: The pthread_create() failure handling in
chunked_server_thread setup closes server.listen_fd and returns early, skipping
destruction of server.cv and server.lock; change the early-return path (the
block after pthread_create(&server.thread, NULL, chunked_server_thread,
&server)) to route through the existing cleanup: label instead of returning
directly — e.g., set an error/ret value if needed and goto cleanup so the
teardown that destroys server.cv and server.lock always runs; reference
pthread_create, server.thread, chunked_server_thread, server.cv, server.lock and
the cleanup: label when making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 49c63fef-fbc2-45e8-b578-9d3ffd4acf13

📥 Commits

Reviewing files that changed from the base of the PR and between 1f47d53 and 7c4bb36.

📒 Files selected for processing (1)
  • tests/runtime/http_client_chunked.c

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c4bb36229

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +191 to +193
flb_net_dns_ctx_init();
flb_net_ctx_init(&ctx->dns_ctx);
flb_net_dns_ctx_set(&ctx->dns_ctx);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clear DNS TLS context on every early return

After flb_net_dns_ctx_set(&ctx->dns_ctx) stores a thread-local pointer, the later return NULL branches for upstream creation/connection failure exit after flb_free(ctx) without unsetting or cleaning that DNS context. If either branch is hit (e.g., allocation/socket setup failure), the thread-local DNS pointer becomes dangling and can be dereferenced by later network operations in the same test thread, causing follow-on crashes or flaky failures.

Useful? React with 👍 / 👎.

Comment thread tests/runtime/http_client_chunked.c Outdated
Comment on lines 332 to 335
cleanup:
if (value != NULL) {
TEST_CHECK(strcmp(value, "tomorrow") == 0);
flb_sds_destroy(value);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restore trailer-header assertions in chunked runtime test

This cleanup-label move removed the previous checks for X-Trace and Expires trailer values, so the test now succeeds even when chunked trailer parsing/regression occurs. Because this test specifically serves runtime coverage for chunked responses, dropping trailer validation weakens its signal and can let trailer-handling bugs slip through CI unnoticed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
tests/runtime/http_client_chunked.c (2)

273-276: ⚠️ Potential issue | 🟡 Minor

Clean up mutex/cond on pthread_create failure

If pthread_create fails, the return path at Line 275 skips pthread_cond_destroy and pthread_mutex_destroy for objects initialized at Lines 259-260.

Proposed fix
     ret = pthread_create(&server.thread, NULL, chunked_server_thread, &server);
     TEST_CHECK(ret == 0);
     if (ret != 0) {
         close(server.listen_fd);
+        pthread_cond_destroy(&server.cv);
+        pthread_mutex_destroy(&server.lock);
         return;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/runtime/http_client_chunked.c` around lines 273 - 276, The failure path
after pthread_create currently returns after closing server.listen_fd but skips
destroying the pthread_cond_t and pthread_mutex_t that were initialized earlier;
update the error path (the branch where pthread_create returns non-zero) to call
pthread_cond_destroy on the condition variable and pthread_mutex_destroy on the
mutex (the pthread_cond_t and pthread_mutex_t initialized just before the
pthread_create call) before closing server.listen_fd and returning so resources
are always cleaned up on pthread_create failure.

182-211: ⚠️ Potential issue | 🔴 Critical

Reset DNS context on all create() failure exits after Line 182

After flb_net_dns_ctx_set(&ctx->dns_ctx), the failure branches at Line 195 and Line 205 free ctx without unsetting/cleaning DNS context. This can leave stale global DNS state pointing to freed memory for subsequent tests.

Proposed fix
 static struct runtime_http_client_ctx *runtime_http_client_ctx_create(int port)
 {
     struct runtime_http_client_ctx *ctx;
+    int dns_ctx_set;

     ctx = flb_calloc(1, sizeof(struct runtime_http_client_ctx));
@@
+    dns_ctx_set = FLB_FALSE;
     flb_engine_evl_init();
     flb_engine_evl_set(ctx->evl);
     flb_net_dns_ctx_init();
     flb_net_ctx_init(&ctx->dns_ctx);
     flb_net_dns_ctx_set(&ctx->dns_ctx);
+    dns_ctx_set = FLB_TRUE;
@@
     if (!TEST_CHECK(ctx->config != NULL)) {
-        flb_net_dns_ctx_set(NULL);
+        flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
+        flb_net_dns_ctx_set(NULL);
         mk_event_loop_destroy(ctx->evl);
         flb_free(ctx);
         return NULL;
     }
@@
     if (!TEST_CHECK(ctx->u != NULL)) {
+        if (dns_ctx_set) {
+            flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
+            flb_net_dns_ctx_set(NULL);
+        }
         flb_config_exit(ctx->config);
         mk_event_loop_destroy(ctx->evl);
         flb_free(ctx);
         return NULL;
     }
@@
     if (!TEST_CHECK(ctx->u_conn != NULL)) {
         flb_upstream_destroy(ctx->u);
+        if (dns_ctx_set) {
+            flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
+            flb_net_dns_ctx_set(NULL);
+        }
         flb_config_exit(ctx->config);
         mk_event_loop_destroy(ctx->evl);
         flb_free(ctx);
         return NULL;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/runtime/http_client_chunked.c` around lines 182 - 211, After calling
flb_net_dns_ctx_set(&ctx->dns_ctx) you must unset the global DNS context on
every failure path that frees ctx; update the error branches that follow
flb_config_init (where ctx is freed), flb_upstream_create failure, and
flb_upstream_conn_get failure to call flb_net_dns_ctx_set(NULL) before
destroying/cleaning and returning. Locate the branches that call
flb_config_exit, flb_upstream_destroy, mk_event_loop_destroy, flb_free(ctx) and
insert flb_net_dns_ctx_set(NULL) prior to those cleanup steps so the global DNS
pointer is never left pointing at freed memory.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@tests/runtime/http_client_chunked.c`:
- Around line 273-276: The failure path after pthread_create currently returns
after closing server.listen_fd but skips destroying the pthread_cond_t and
pthread_mutex_t that were initialized earlier; update the error path (the branch
where pthread_create returns non-zero) to call pthread_cond_destroy on the
condition variable and pthread_mutex_destroy on the mutex (the pthread_cond_t
and pthread_mutex_t initialized just before the pthread_create call) before
closing server.listen_fd and returning so resources are always cleaned up on
pthread_create failure.
- Around line 182-211: After calling flb_net_dns_ctx_set(&ctx->dns_ctx) you must
unset the global DNS context on every failure path that frees ctx; update the
error branches that follow flb_config_init (where ctx is freed),
flb_upstream_create failure, and flb_upstream_conn_get failure to call
flb_net_dns_ctx_set(NULL) before destroying/cleaning and returning. Locate the
branches that call flb_config_exit, flb_upstream_destroy, mk_event_loop_destroy,
flb_free(ctx) and insert flb_net_dns_ctx_set(NULL) prior to those cleanup steps
so the global DNS pointer is never left pointing at freed memory.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0f176ff0-149c-4c5b-900c-a3b9337d6255

📥 Commits

Reviewing files that changed from the base of the PR and between 7c4bb36 and 33cef76.

📒 Files selected for processing (1)
  • tests/runtime/http_client_chunked.c

@edsiper
Copy link
Copy Markdown
Member Author

edsiper commented Apr 16, 2026

this was already fixed in master.

@edsiper edsiper closed this Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant