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; 9+ 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] 9+ 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; 9+ 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] 9+ 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
                     ` (2 more replies)
  1 sibling, 3 replies; 9+ 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] 9+ 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
  2026-06-11  8:04   ` [LTP] [PATCH v2] " Andrea Cervesato via ltp
  2026-06-11 15:33   ` Martin Doucha
  2 siblings, 0 replies; 9+ 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] 9+ messages in thread

* Re: [LTP] [PATCH v2] 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   ` [LTP] " linuxtestproject.agent
@ 2026-06-11  8:04   ` Andrea Cervesato via ltp
  2026-06-11 15:33   ` Martin Doucha
  2 siblings, 0 replies; 9+ messages in thread
From: Andrea Cervesato via ltp @ 2026-06-11  8:04 UTC (permalink / raw)
  To: Wei Gao via ltp; +Cc: ltp

Reviewed-by: Andrea Cervesato <andrea.cervesato@suse.com>

--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

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

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

* Re: [LTP] [PATCH v2] 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   ` [LTP] " linuxtestproject.agent
  2026-06-11  8:04   ` [LTP] [PATCH v2] " Andrea Cervesato via ltp
@ 2026-06-11 15:33   ` Martin Doucha
  2026-06-12  2:05     ` Wei Gao via ltp
  2 siblings, 1 reply; 9+ messages in thread
From: Martin Doucha @ 2026-06-11 15:33 UTC (permalink / raw)
  To: Wei Gao, ltp

Hi,
this looks like trying to hide a glibc bug to me. I've looked at both 
the POSIX standard and Linux man pages and there is no mention that 
already exited threads may be left joinable. I think we should report 
this as a bug and wait for response from glibc maintainers.

On 5/9/26 09:39, Wei Gao via ltp wrote:
> 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");


-- 
Martin Doucha   mdoucha@suse.cz
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

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

* Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc
  2026-06-11 15:33   ` Martin Doucha
@ 2026-06-12  2:05     ` Wei Gao via ltp
  2026-06-12  8:38       ` Martin Doucha
  0 siblings, 1 reply; 9+ messages in thread
From: Wei Gao via ltp @ 2026-06-12  2:05 UTC (permalink / raw)
  To: Martin Doucha; +Cc: Adhemerval Zanella, ltp

On Thu, Jun 11, 2026 at 05:33:31PM +0200, Martin Doucha wrote:
> Hi,
> this looks like trying to hide a glibc bug to me. I've looked at both the
> POSIX standard and Linux man pages and there is no mention that already
> exited threads may be left joinable. I think we should report this as a bug
> and wait for response from glibc maintainers.

I think is actually not a glibc bug, but correct behavior under POSIX.

Link: The Open Group - pthread_detach (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_detach.html)

Wording: 
1)Under the DESCRIPTION section, it explicitly states:
    > "The behavior is undefined if the value specified by the thread argument to pthread_detach() does not refer to a joinable thread."

2)Under the CHANGE HISTORY section also mention:

Issue 6
The pthread_detach() function is marked as part of the Threads option.
IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/95 is applied, updating the ERRORS section so that the [EINVAL] and [ESRCH] error cases become optional. <<<<<<

Issue 7
The pthread_detach() function is moved from the Threads option to the Base.
Austin Group Interpretation 1003.1-2001 #142 is applied, removing the [ESRCH] error condition. <<<<<<<<

Also include Adhemerval Zanella who submit the glibc change.

> 
> 
> 
> -- 
> Martin Doucha   mdoucha@suse.cz
> 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

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

* Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc
  2026-06-12  2:05     ` Wei Gao via ltp
@ 2026-06-12  8:38       ` Martin Doucha
  2026-06-15 13:28         ` Andrea Cervesato via ltp
  0 siblings, 1 reply; 9+ messages in thread
From: Martin Doucha @ 2026-06-12  8:38 UTC (permalink / raw)
  To: Wei Gao; +Cc: Adhemerval Zanella, ltp

On 6/12/26 04:05, Wei Gao wrote:
> On Thu, Jun 11, 2026 at 05:33:31PM +0200, Martin Doucha wrote:
>> Hi,
>> this looks like trying to hide a glibc bug to me. I've looked at both the
>> POSIX standard and Linux man pages and there is no mention that already
>> exited threads may be left joinable. I think we should report this as a bug
>> and wait for response from glibc maintainers.
> 
> I think is actually not a glibc bug, but correct behavior under POSIX.
> 
> Link: The Open Group - pthread_detach (https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_detach.html)
> 
> Wording:
> 1)Under the DESCRIPTION section, it explicitly states:
>      > "The behavior is undefined if the value specified by the thread argument to pthread_detach() does not refer to a joinable thread."

Every thread that was created as joinable remains joinable until one of 
two things happens:
1) It gets joined by pthread_join(). In this case, the thread handle 
becomes invalid.
2) It gets detached by pthread_detach().

If a thread has already exited before it got detached, it is still 
joinable, otherwise pthread_join() would be fundamentally broken.

But POSIX issue 7 has also changed the specification of errors for 
pthread_join() and calling it on invalid thread handle is now undefined 
behavior. I think we should drop the test entirely, the scenario is no 
longer valid according to the latest POSIX.

-- 
Martin Doucha   mdoucha@suse.cz
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

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

* Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc
  2026-06-12  8:38       ` Martin Doucha
@ 2026-06-15 13:28         ` Andrea Cervesato via ltp
  0 siblings, 0 replies; 9+ messages in thread
From: Andrea Cervesato via ltp @ 2026-06-15 13:28 UTC (permalink / raw)
  To: Martin Doucha; +Cc: ltp, Adhemerval Zanella

@Martin, can you please send a patch to remove this test?
I will move the patch to rejected state.

Thanks,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

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

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

end of thread, other threads:[~2026-06-15 13:28 UTC | newest]

Thread overview: 9+ 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
2026-06-11  8:04   ` [LTP] [PATCH v2] " Andrea Cervesato via ltp
2026-06-11 15:33   ` Martin Doucha
2026-06-12  2:05     ` Wei Gao via ltp
2026-06-12  8:38       ` Martin Doucha
2026-06-15 13:28         ` Andrea Cervesato via ltp

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.