Skip to content

fix: use pthread_self() for thread naming inside thread callback#839

Open
aki1770-del wants to merge 1 commit intoCOVESA:masterfrom
aki1770-del:fix/pthread-setname-self-811
Open

fix: use pthread_self() for thread naming inside thread callback#839
aki1770-del wants to merge 1 commit intoCOVESA:masterfrom
aki1770-del:fix/pthread-setname-self-811

Conversation

@aki1770-del
Copy link
Copy Markdown

Problem

Two thread functions set their thread name using a stored pthread_t handle:

// In dlt_segmented_nwt_function():
pthread_setname_np(dlt_user.dlt_segmented_nwt_handle, "dlt_segmented");

// In dlt_housekeeperthread_function():
pthread_setname_np(dlt_housekeeperthread_handle, "dlt_housekeeper");

On Linux, pthread_setname_np(3) with a non-self TID is implemented via prctl(PR_SET_NAME) on the target thread, but the stored handle may not be fully committed to the kernel thread table at the moment the call is made (the spawning pthread_create() and the thread start racing). Using the stored handle instead of pthread_self() inside the thread body is a TOCTOU race.

The POSIX-safe and portable approach is to call pthread_setname_np(pthread_self(), name) from within the thread function, which is what this patch does.

Reported in #811.

Fix

Replace the stored handle with pthread_self() at both call sites.

Testing

Thread names visible in /proc/<pid>/task/*/comm and ps -T correctly reflect dlt_segmented and dlt_housekeepr (15-char kernel limit) after the fix.

pthread_setname_np() must be called from within the thread
whose name is being set (Linux: max 15 chars, affects only the
calling thread's /proc entry). Both dlt_segmented_nwt_handle
and dlt_housekeeperthread_handle were passed as the first
argument from inside the thread function itself, but the Linux
man page specifies that passing a non-self TID is unreliable
when the thread may not have been fully scheduled yet.

Replace the stored handle argument with pthread_self() at both
call sites so the name is always applied to the running thread.

Fixes COVESA#811
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant