This is the mail archive of the glibc-bugs@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug nptl/415] New: pthread_cancel does not unwind stack when thread is cancelled inside signal handler


The New NPTL is specifically designed to solve the problem with thread
cancellation (pthread_cancel) or pthread_exit does not unwind thread stack (so
thread local objects are not destructed) problem, I've tested and it works great
if a thread is cancelled while it is in it's normal execution; however, I tested
that if the thread is cancelled while it is inside it's signal handler, the
thread stack still does not unwind, I think this is a very serious bug since
NPTL is designed to solve this issue.

The following is a simple test case:

//////////////////////////////////////////////////////////////////////
//
// Author: Yufeng Xiong
// pthread_cancel.cxx  --  
// 
//////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>

class Test
{
private:
  int var;
public:
  Test(int i= 0) : var(i) { printf("Test %d created!\n", var); }
  ~Test() { printf("Test %d destructed!\n", var); }
};

extern "C" {
void * Thread(void *)
{
  //ignore SIGALRM
  sigset_t sigs;
  pthread_sigmask(SIG_SETMASK, NULL, &sigs);
  sigaddset(&sigs, SIGALRM);
  pthread_sigmask(SIG_SETMASK, &sigs, NULL);
  Test One(1), Two(2);
  sleep(1000000); //pause - wait to be cancelled
  return (void *)0;
}

void sighand(int signo)
{
  printf("Caught signal\n");
  sleep(1000000); //wait to be cancelled
  return;
}
}

//main test, pass argv[1] = 0 or no argument to test cancel
//in normal execution, pass argv[1] = 1 to test cancel in
//thread signal handler
//to compile: $CC -D_POSIX_C_SOURCE=199506L -g -o pthread_cancel
pthread_cancel.cxx -lpthread
//expected results
/* case 1
./pthread_cancel 
Set up the SIGUSR1 handler for the process
Test 1 created!
Test 2 created!
Main thread cancel!
Test 2 destructed!
Test 1 destructed!
*/
/* case 2
./pthread_cancel 1
Set up the SIGUSR1 handler for the process
Test 1 created!
Test 2 created!
Main thread send signal!
Main thread cancel!
Caught signal
Test 2 destructed!
Test 1 destructed!
*/

int main(int argc, char **argv)
{
  struct sigaction actions;
  printf("Set up the SIGUSR1 handler for the process\n");
 
  sigemptyset(&actions.sa_mask);
  actions.sa_flags = 0;
  actions.sa_handler = sighand;
  sigaction(SIGUSR1,&actions,NULL);  

  pthread_t thr = 0;
  pthread_create(&thr, NULL, Thread, NULL);

  //seems can not use sleep or nanosleep to yield execution
  //because that may interfere with pause call
  sleep(1); //give the thread chance to run
  
  //if passed argv[1] = 1, send signal first
  if (argc > 1 && strcmp(argv[1], "1") == 0)
  {
      printf("Main thread send signal!\n");
      pthread_kill(thr, SIGUSR1);
  }
  //cancel the thread, if signal is sent, thread 'thr' 
  //should be in it's signal handler, otherwise it is in
  //it's normal exeuction, both case it is 'pausing' and
  //can be safely cancelled
  if (pthread_kill(thr, 0) == 0)
  {
      printf("Main thread cancel!\n");
      pthread_cancel(thr);
  }
  pthread_join(thr, (void **)0);

  return 0;
}

-- 
           Summary: pthread_cancel does not unwind stack when thread is
                    cancelled inside signal handler
           Product: glibc
           Version: 2.3.3
            Status: NEW
          Severity: critical
          Priority: P1
         Component: nptl
        AssignedTo: drepper at redhat dot com
        ReportedBy: yufeng_xiong at ltx dot com
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: i686-redhat-linux-gnu
  GCC host triplet: i686-redhat-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://sources.redhat.com/bugzilla/show_bug.cgi?id=415

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]