Issue #16808 has been reported by jiasha (Shan Jiang).

----------------------------------------
Bug #16808: Thread#name= only accepts up to 15 characters with Linux/pthread implementation
https://bugs.ruby-lang.org/issues/16808

* Author: jiasha (Shan Jiang)
* Status: Open
* Priority: Normal
* ruby -v: 2.7.1
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>