All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1] pthread_detach/1-2: Fix race condition between thread exit and join
@ 2026-05-08 10:00 Wei Gao via ltp
  2026-05-08 11:20 ` [LTP] " linuxtestproject.agent
  2026-05-09  7:39 ` [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc Wei Gao via ltp
  0 siblings, 2 replies; 4+ messages in thread
From: Wei Gao via ltp @ 2026-05-08 10:00 UTC (permalink / raw)
  To: ltp

Latest test in our openqa setup(kernel 7.0.3) show following error:

FAILED: We were able to join a detached thread.

Add a second synchronization semaphore to ensure the child thread remains alive until
the main thread has completed its checks can make test case pass.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 .../interfaces/pthread_detach/1-2.c           | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c
index dc5ed39d3..1a9650f49 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c
@@ -92,6 +92,7 @@
 /********************************************************************************************/
 
 static unsigned int sc;
+static sem_t sem_exit;
 
 static void *threaded(void *arg)
 {
@@ -113,6 +114,15 @@ static void *threaded(void *arg)
 		UNRESOLVED(errno, "Failed to post the semaphore");
 	}
 
+	/* Wait for the main thread to allow exit */
+	do {
+		ret = sem_wait(&sem_exit);
+	}
+	while ((ret == -1) && (errno == EINTR));
+	if (ret == -1) {
+		UNRESOLVED(errno, "Failed to wait for the exit semaphore");
+	}
+
 	return arg;
 }
 
@@ -125,6 +135,11 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 
 	scenar_init();
 
+	ret = sem_init(&sem_exit, 0, 0);
+	if (ret != 0) {
+		UNRESOLVED(errno, "Failed to init the exit semaphore");
+	}
+
 	for (sc = 0; sc < NSCENAR; sc++) {
 #if VERBOSE > 0
 		output("-----\n");
@@ -202,11 +217,21 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 				    ("We were able to join a detached thread.");
 			}
 
+			/* Allow the thread to exit */
+			do {
+				ret = sem_post(&sem_exit);
+			}
+			while ((ret == -1) && (errno == EINTR));
+			if (ret == -1) {
+				UNRESOLVED(errno, "Failed to post the exit semaphore");
+			}
+
 			/* Let the thread an additionnal row to cleanup */
 			sched_yield();
 		}
 	}
 
+	sem_destroy(&sem_exit);
 	scenar_fini();
 #if VERBOSE > 0
 	output("-----\n");
-- 
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] pthread_detach/1-2: Fix race condition between thread exit and join
  2026-05-08 10:00 [LTP] [PATCH v1] pthread_detach/1-2: Fix race condition between thread exit and join Wei Gao via ltp
