public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] Fix sporadic XFAILs in gdb.threads/attach-many-short-lived-threads.exp
@ 2024-04-06  4:46 Bernd Edlinger
  2024-04-06 17:40 ` Andrew Burgess
  0 siblings, 1 reply; 2+ messages in thread
From: Bernd Edlinger @ 2024-04-06  4:46 UTC (permalink / raw)
  To: gdb-patches, Thiago Jung Bauermann

This is about random test failures like those:

XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 6: attach (EPERM)
XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 7: attach (EPERM)
XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: attach (EPERM)
XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 9: attach (EPERM)
XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 10: attach (EPERM)

The reason for this effect is apparently as follows:

There is a race condition when gdb tries to attach a thread but the
thread exits at the same time.  Normally when that happens the return
code of ptrace(PTRACE_ATTACH, x) is EPERM, which could also have other
reasons.  To detect the true reason, we try to open /proc/<pid>/status
which normally fails in that situation, but it may happen that the
fopen succeeds, and the thread disappears while reading the content,
then the read has the errno=ESRCH, use that as an indication that the
thread has exited between fopen and reading of the status file.
---
 gdb/nat/linux-procfs.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

v2: from kernel code review, it seems the missing "State:"
 can only happen if the thread disappeared, so no need to
 look at errno at all here.

diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
index e2086952ce6..8d46d5bf289 100644
--- a/gdb/nat/linux-procfs.c
+++ b/gdb/nat/linux-procfs.c
@@ -157,17 +157,12 @@ linux_proc_pid_is_gone (pid_t pid)
   enum proc_state state;
 
   have_state = linux_proc_pid_get_state (pid, 0, &state);
-  if (have_state < 0)
+  if (have_state <= 0)
     {
-      /* If we can't open the status file, assume the thread has
-	 disappeared.  */
+      /* If we can't open the status file or there is no "State:" line,
+	 assume the thread has disappeared.  */
       return 1;
     }
-  else if (have_state == 0)
-    {
-      /* No "State:" line, assume thread is alive.  */
-      return 0;
-    }
   else
     return (state == PROC_STATE_ZOMBIE || state == PROC_STATE_DEAD);
 }
-- 
2.39.2


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

* Re: [PATCH v2] Fix sporadic XFAILs in gdb.threads/attach-many-short-lived-threads.exp
  2024-04-06  4:46 [PATCH v2] Fix sporadic XFAILs in gdb.threads/attach-many-short-lived-threads.exp Bernd Edlinger
@ 2024-04-06 17:40 ` Andrew Burgess
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Burgess @ 2024-04-06 17:40 UTC (permalink / raw)
  To: Bernd Edlinger, gdb-patches, Thiago Jung Bauermann

Bernd Edlinger <bernd.edlinger@hotmail.de> writes:

Thanks for looking into these failures.

> This is about random test failures like those:
>
> XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 6: attach (EPERM)
> XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 7: attach (EPERM)
> XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 8: attach (EPERM)
> XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 9: attach (EPERM)
> XFAIL: gdb.threads/attach-many-short-lived-threads.exp: iter 10: attach (EPERM)
>
> The reason for this effect is apparently as follows:
>
> There is a race condition when gdb tries to attach a thread but the
> thread exits at the same time.  Normally when that happens the return
> code of ptrace(PTRACE_ATTACH, x) is EPERM, which could also have other
> reasons.  To detect the true reason, we try to open /proc/<pid>/status
> which normally fails in that situation, but it may happen that the
> fopen succeeds, and the thread disappears while reading the content,
> then the read has the errno=ESRCH, use that as an indication that the
> thread has exited between fopen and reading of the status file.

I assume this would be the commit message for this change?  This text
seems to be out of date given the changes in V2 and probably needs
updating.

Thanks,
Andrew



> ---
>  gdb/nat/linux-procfs.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
>
> v2: from kernel code review, it seems the missing "State:"
>  can only happen if the thread disappeared, so no need to
>  look at errno at all here.
>
> diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c
> index e2086952ce6..8d46d5bf289 100644
> --- a/gdb/nat/linux-procfs.c
> +++ b/gdb/nat/linux-procfs.c
> @@ -157,17 +157,12 @@ linux_proc_pid_is_gone (pid_t pid)
>    enum proc_state state;
>  
>    have_state = linux_proc_pid_get_state (pid, 0, &state);
> -  if (have_state < 0)
> +  if (have_state <= 0)
>      {
> -      /* If we can't open the status file, assume the thread has
> -	 disappeared.  */
> +      /* If we can't open the status file or there is no "State:" line,
> +	 assume the thread has disappeared.  */
>        return 1;
>      }
> -  else if (have_state == 0)
> -    {
> -      /* No "State:" line, assume thread is alive.  */
> -      return 0;
> -    }
>    else
>      return (state == PROC_STATE_ZOMBIE || state == PROC_STATE_DEAD);
>  }
> -- 
> 2.39.2


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

end of thread, other threads:[~2024-04-06 17:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-06  4:46 [PATCH v2] Fix sporadic XFAILs in gdb.threads/attach-many-short-lived-threads.exp Bernd Edlinger
2024-04-06 17:40 ` Andrew Burgess

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