Project

General

Profile

Actions

Bug #16808

closed

Thread#name= only accepts up to 15 characters with Linux/pthread implementation

Added by jiasha (Shan Jiang) almost 4 years ago. Updated almost 4 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:98025]

Description

Repro:

irb(main):001:0> RUBY_VERSION
=> "2.7.1"
irb(main):002:0> Thread.new { Thread.current.name = 'abcdefg'; sleep }
=> #<Thread:0x000055584a17e1c8 (irb):2 run>
irb(main):003:0> Thread.new { Thread.current.name = 'abcdefghijklmnopqrst'; sleep }
=> #<Thread:0x000055584a18e668 (irb):3 run>
irb(main):004:0> Thread.new { Thread.current.name = 'abcdefghijklmnop'; sleep }
=> #<Thread:0x000055584a16f9e8 (irb):4 run>
irb(main):005:0> Thread.new { Thread.current.name = 'abcdefghijklmno'; sleep }
=> #<Thread:0x000055584a176478 (irb):5 run>

Result:

$ ps -T -p PID
PID SPID TTY TIME CMD
13137 13137 pts/0 00:00:00 irb
13137 13158 pts/0 00:00:00 abcdefg
13137 13160 pts/0 00:00:00 (irb):3
13137 13165 pts/0 00:00:00 (irb):4
13137 13167 pts/0 00:00:00 abcdefghijklmno

It looked like the Linux implementation calls pthread_setname_np(), which limits the length of thread names to 15 characters, which is a hardcoded constant (TASK_COMM_LEN) in the kernel:
https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/pthread_setname.c.html#pthread_setname_np

Thread#name= calls pthread_setname_np() without checking its return value:
https://github.com/ruby/ruby/blob/0256e4f0f5e10f0a15cbba2cd64e252dfa864e4a/thread_pthread.c#L1615

Since it's unlikely for one to call this method intensely, fetch/checking the thread name again after setting it, or even truncating the passed in string to 15 characters in this method might be a better solution.

Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 4 years ago

  • Status changed from Open to Closed

Applied in changeset git|a52a459b16ce1b5cf32cb6393960ff59e35c48d0.


Truncate too long thread name before setting [Bug #16808]

Actions

Also available in: Atom PDF

Like0
Like0