Skip to contentSkip to conversation

pthread_detach/1-2: Fix race condition between thread exit and join

PATCH In review Wei Gao via ltp 3 participants 9 messages May 8 – Jun 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pthread_detach/1-2: Fix race condition between thread exit and join

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.

---
 .../interfaces/pthread_detach/1-2.c           | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)
testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +25 -0
index dc5ed39d3..1a9650f49 100644
@@ -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");
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by LTP AI Reviewer on v1 1/1
#1 lore raw

Hi Wei,

Quoted 5 lines · Wei Gao

pthread_detach/1-2: Fix race condition between thread exit and join

testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +3 -0
+       /* Wait for the main thread to allow exit */
+       do {
+               ret = sem_wait(&sem_exit);

testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) · line 234

Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +8 -0
+       }
+       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.

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v2 1/1 pthread_detach_1-2: Fix failure on modern glibc

In reply to Wei Gao via 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.

---
v1->v2:
- Update commit message for root cause detail

 .../interfaces/pthread_detach/1-2.c           | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)
testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +25 -0
index dc5ed39d3..1a9650f49 100644
@@ -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");
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by LTP AI Reviewer on v2 1/1
#3 lore raw

Re: [LTP] pthread_detach_1-2: Fix failure on modern glibc

Hi Wei,

Quoted 10 lines · Wei Gao

pthread_detach_1-2: Fix failure on modern glibc

testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +8 -0
+	/* 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");
+	}

[...]

Quoted 4 lines
testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +4 -0
+	ret = sem_init(&sem_exit, 0, 0);
+	if (ret != 0) {
+		UNRESOLVED(errno, "Failed to init the exit semaphore");
+	}

[...]

testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) · line 234

Quoted 1 line
testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +1 -0
+	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.

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc

In reply to Wei Gao via 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.

Quoted 86 lines · Wei Gao via 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().

(quoted diff) +0 -4
-  /* 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.

---
v1->v2:
- Update commit message for root cause detail

  .../interfaces/pthread_detach/1-2.c           | 25 +++++++++++++++++++
  1 file changed, 25 insertions(+)
testcases/open_posix_testsuite/conformance/interfaces/pthread_detach/1-2.c +25 -0
index dc5ed39d3..1a9650f49 100644
@@ -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");
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
#6 lore raw

Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc

Quoted 5 lines · Martin Doucha

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.

Wording:
1)Under the DESCRIPTION section, it explicitly states:

Quoted 1 line

"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.

Quoted 11 lines
Signature · 8 lines
-- 
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc

Quoted 7 lines · Wei Gao
Quoted 5 lines · Martin Doucha

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.

Quoted 7 lines

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

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.

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
#8 lore raw

Re: [LTP] [PATCH v2] pthread_detach_1-2: Fix failure on modern glibc

@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
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp