theRockLiu / thread-sanitizer

Automatically exported from code.google.com/p/thread-sanitizer
0 stars 0 forks source link

signals that arrive during pthread_cond_wait are never delivered #91

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

#include <pthread.h>
#include <signal.h>
#include <unistd.h>

int g_thread_run = 1;
pthread_mutex_t mutex;
pthread_cond_t cond;
int g_signal_count = 0;

void sig_handler(int signum) {
  write(0, "sig\n", 4);
  __sync_fetch_and_add(&g_signal_count, 1);
}

void* my_thread(void* arg) {
  pthread_mutex_lock(&mutex);
  while (g_thread_run) {
    pthread_cond_wait(&cond, &mutex);
  }
  pthread_mutex_unlock(&mutex);
  return NULL;
}

int main(int argc, char**argv) {
  signal(SIGUSR1, &sig_handler);

  pthread_t thr;
  pthread_create(&thr, NULL, &my_thread, NULL);
  sleep(1); // wait for thread to get inside pthread_cond_wait
  pthread_kill(thr, SIGUSR1);

  while (__atomic_load_n(&g_signal_count, __ATOMIC_SEQ_CST) == 0) {
    ;
  }

  pthread_mutex_lock(&mutex);
  g_thread_run = 0;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mutex);
  void* ret;
  pthread_join(thr, &ret);
  return 0;
}

What is the expected output? What do you see instead?

todd@todd-ThinkPad-T540p:/tmp$ /home/todd/sw/clang-230631-2/bin/clang -o test 
test.c -lpthread
todd@todd-ThinkPad-T540p:/tmp$ time ./test
sig

real    0m1.001s
user    0m0.000s
sys 0m0.001s
todd@todd-ThinkPad-T540p:/tmp$ /home/todd/sw/clang-230631-2/bin/clang -o test 
-fsanitize=thread test.c -lpthread
todd@todd-ThinkPad-T540p:/tmp$ time ./test
<hangs forever>

What version of the product are you using? On what operating system?
todd@todd-ThinkPad-T540p:/tmp$ /home/todd/sw/clang-230631-2/bin/clang -v
clang version 3.7.0 (trunk 230631)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.8.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.1
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
(from the chromium toolchain)

Please provide any additional information below.

Original issue reported on code.google.com by tlip...@gmail.com on 8 Apr 2015 at 4:29

GoogleCodeExporter commented 9 years ago
Should actually rephrase the title a bit -- the signal does arrive, but only 
after pthread_cond_wait returns. It looks like the pthread_cond_wait 
interceptor is missing a wrapping 'BlockingCall' scope, perhaps?

Original comment by tlip...@gmail.com on 8 Apr 2015 at 4:35

GoogleCodeExporter commented 9 years ago
Thanks for the report!
Should be fixed by:
http://llvm.org/viewvc/llvm-project?view=revision&revision=234394

Original comment by dvyu...@google.com on 8 Apr 2015 at 7:52