• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t
2 
3 // Data race randomly triggered.
4 // UNSUPPORTED: netbsd
5 
6 // Make sure TSan doesn't deadlock on a file stream lock at program shutdown.
7 // See https://github.com/google/sanitizers/issues/454
8 #ifdef __FreeBSD__
9 #define _WITH_GETLINE  // to declare getline()
10 #endif
11 
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <unistd.h>
15 
thread(void * unused)16 void *thread(void *unused) {
17   char *line = NULL;
18   size_t size;
19   int fd[2];
20   pipe(fd);
21   // Forge a non-standard stream to make sure it's not closed.
22   FILE *stream = fdopen(fd[0], "r");
23   while (1) {
24     volatile int res = getline(&line, &size, stream);
25     (void)res;
26   }
27   return NULL;
28 }
29 
main()30 int main() {
31   pthread_t t;
32   pthread_attr_t a;
33   pthread_attr_init(&a);
34   pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
35   pthread_create(&t, &a, thread, NULL);
36   pthread_attr_destroy(&a);
37   fprintf(stderr, "DONE\n");
38   return 0;
39   // ThreadSanitizer used to hang here because of a deadlock on a file stream.
40 }
41 
42 // CHECK: DONE
43