From 5fd55837bbc400d8cc15152ac2b80b64baa9b68c Mon Sep 17 00:00:00 2001 From: Qi Wang Date: Fri, 10 Feb 2023 15:28:22 -0800 Subject: Fix thread_name updating for heap profiling. The current thread name reading path updates the name every time, which requires both alloc and dalloc -- and the temporary NULL value in the middle causes races where the prof dump read path gets NULLed in the middle. Minimize the changes in this commit to isolate the bugfix testing; will also refactor the whole thread name paths later. --- src/prof_data.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/prof_data.c b/src/prof_data.c index f8b19594..56d3dc88 100644 --- a/src/prof_data.c +++ b/src/prof_data.c @@ -451,16 +451,15 @@ prof_thread_name_alloc(tsd_t *tsd, const char *thread_name) { } size = strlen(thread_name) + 1; - if (size == 1) { - return ""; - } - ret = iallocztm(tsd_tsdn(tsd), size, sz_size2index(size), false, NULL, true, arena_get(TSDN_NULL, 0, true), true); if (ret == NULL) { return NULL; } + memcpy(ret, thread_name, size); + ret[size - 1] = '\0'; + return ret; } @@ -493,14 +492,14 @@ prof_thread_name_set_impl(tsd_t *tsd, const char *thread_name) { return EAGAIN; } - if (tdata->thread_name != NULL) { - idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true, - true); - tdata->thread_name = NULL; - } - if (strlen(s) > 0) { - tdata->thread_name = s; + char *old_thread_name = tdata->thread_name; + tdata->thread_name = s; + if (old_thread_name != NULL) { + idalloctm(tsd_tsdn(tsd), old_thread_name, /* tcache */ NULL, + /* alloc_ctx */ NULL, /* is_internal */ true, + /* slow_path */ true); } + return 0; } -- cgit v1.2.1