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.
This commit is contained in:
Qi Wang 2023-02-10 15:28:22 -08:00 committed by Qi Wang
parent 8580c65f81
commit 5fd55837bb

View File

@ -451,16 +451,15 @@ prof_thread_name_alloc(tsd_t *tsd, const char *thread_name) {
} }
size = strlen(thread_name) + 1; size = strlen(thread_name) + 1;
if (size == 1) {
return "";
}
ret = iallocztm(tsd_tsdn(tsd), size, sz_size2index(size), false, NULL, ret = iallocztm(tsd_tsdn(tsd), size, sz_size2index(size), false, NULL,
true, arena_get(TSDN_NULL, 0, true), true); true, arena_get(TSDN_NULL, 0, true), true);
if (ret == NULL) { if (ret == NULL) {
return NULL; return NULL;
} }
memcpy(ret, thread_name, size); memcpy(ret, thread_name, size);
ret[size - 1] = '\0';
return ret; return ret;
} }
@ -493,14 +492,14 @@ prof_thread_name_set_impl(tsd_t *tsd, const char *thread_name) {
return EAGAIN; return EAGAIN;
} }
if (tdata->thread_name != NULL) { char *old_thread_name = tdata->thread_name;
idalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true, tdata->thread_name = s;
true); if (old_thread_name != NULL) {
tdata->thread_name = NULL; idalloctm(tsd_tsdn(tsd), old_thread_name, /* tcache */ NULL,
} /* alloc_ctx */ NULL, /* is_internal */ true,
if (strlen(s) > 0) { /* slow_path */ true);
tdata->thread_name = s;
} }
return 0; return 0;
} }