kejiewei / thread-sanitizer

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

Instrumented program hangs at exit if one of the threads is running getline() #47

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
$ cat t.c
#include <pthread.h>
#include <stdio.h>

void *thread(void *unused) {
  char *line = NULL;
  size_t size;
  getline(&line, &size, stdin);
  return NULL;
}

int main() {
  pthread_t t;
  pthread_create(&t, NULL, thread, NULL);
  return 0;
}
$ clang t.c -o t -lpthread
$ ./t
(everything ok)
$ clang t.c -o t -lpthread -fsanitize=thread
% ./t
(hangs)

Original issue reported on code.google.com by gli...@google.com on 6 Feb 2014 at 11:59

GoogleCodeExporter commented 9 years ago
This happens because TSan calls fflush(0) at program shutdown, while the child 
thread is holding a lock on the file stream.
Dima's suggestion is to replace the call to fflush(0) with "fflush(stdout); 
fflush(stderr);"

Original comment by gli...@google.com on 6 Feb 2014 at 12:15

GoogleCodeExporter commented 9 years ago
I don't see how I can make this test reliable enough to commit it.
Turns out that the program may exit before the call to getline(), and we can't 
synchronize that call with the program exit, because getline() never ends.
I'm gonna submit the fix without this test.

Original comment by gli...@google.com on 6 Feb 2014 at 1:39

GoogleCodeExporter commented 9 years ago
sleep(1) will help you to write the test
also it's not necessary for a test to fail 100% of time

Original comment by dvyu...@google.com on 6 Feb 2014 at 1:46

GoogleCodeExporter commented 9 years ago
Coped with an infinite loop in thread() instead of a sleep.
r200922 fixes the issue and r200923 adds the test.

Original comment by gli...@google.com on 6 Feb 2014 at 2:12

GoogleCodeExporter commented 9 years ago
Adding Project:ThreadSanitizer as part of GitHub migration.

Original comment by gli...@google.com on 30 Jul 2015 at 9:21