Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(agent): Fix nr_header_create_distributed_trace_map mem leak when hashmap is destroyed #1017

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion axiom/nr_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ nr_hashmap_t* nr_header_create_distributed_trace_map(const char* nr_header,
return NULL;
}

header_map = nr_hashmap_create(NULL);
header_map = nr_hashmap_create((nr_hashmap_dtor_func_t)nr_hashmap_dtor_str);

if (nr_header) {
nr_hashmap_set(header_map, NR_PSTR(NEWRELIC), nr_strdup(nr_header));
}
Expand Down
70 changes: 70 additions & 0 deletions axiom/tests/test_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,75 @@ static void test_account_id_from_cross_process_id(void) {
nr_header_account_id_from_cross_process_id("10#10"));
}

static void test_nr_header_create_distributed_trace_map(void) {
nr_hashmap_t* header_map = NULL;
char* tracestate = "tracestate";
char* traceparent = "traceparent";
char* dt_payload = "newrelic";

header_map = nr_header_create_distributed_trace_map(NULL, NULL, NULL);
tlib_pass_if_null(
"NULL payload and NULL traceparent should return NULL header map",
header_map);

header_map = nr_header_create_distributed_trace_map(NULL, NULL, tracestate);
tlib_pass_if_null(
"NULL payload and NULL traceparent should return NULL header map",
header_map);

header_map = nr_header_create_distributed_trace_map(dt_payload, NULL, NULL);
tlib_pass_if_not_null("if valid dt_payload should return a header map",
header_map);
tlib_pass_if_size_t_equal(
"1 header passed in so should expect headers hashmap size of 1", 1,
nr_hashmap_count(header_map));
nr_hashmap_destroy(&header_map);

header_map
= nr_header_create_distributed_trace_map(dt_payload, traceparent, NULL);
tlib_pass_if_not_null("if valid dt_payload should return a header map",
header_map);
tlib_pass_if_size_t_equal(
"2 headers passed in so should expect headers hashmap size of 2", 2,
nr_hashmap_count(header_map));
nr_hashmap_destroy(&header_map);

header_map
= nr_header_create_distributed_trace_map(dt_payload, NULL, tracestate);
tlib_pass_if_not_null("if valid dt_payload should return a header map",
header_map);
tlib_pass_if_size_t_equal(
"2 headers passed in so should expect headers hashmap size of 2", 2,
nr_hashmap_count(header_map));
nr_hashmap_destroy(&header_map);

header_map = nr_header_create_distributed_trace_map(dt_payload, traceparent,
tracestate);
tlib_pass_if_not_null("if valid dt_payload should return a header map",
header_map);
tlib_pass_if_size_t_equal(
"3 headers passed in so should expect headers hashmap size of 3", 3,
nr_hashmap_count(header_map));
nr_hashmap_destroy(&header_map);

header_map
= nr_header_create_distributed_trace_map(NULL, traceparent, tracestate);
tlib_pass_if_not_null("if valid traceparent should return a header map",
header_map);
tlib_pass_if_size_t_equal(
"Two headers passed in so should expect headers hashmap size of 2", 2,
nr_hashmap_count(header_map));
nr_hashmap_destroy(&header_map);

header_map = nr_header_create_distributed_trace_map(NULL, traceparent, NULL);
tlib_pass_if_not_null("if valid traceparent should return a header map",
header_map);
tlib_pass_if_size_t_equal(
"1 header passed in so should expect headers hashmap size of 1", 1,
nr_hashmap_count(header_map));
nr_hashmap_destroy(&header_map);
}

tlib_parallel_info_t parallel_info = {.suggested_nthreads = 2, .state_size = 0};

void test_main(void* p NRUNUSED) {
Expand All @@ -1721,4 +1790,5 @@ void test_main(void* p NRUNUSED) {
test_set_cat_txn();
test_set_synthetics_txn();
test_account_id_from_cross_process_id();
test_nr_header_create_distributed_trace_map();
}