[LTP] [PATCH] openposix: pthread_join/speculative/6-1: Fix race with detached thread exit

Avinesh Kumar avinesh.kumar@suse.com
Thu Nov 12 22:49:30 CET 2026


From: Avinesh Kumar <avinesh.kumar@suse.com>

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.

Signed-off-by: Avinesh Kumar <avinesh.kumar@suse.com>
---
 .../interfaces/pthread_join/speculative/6-1.c | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_join/speculative/6-1.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_join/speculative/6-1.c
index 127b724c69e4..232fd4cc33ac 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/pthread_join/speculative/6-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/pthread_join/speculative/6-1.c
@@ -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);
-- 
2.56.0



More information about the ltp mailing list