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

varnishd: truncate thread name on Linux #4211

Merged
merged 1 commit into from
Oct 14, 2024

Conversation

asadsa92
Copy link
Contributor

On Linux, threads can not have name longer than 15 bytes plus a terminating '\0' byte:

PR_SET_NAME (since Linux 2.6.9)
Set the name of the calling thread, using the value in the loca‐
tion pointed to by (char *) arg2. The name can be up to 16
bytes long, including the terminating null byte. (If the length
of the string, including the terminating null byte, exceeds 16
bytes, the string is silently truncated.) This is the same at‐
tribute that can be set via pthread_setname_np(3) and retrieved
using pthread_getname_np(3). The attribute is likewise accessi‐
ble via /proc/self/task/tid/comm (see proc(5)), where tid is the
thread ID of the calling thread, as returned by gettid(2).

We have until now ignored the return value from pthread_setname_np(), this is not great as the call then becomes a NOP:

The pthread_setname_np() function can be used to set a
unique name for a thread, which can be useful for debugging multi‐
threaded applications. The thread name is a meaningful C language
string, whose length is restricted to 16 characters, including the ter‐
minating null byte ('\0').
[...]
ERANGE The length of the string specified pointed to by name exceeds
the allowed limit.

This patch truncates long names to 14 characters plus a tilde ('~') character.

On Linux, threads can not have name longer than 15 bytes plus a terminating
'\0' byte:
> PR_SET_NAME (since Linux 2.6.9)
>     Set the name of the calling thread, using the value in the loca‐
>     tion  pointed  to  by  (char  *) arg2.  The name can be up to 16
>     bytes long, including the terminating null byte.  (If the length
>     of  the  string, including the terminating null byte, exceeds 16
>     bytes, the string is silently truncated.)  This is the same  at‐
>     tribute  that can be set via pthread_setname_np(3) and retrieved
>     using pthread_getname_np(3).  The attribute is likewise accessi‐
>     ble via /proc/self/task/tid/comm (see proc(5)), where tid is the
>     thread ID of the calling thread, as returned by gettid(2).

We have until now ignored the return value from pthread_setname_np(), this
is not great as the call then becomes a NOP:
> The pthread_setname_np() function can be used to  set  a
>     unique  name  for  a  thread,  which can be useful for debugging multi‐
>     threaded applications.  The thread name  is  a  meaningful  C  language
>     string, whose length is restricted to 16 characters, including the ter‐
>     minating null byte ('\0').
> [...]
>     ERANGE The  length  of  the string specified pointed to by name exceeds
>         the allowed limit.

This patch truncates long names to 14 characters plus a tilde ('~') character.

Signed-off-by: Asad Sajjad Ahmed <asadsa@varnish-software.com>
@asadsa92 asadsa92 added the b=bug label Oct 11, 2024
@asadsa92 asadsa92 self-assigned this Oct 11, 2024
@nigoroll nigoroll merged commit 1a79fde into varnishcache:master Oct 14, 2024
10 of 11 checks passed
@nigoroll
Copy link
Member

merged per bugwash decision

@asadsa92 asadsa92 deleted the thr_setname branch October 17, 2024 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants