• Roland McGrath's avatar
    [PATCH] fix MT reparenting when thread group leader dies · d00e2ab5
    Roland McGrath authored
    When the initial thread in a multi-threaded program dies (the thread group
    leader), its child processes are wrongly orphaned, and thereafter when
    other threads die their child processes are also orphaned even though live
    threads remain in the parent process that can call wait.  I have a small
    (under 100 lines), POSIX-compliant test program that demonstrates this
    using -lpthread (NPTL) if anyone is interested in seeing it.
    
    The bug is that forget_original_parent moves children to the dead parent's
    group leader if it's alive, but if not it orphans them.  I've changed it so
    it instead reparents children to any other live thread in the dead parent's
    group (not even preferring the group leader).  Children go to init only if
    there are no live threads in the parent's group at all.  These are the
    correct semantics for fork children of POSIX threads. 
    
    The second part of the change is to do the CLONE_PARENT behavior always for
    CLONE_THREAD, i.e.  make sure that each new thread's parent link points to
    the real parent of the process and never another thread in its own group.
    Without this, when the group leader dies leaving a sole live thread in the
    group, forget_original_parent will try to reparent that thread to itself
    because it's a child of the dying group leader.  Rather handling this case
    specially to reparent to the group leader's parent instead, it's more
    efficient just to make sure that noone ever has a parent link to inside his
    own thread group.  Now the reparenting work never needs to be done for
    threads created in the same group when their creator thread dies.  The only
    change from losing the who-created-whom information is when you look at
    "PPid:" in /proc/PID/task/TID/status.  For purposes of all direct system
    calls, it was already as if CLONE_THREAD threads had the parent of the
    group leader.  (POSIX provides no way to keep track of which thread created
    which other thread with pthread_create.)
    Signed-off-by: default avatarRoland McGrath <roland@redhat.com>
    Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
    d00e2ab5
fork.c 32.4 KB