@ 2026-05-08 11:20 ` linuxtestproject.agent
  2026-05-09  7:39 ` [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc Wei Gao via ltp
  1 sibling, 0 replies; 4+ messages in thread
From: linuxtestproject.agent @ 2026-05-08 11:20 UTC (permalink / raw)
  To: Wei Gao; +Cc: ltp, LTP AI Reviewer

Hi Wei,

On Fri, 8 May 2026 10:00:58 +0000, Wei Gao wrote:
> pthread_detach/1-2: Fix race condition between thread exit and join

> +       /* Wait for the main thread to allow exit */
> +       do {
> +               ret = sem_wait(&sem_exit);
> +       }
> +       while ((ret == -1) && (errno == EINTR));

> +       /* Allow the thread to exit */
> +       do {
> +               ret = sem_post(&sem_exit);
> +       }
> +       while ((ret == -1) && (errno == EINTR));

> +       sem_destroy(&sem_exit);

Correct fix. The second semaphore ensures the child is still alive when
pthread_join() is called, eliminating the race.

Reviewed-by: LTP AI Reviewer <ltp-ai@noreply.github.com>

Pre-existing issues noticed in the surrounding code (not introduced
by this patch):

- 1-2.c: uses testfrmw.h harness with test_main() instead of posixtest.h
  and main() — not introduced by this patch.

---
Note:

Our agent completed the review of the patch.

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc
  2026-05-08 10:00 [LTP] [PATCH v1] pthread_detach/1-2: Fix race condition between thread exit and join Wei Gao via ltp
  2026-05-08 11:20 ` [LTP] " linuxtestproject.agent
@ 2026-05-09  7:39 ` Wei Gao via ltp
  2026-05-15  9:56   ` [LTP] " linuxtestproject.agent
  1 sibling, 1 reply; 4+ messages in thread
From: Wei Gao via ltp @ 2026-05-09  7:39 UTC (permalink / raw)
  To: ltp

Latest test in our openqa setup (Linux 7.1-rc2, Glibc 2.43) show following error:
Test FAILED: We were able to join a detached thread.

This is triggered by glibc commit 5da15b15adab661c80e373b6af89be0b5fa5b3ad
("nptl: Do not use pthread set_tid_address as state synchronization (BZ #19951)").

New logic remove the logic of check whether valid thread handle in __pthread_clockjoin_ex().
-  /* Make sure the descriptor is valid.  */
-  if (INVALID_NOT_TERMINATED_TD_P (pd))
-    /* Not a valid thread handle.  */
-    return ESRCH;

So add a second synchronization semaphore to ensure the child thread remains
alive until the main thread has completed its checks. This forces glibc
to see the THREAD_STATE_DETACHED status and correctly return EINVAL.

Signed-off-by: Wei Gao <wegao@suse.com>
---
v1->v2:
- Update commit message for root cause detail

 .../interfaces/pthread_detach/1-2.c           | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c
index dc5ed39d3..1a9650f49 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c
@@ -92,6 +92,7 @@
 /********************************************************************************************/
 
 static unsigned int sc;
+static sem_t sem_exit;
 
 static void *threaded(void *arg)
 {
@@ -113,6 +114,15 @@ static void *threaded(void *arg)
 		UNRESOLVED(errno, "Failed to post the semaphore");
 	}
 
+	/* Wait for the main thread to allow exit */
+	do {
+		ret = sem_wait(&sem_exit);
+	}
+	while ((ret == -1) && (errno == EINTR));
+	if (ret == -1) {
+		UNRESOLVED(errno, "Failed to wait for the exit semaphore");
+	}
+
 	return arg;
 }
 
@@ -125,6 +135,11 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 
 	scenar_init();
 
+	ret = sem_init(&sem_exit, 0, 0);
+	if (ret != 0) {
+		UNRESOLVED(errno, "Failed to init the exit semaphore");
+	}
+
 	for (sc = 0; sc < NSCENAR; sc++) {
 #if VERBOSE > 0
 		output("-----\n");
@@ -202,11 +217,21 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 				    ("We were able to join a detached thread.");
 			}
 
+			/* Allow the thread to exit */
+			do {
+				ret = sem_post(&sem_exit);
+			}
+			while ((ret == -1) && (errno == EINTR));
+			if (ret == -1) {
+				UNRESOLVED(errno, "Failed to post the exit semaphore");
+			}
+
 			/* Let the thread an additionnal row to cleanup */
 			sched_yield();
 		}
 	}
 
+	sem_destroy(&sem_exit);
 	scenar_fini();
 #if VERBOSE > 0
 	output("-----\n");
-- 
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] pthread_detach_1-2: Fix failure on modern glibc
  2026-05-09  7:39 ` [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc Wei Gao via ltp
@ 2026-05-15  9:56   ` linuxtestproject.agent
  0 siblings, 0 replies; 4+ messages in thread
From: linuxtestproject.agent @ 2026-05-15  9:56 UTC (permalink / raw)
  To: Wei Gao; +Cc: ltp, LTP AI Reviewer

Hi Wei,

On Sat, 9 May 2026, Wei Gao wrote:
> pthread_detach_1-2: Fix failure on modern glibc

> +	/* Wait for the main thread to allow exit */
> +	do {
> +		ret = sem_wait(&sem_exit);
> +	}
> +	while ((ret == -1) && (errno == EINTR));
> +	if (ret == -1) {
> +		UNRESOLVED(errno, "Failed to wait for the exit semaphore");
> +	}

[...]

> +	ret = sem_init(&sem_exit, 0, 0);
> +	if (ret != 0) {
> +		UNRESOLVED(errno, "Failed to init the exit semaphore");
> +	}

[...]

> +	sem_destroy(&sem_exit);

Fix looks correct. The extra semaphore ensures the child stays alive past
the pthread_join() probe, so glibc reliably sees THREAD_STATE_DETACHED
and returns EINVAL rather than ESRCH.

Reviewed-by: LTP AI Reviewer <ltp-ai@noreply.github.com>

Pre-existing issues noticed in the surrounding code (not introduced
by this patch):

- 1-2.c:5 — testfrmw.h used instead of posixtest.h; entry point is
  test_main() rather than main() — established convention for this
  test framework subdirectory.
- 1-2.c:178 — break inside #if VERBOSE > 0 causes case 1 fall-through
  when VERBOSE=0; harmless because case 2/default are also gated by the
  same #ifdef.

---
Note:

Our agent completed the review of the patch.

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

end of thread, other threads:[~2026-05-15  9:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-08 10:00 [LTP] [PATCH v1] pthread_detach/1-2: Fix race condition between thread exit and join Wei Gao via ltp
2026-05-08 11:20 ` [LTP] " linuxtestproject.agent
2026-05-09  7:39 ` [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc Wei Gao via ltp
2026-05-15  9:56   ` [LTP] " linuxtestproject.agent

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.