mailing list of musl libc
 help / color / mirror / code / Atom feed
* segmentation fault on pthread_detach
@ 2018-03-27 12:00 Salman.Ahmed
  2018-03-27 12:32 ` Jens Gustedt
  0 siblings, 1 reply; 4+ messages in thread
From: Salman.Ahmed @ 2018-03-27 12:00 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 2572 bytes --]

Hello,

My application using musl libc runs into a seg fault if I call pthread_detach after pthread_join on the same thread. From my understanding pthread_detach should return ESRCH if a previous join was called on a particular thread. That’s the correct behavior I see if I run my program with glibc. But on my LEDE based router I run into the seg fault issue.
The cross compiling toolchain I use is arm_cortex-a9+neon_gcc-5.4.0_musl-1.1.16_eabi. Simple example I used for testing is below.

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );

int main()
{
     pthread_t thread1, thread2;
     const char *message1 = "Thread 1";
     const char *message2 = "Thread 2";
     int  iret1, iret2;
     int dret1, dret2;

     iret1 = pthread_create( &thread1, NULL, &print_message_function, (void*) message1);
     if(iret1)
     {
         fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
         exit(EXIT_FAILURE);
     }

     iret2 = pthread_create( &thread2, NULL, &print_message_function, (void*) message2);
     if(iret2)
     {
         fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
         exit(EXIT_FAILURE);
     }

     printf("pthread_create() for thread 1 returns: %d\n",iret1);
     printf("pthread_create() for thread 2 returns: %d\n",iret2);

     pthread_join( thread1, NULL);
     pthread_join( thread2, NULL);

     dret1 =  pthread_detach(thread1);
     if (dret1 == EINVAL) {
          printf("got EINVAL when detaching 1\n");
     }
     if (dret1 ==  ESRCH) {
          printf("got ESRCH when detaching 1\n");
     }
     dret2 = pthread_detach(thread2);
     if (dret2 == EINVAL) {
          printf("got EINVAL when detaching 2\n");
     }
     if (dret2 ==  ESRCH) {
          printf("got ESRCH when detaching 2\n");
     }

     return 0;
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}

Output gotten
pthread_create() for thread 1 returns: 0
Thread 1
pthread_create() for thread 2 returns: 0
Thread 2
Segmentation fault (core dumped)

Regards
Salman
________________________________
Kommanditgesellschaft - Sitz: Detmold - Amtsgericht Lemgo HRA 2790 -
Komplementärin: Weidmüller Interface Führungsgesellschaft mbH -
Sitz: Detmold - Amtsgericht Lemgo HRB 3924;
Geschäftsführer: José Carlos Álvarez Tobar, Elke Eckstein, Jörg Timmermann;
USt-ID-Nr. DE124599660

[-- Attachment #2: Type: text/html, Size: 14289 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: segmentation fault on pthread_detach
  2018-03-27 12:00 segmentation fault on pthread_detach Salman.Ahmed
@ 2018-03-27 12:32 ` Jens Gustedt
  2018-03-27 12:51   ` AW: " Salman.Ahmed
  0 siblings, 1 reply; 4+ messages in thread
From: Jens Gustedt @ 2018-03-27 12:32 UTC (permalink / raw)
  To: Salman.Ahmed; +Cc: musl

[-- Attachment #1: Type: text/plain, Size: 1419 bytes --]

Hello Salman,

On Tue, 27 Mar 2018 12:00:39 +0000 <Salman.Ahmed@weidmueller.com> wrote:

> My application using musl libc runs into a seg fault if I call
> pthread_detach after pthread_join on the same thread. From my
> understanding pthread_detach should return ESRCH if a previous join
> was called on a particular thread. That’s the correct behavior I see
> if I run my program with glibc. But on my LEDE based router I run
> into the seg fault issue. The cross compiling toolchain I use is
> arm_cortex-a9+neon_gcc-5.4.0_musl-1.1.16_eabi. Simple example I used
> for testing is below.

I don't think that your expectation is correct. The POSIX standard
states:

> The behavior is undefined if the value specified by the thread argument
> to pthread_detach() does not refer to a joinable thread.

(a thread that has already been joined is not joinable)

This is for good reasons. The system must be able to dispose of all
resources that a thread occupied after it has been joined. The memory
will be reused, in particular there may be a new thread that uses the
same identifier.

Jens

-- 
:: INRIA Nancy Grand Est ::: Camus ::::::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

[-- Attachment #2: Digitale Signatur von OpenPGP --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* AW: segmentation fault on pthread_detach
  2018-03-27 12:32 ` Jens Gustedt
@ 2018-03-27 12:51   ` Salman.Ahmed
  2018-03-27 13:00     ` Szabolcs Nagy
  0 siblings, 1 reply; 4+ messages in thread
From: Salman.Ahmed @ 2018-03-27 12:51 UTC (permalink / raw)
  To: jens.gustedt; +Cc: musl

Hi Jens,

Thanks for the clarification. So I guess with musl I have to remove the pthread_detach (which is redundant anyways after a successful join) or call it only if pthread_join returns an error?

Regards
Salman

-----Ursprüngliche Nachricht-----
Von: Jens Gustedt [mailto:jens.gustedt@inria.fr]
Gesendet: Dienstag, 27. März 2018 14:33
An: Ahmed, Salman <Salman.Ahmed@weidmueller.com>
Cc: musl@lists.openwall.com
Betreff: Re: [musl] segmentation fault on pthread_detach

Hello Salman,

On Tue, 27 Mar 2018 12:00:39 +0000 <Salman.Ahmed@weidmueller.com> wrote:

> My application using musl libc runs into a seg fault if I call
> pthread_detach after pthread_join on the same thread. From my
> understanding pthread_detach should return ESRCH if a previous join
> was called on a particular thread. That’s the correct behavior I see
> if I run my program with glibc. But on my LEDE based router I run into
> the seg fault issue. The cross compiling toolchain I use is
> arm_cortex-a9+neon_gcc-5.4.0_musl-1.1.16_eabi. Simple example I used
> for testing is below.

I don't think that your expectation is correct. The POSIX standard
states:

> The behavior is undefined if the value specified by the thread
> argument to pthread_detach() does not refer to a joinable thread.

(a thread that has already been joined is not joinable)

This is for good reasons. The system must be able to dispose of all resources that a thread occupied after it has been joined. The memory will be reused, in particular there may be a new thread that uses the same identifier.

Jens

--
:: INRIA Nancy Grand Est ::: Camus ::::::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::
________________________________
Kommanditgesellschaft - Sitz: Detmold - Amtsgericht Lemgo HRA 2790 -
Komplementärin: Weidmüller Interface Führungsgesellschaft mbH -
Sitz: Detmold - Amtsgericht Lemgo HRB 3924;
Geschäftsführer: José Carlos Álvarez Tobar, Elke Eckstein, Jörg Timmermann;
USt-ID-Nr. DE124599660

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: segmentation fault on pthread_detach
  2018-03-27 12:51   ` AW: " Salman.Ahmed
@ 2018-03-27 13:00     ` Szabolcs Nagy
  0 siblings, 0 replies; 4+ messages in thread
From: Szabolcs Nagy @ 2018-03-27 13:00 UTC (permalink / raw)
  To: musl; +Cc: jens.gustedt, Salman.Ahmed

* Salman.Ahmed@weidmueller.com <Salman.Ahmed@weidmueller.com> [2018-03-27 12:51:54 +0000]:
> Thanks for the clarification. So I guess with musl I have to remove the pthread_detach (which is redundant anyways after a successful join) or call it only if pthread_join returns an error?

this is a bug on all systems, not just on musl

if it works anywhere that's just luck,
it can certainly crash on glibc too, but
i'd be surprised if it worked reliably on
any system, the crash may be rare though.

don't use pthread_detach it does not help there.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-03-27 13:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-27 12:00 segmentation fault on pthread_detach Salman.Ahmed
2018-03-27 12:32 ` Jens Gustedt
2018-03-27 12:51   ` AW: " Salman.Ahmed
2018-03-27 13:00     ` Szabolcs Nagy

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).