Menu

#57 prctl + PR_SET_NAME + threads = pstree bug

Next Release
closed
None
1
2015-02-06
2013-05-15
Марк
No

TO reproduce, run python program:

from functools import partial
import os
import subprocess
from threading import Thread
import time
import prctl

def thrd(num):
    prctl.set_name('thr_name_' + str(num))
    time.sleep(10)

def main():
    for i in xrange(2):
        t = Thread(target=partial(thrd, i))
        t.daemon = True
        t.start()

    prctl.set_name('proc_name')
    time.sleep(0.1)
    subprocess.call(['pstree', '-pa', str(os.getpid())])
    subprocess.call(['top', '-Hbn1', '-p', str(os.getpid())])

if __name__ == '__main__':
    main()

proc_name,6641 /home/mmarkk/src/pytest/qwe.py
  |-pstree,6644 -pa 6641
  |-{proc_name},6642
  `-{proc_name},6643

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND            
 6641 mmarkk    20   0  180m 6168 2892 S    0  0.2   0:00.02 proc_name          
 6642 mmarkk    20   0  180m 6168 2892 S    0  0.2   0:00.00 thr_name_0         
 6643 mmarkk    20   0  180m 6168 2892 S    0  0.2   0:00.00 thr_name_1

Discussion

  • Марк

    Марк - 2013-05-15

    I expect that pstree shows thread names for threads instead of process name for each thread. Example shows that "top" utility works correctly.

     
  • Craig Small

    Craig Small - 2013-09-29

    Line 874 has the problem:

    sprintf(threadname, "{%.*s}", COMM_LEN, comm);
    

    What this does is it takes the command name of the process, puts braces around it and calls it a thread name. This is what we're seeing. What pstree should do is look in /proc/<PID>/task/<TID>/stat for the right command name for the thread.

    Pretty easy one to fix, thanks for pointing it out and giving me a test script.

     
  • Craig Small

    Craig Small - 2013-09-29
    • status: open --> pending
    • assigned_to: Craig Small
     
  • Craig Small

    Craig Small - 2013-09-29

    Git commit [9de89a] has the fix for this. The commit shows the before and after behaviour.

     

    Related

    Commit: [9de89a]


    Last edit: Craig Small 2014-01-31
  • Craig Small

    Craig Small - 2014-01-31
    • status: pending --> closed
     
  • Anonymous

    Anonymous - 2014-02-01

    Hay! you forgot to call fclose() after fopen() in new code (!)

     
  • Craig Small

    Craig Small - 2014-02-02

    fcloses are in commit [ffc53f], assuming that was the ones you were referring to.

     

    Related

    Commit: [ffc53f]

Anonymous
Anonymous

Add attachments
Cancel