47 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t2 3// Data race randomly triggered.4// UNSUPPORTED: target={{.*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/4548 9// https://github.com/google/sanitizers/issues/173310// UNSUPPORTED: glibc-2.3811 12#ifdef __FreeBSD__13#define _WITH_GETLINE // to declare getline()14#endif15 16#include <pthread.h>17#include <stdio.h>18#include <unistd.h>19 20void *thread(void *unused) {21 char *line = NULL;22 size_t size;23 int fd[2];24 pipe(fd);25 // Forge a non-standard stream to make sure it's not closed.26 FILE *stream = fdopen(fd[0], "r");27 while (1) {28 volatile int res = getline(&line, &size, stream);29 (void)res;30 }31 return NULL;32}33 34int main() {35 pthread_t t;36 pthread_attr_t a;37 pthread_attr_init(&a);38 pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);39 pthread_create(&t, &a, thread, NULL);40 pthread_attr_destroy(&a);41 fprintf(stderr, "DONE\n");42 return 0;43 // ThreadSanitizer used to hang here because of a deadlock on a file stream.44}45 46// CHECK: DONE47