Re: [Valgrind-users] Error when analysing pthreads code?

2018-06-29 Thread EML
Hi Philippe - yes, I think you're right. I've reported a possible race 
condition on RedHat's pthread_exit 
(https://bugzilla.redhat.com/show_bug.cgi?id=1596537).


Thanks.

--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users


Re: [Valgrind-users] Error when analysing pthreads code?

2018-06-28 Thread Philippe Waroquiers
Yo

On Thu, 2018-06-28 at 14:50 +0100, EML wrote:
> The program below creates two threads, in a detached state, and waits for 
> them to complete with 'pthread_exit'. Compile and run as:
> 
> > gcc -lpthread test.c
> > valgrind --leak-check=full a.out
> 
> valgrind-3.13.0 (compiled from source) reports no errors on some runs, and a 
> possible leak on other runs (560 bytes in 1 blocks), fairly randomly. On my 
> system, the ratio of error-free to bad runs is about 50/50.
> 
> Is this is a problem with valgrind, or my use of pthreads? Note that this 
> isn't the standard 'pthread_create' memory leak with joinable threads which 
> haven't been joined, since the threads are detached.  I'm compiling on Centos 
> 6.9, gcc 4.4.7. Thanks.

On my system, I systematically have the following possible leak:
==20923== 272 bytes in 1 blocks are possibly lost in loss record 1 of 1
==20923==at 0x4C2DB70: calloc (vg_replace_malloc.c:711)
==20923==by 0x4011EF1: allocate_dtv (dl-tls.c:322)
==20923==by 0x401287D: _dl_allocate_tls (dl-tls.c:539)
==20923==by 0x4E4100B: allocate_stack (allocatestack.c:580)
==20923==by 0x4E4100B: pthread_create@@GLIBC_2.2.5 (pthread_create.c:539)
==20923==by 0x108936: main (pth.c:19)

Assuming what you see is the same leak (always interesting to give the error
when you discuss an error :)), the possible leak disappears if I am adding
   sleep (10);
before
   pthread_exit(0);

So, it looks like pthread_exit (0) is executed in your case
when a detached thread has not yet cleaned up some of the memory
(the dtv array) used for its thread local storage.
And when this detached thread exits as the last one of the process,
no cleanup of this memory is done.

You can further investigate what happens by adding -v -v -v -d -d -d
and/or use some more options of memcheck to see what stack trace is releasing
this dtv memory when you add the sleep.

Philippe




--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
___
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users


[Valgrind-users] Error when analysing pthreads code?

2018-06-28 Thread EML
The program below creates two threads, in a detached state, and waits 
for them to complete with 'pthread_exit'. Compile and run as:


> gcc -lpthread test.c
> valgrind --leak-check=full a.out

valgrind-3.13.0 (compiled from source) reports no errors on some runs, 
and a possible leak on other runs (560 bytes in 1 blocks), fairly 
randomly. On my system, the ratio of error-free to bad runs is about 50/50.


Is this is a problem with valgrind, or my use of pthreads? *Note that 
this isn't the standard 'pthread_create' memory leak with joinable 
threads which haven't been joined, since the threads are detached.* I'm 
compiling on Centos 6.9, gcc 4.4.7. Thanks.



#include 
#include 
#include 

void *thread(void *data) {
   printf("in new thread %ld\n", (long)data);
}

int main() {
   pthread_t  thread_id[2];
   pthread_attr_t thread_attr;
   int failed = 1;

   do {
  if(pthread_attr_init(_attr))
 break;
  if(pthread_attr_setdetachstate(_attr, 
PTHREAD_CREATE_DETACHED))

 break;
  if(pthread_create(_id[0], _attr, thread, (void*)0))
 break;
  if(pthread_create(_id[1], _attr, thread, (void*)1))
 break;
  failed = 0;
   } while(0);

   if(failed) {
  printf("pthreads call failed\n");
  exit(1);
   }

   pthread_exit(0);
}



--
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot___
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users