unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] nptl: Fix tst-cancel7 and tst-cancelx7 pidfile race
@ 2021-10-16 22:41 Stafford Horne via Libc-alpha
  2021-10-18 16:30 ` Adhemerval Zanella via Libc-alpha
  0 siblings, 1 reply; 2+ messages in thread
From: Stafford Horne via Libc-alpha @ 2021-10-16 22:41 UTC (permalink / raw
  To: GLIBC patches

The check for waiting for the pidfile to be created looks wrong.  At the
point when ACCESS is run the pid file will always be created and
accessible as it is created during DO_PREPARE.  This means that thread
cancellation may be performed before the pid is written to the pidfile.

This was found to be flaky when testing on my OpenRISC platform.

Fix this by using the semaphore to wait for pidfile pid write
completion.
---
 nptl/tst-cancel7.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/nptl/tst-cancel7.c b/nptl/tst-cancel7.c
index 54ed2a54ff..e926f4455c 100644
--- a/nptl/tst-cancel7.c
+++ b/nptl/tst-cancel7.c
@@ -115,16 +115,13 @@ do_test (void)
 {
   pthread_t th = xpthread_create (NULL, tf, NULL);
 
-  do
-    nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 100000000 }, NULL);
-  while (access (pidfilename, R_OK) != 0);
+  /* Wait to cancel until after the pid is written.  */
+  if (sem_wait (sem) != 0)
+    FAIL_EXIT1 ("sem_wait: %m");
 
   xpthread_cancel (th);
   void *r = xpthread_join (th);
 
-  if (sem_wait (sem) != 0)
-    FAIL_EXIT1 ("sem_wait: %m");
-
   FILE *f = xfopen (pidfilename, "r+");
 
   long long ll;
-- 
2.31.1


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

* Re: [PATCH] nptl: Fix tst-cancel7 and tst-cancelx7 pidfile race
  2021-10-16 22:41 [PATCH] nptl: Fix tst-cancel7 and tst-cancelx7 pidfile race Stafford Horne via Libc-alpha
@ 2021-10-18 16:30 ` Adhemerval Zanella via Libc-alpha
  0 siblings, 0 replies; 2+ messages in thread
From: Adhemerval Zanella via Libc-alpha @ 2021-10-18 16:30 UTC (permalink / raw
  To: Stafford Horne, GLIBC patches



On 16/10/2021 19:41, Stafford Horne via Libc-alpha wrote:
> The check for waiting for the pidfile to be created looks wrong.  At the
> point when ACCESS is run the pid file will always be created and
> accessible as it is created during DO_PREPARE.  This means that thread
> cancellation may be performed before the pid is written to the pidfile.
> 
> This was found to be flaky when testing on my OpenRISC platform.
> 
> Fix this by using the semaphore to wait for pidfile pid write
> completion.

LGTM, thanks.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>

> ---
>  nptl/tst-cancel7.c | 9 +++------
>  1 file changed, 3 insertions(+), 6 deletions(-)
> 
> diff --git a/nptl/tst-cancel7.c b/nptl/tst-cancel7.c
> index 54ed2a54ff..e926f4455c 100644
> --- a/nptl/tst-cancel7.c
> +++ b/nptl/tst-cancel7.c
> @@ -115,16 +115,13 @@ do_test (void)
>  {
>    pthread_t th = xpthread_create (NULL, tf, NULL);
>  
> -  do
> -    nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 100000000 }, NULL);
> -  while (access (pidfilename, R_OK) != 0);
> +  /* Wait to cancel until after the pid is written.  */
> +  if (sem_wait (sem) != 0)
> +    FAIL_EXIT1 ("sem_wait: %m");
>  
>    xpthread_cancel (th);
>    void *r = xpthread_join (th);
>  
> -  if (sem_wait (sem) != 0)
> -    FAIL_EXIT1 ("sem_wait: %m");
> -
>    FILE *f = xfopen (pidfilename, "r+");
>  
>    long long ll;
> 

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

end of thread, other threads:[~2021-10-18 16:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-10-16 22:41 [PATCH] nptl: Fix tst-cancel7 and tst-cancelx7 pidfile race Stafford Horne via Libc-alpha
2021-10-18 16:30 ` Adhemerval Zanella via Libc-alpha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).