openposix: pthread_join/speculative/6-1: Fix race with detached thread exit
PATCH 1/1 openposix: pthread_join/speculative/6-1: Fix race with detached thread exit
From: Avinesh Kumar <[email protected]>
The test creates a detached thread whose body returns immediately, then expects pthread_join() on its ID to fail with EINVAL. Once the thread has exited, its ID is no longer valid and the behavior is undefined. The outcome depends on scheduling, so the test fails sporadically, mostly on single CPU machines.
Keep the thread blocked on a semaphore until pthread_join() has returned.
--- .../interfaces/pthread_join/speculative/6-1.c | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+)
testcases/open_posix_testsuite/conformance/interfaces/pthread_join/speculative/6-1.c +27 -0
@@ -25,18 +25,30 @@* Steps: * 1. Create a new thread that is non-joinable (i.e. detached state * attribute is PTHREAD_CREATE_DETACHED. +* The thread blocks until released. * 2. Call pthread_join() in main. It should return EINVAL. * If not, the test fails. +* 3. Release the thread. +* +* The thread must still be running while pthread_join() is called. Once a +* detached thread has exited its ID is no longer valid and the behavior of +* pthread_join() is then undefined. */ #include <pthread.h> +#include <semaphore.h> #include <stdio.h> #include <unistd.h> #include <errno.h> #include "posixtest.h" +static sem_t gate; +static void *a_thread_func () { +if (sem_wait (&gate) !=0 ) +perror ("sem_wait()" ); +pthread_exit (0 );return NULL ; }
@@ -47,6 +59,11 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)pthread_attr_t attr;int ret; +if (sem_init (&gate,0 ,0 ) !=0 ) { +perror ("sem_init()" ); +return PTS_UNRESOLVED ; + } +pthread_attr_init (&attr);pthread_attr_setdetachstate (&attr,PTHREAD_CREATE_DETACHED );
@@ -55,7 +72,17 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)return PTS_UNRESOLVED ; } +/* +* The thread cannot get past sem_wait() before the post below, +* so it is alive and detached. +*/ ret =pthread_join (new_th,NULL ); + +if (sem_post (&gate) !=0 ) { +perror ("sem_post()" ); +return PTS_UNRESOLVED ; + } +if (ret !=EINVAL ) {printf ("Test FAILED: Return code should be EINVAL, " "but is: %d instead.\n" , ret);
Signature · 5 lines
2.56.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Avinesh,
Quoted 1 line · Avinesh Kumar
openposix: pthread_join/speculative/6-1: Fix race with detached thread exit
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 9 lines · Avinesh Kumar via ltp
From: Avinesh Kumar <[email protected]>
The test creates a detached thread whose body returns immediately, then expects
pthread_join()on its ID to fail with EINVAL. Once the thread has exited, its ID is no longer valid and the behavior is undefined. The outcome depends on scheduling, so the test fails sporadically, mostly on single CPU machines.Keep the thread blocked on a semaphore until
pthread_join()has returned.
I believe we've already had a discussion about this test. Calling pthread_join() on detached threads is undefined behavior according to the latest POSIX standard. The test should simply be dropped.
https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_join.html
Signature · 10 lines
Martin Doucha [email protected] SW Quality Engineer SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic -- Mailing list info: https://lists.linux.it/listinfo/ltp