public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Hannes Domani <ssbssa@yahoo.de>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: Re: [PATCH 3/3] Windows: Fix run/attach hang after bad run/attach
Date: Tue, 13 Feb 2024 21:14:00 +0000	[thread overview]
Message-ID: <f2bc98e3-4b81-48a7-a56f-928b501dc09e@palves.net> (raw)
In-Reply-To: <d99922aa-a994-4de7-b312-4c2029f718ef@palves.net>

On 2024-02-13 12:20, Pedro Alves wrote:
> On 2024-02-12 20:14, Hannes Domani wrote:
> 
>> The same problem also exists in the !__CYGWIN__ branch, I ran into this
>> multiple times already.
> 
> Wow, the #ifdef region is so long that I didn't notice this particular code was Cygwin-only.
> 
> I had put "Windows" in the subject as I thought I was fixing native Windows too...
> 
> I'll go fix the !__CYGWIN__ branches too.
> 

Here's an updated version that now handles the !__CYGWIN__ branch too.  I confirmed manually
that I saw the hangs on MinGW gdb without the fix, and that both the "attach" and "run" hangs go away
with this version patch (the previous version still hanged with "run").


---- 8< ----
From af5cae1e2c0bf2cac2c5178d45f70eae5795cf8f Mon Sep 17 00:00:00 2001
From: Pedro Alves <pedro@palves.net>
Subject: [PATCH] Windows: Fix run/attach hang after bad run/attach

On Cygwin, gdb.base/attach.exp exposes that a attach after a
previously failed attach hangs:

 (gdb) PASS: gdb.base/attach.exp: do_attach_failure_tests: attach to digits-starting nonsense is prohibited
 attach 0
 Can't attach to process 0 (error 2: The system cannot find the file specified.)
 (gdb) PASS: gdb.base/attach.exp: do_attach_failure_tests: attach to nonexistent process is prohibited
 attach 10644
 FAIL: gdb.base/attach.exp: do_attach_failure_tests: first attach (timeout)

The problem is that windows_nat_target::attach always returns success
even if the attach fails.  When we return success, the helper thread
begins waiting for events (which will never come), and thus the next
attach deadlocks on the do_synchronously call within
windows_nat_target::attach.

"run" has the same problem, which is exposed by the new
gdb.base/run-fail-twice.exp testcase:

 (gdb) run
 Starting program: .../gdb.base/run-fail-twice/run-fail-twice.nox
 Error creating process .../gdb.base/run-fail-twice/run-fail-twice.nox, (error 6: The handle is invalid.)
 (gdb) PASS: gdb.base/run-fail-twice.exp: test: bad run 1
 run
 Starting program: .../gdb.base/run-fail-twice/run-fail-twice.nox
 FAIL: gdb.base/run-fail-twice.exp: test: bad run 2 (timeout)

The problem here is the same, except that this time it is
windows_nat_target::create_inferior that returns the incorrect result.

This commit fixes both the "attach" and "run" paths, and the latter
both the Cygwin and MinGW paths.  The tests mentioned above now pass
on Cygwin.  Confirmed the fixes manually for MinGW GDB.

Change-Id: I15ec9fa279aff269d4982b00f4ea7c25ae917239
---
 gdb/windows-nat.c | 35 ++++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
index 7f3044fc61d..5c47dd40738 100644
--- a/gdb/windows-nat.c
+++ b/gdb/windows-nat.c
@@ -2059,7 +2059,7 @@ windows_nat_target::attach (const char *args, int from_tty)
       if (!ok)
 	err = (unsigned) GetLastError ();
 
-      return true;
+      return ok;
     });
 
   if (err.has_value ())
@@ -2664,12 +2664,15 @@ windows_nat_target::create_inferior (const char *exec_file,
   windows_init_thread_list ();
   do_synchronously ([&] ()
     {
-      if (!create_process (nullptr, args, flags, w32_env,
-			   inferior_cwd != nullptr ? infcwd : nullptr,
-			   disable_randomization,
-			   &si, &pi))
+      BOOL ok = create_process (nullptr, args, flags, w32_env,
+				inferior_cwd != nullptr ? infcwd : nullptr,
+				disable_randomization,
+				&si, &pi);
+
+      if (!ok)
 	ret = (unsigned) GetLastError ();
-      return true;
+
+      return ok;
     });
 
   if (w32_env)
@@ -2790,16 +2793,18 @@ windows_nat_target::create_inferior (const char *exec_file,
   windows_init_thread_list ();
   do_synchronously ([&] ()
     {
-      if (!create_process (nullptr, /* image */
-			   args,	/* command line */
-			   flags,	/* start flags */
-			   w32env,	/* environment */
-			   inferior_cwd, /* current directory */
-			   disable_randomization,
-			   &si,
-			   &pi))
+      BOOL ok = create_process (nullptr, /* image */
+				args,	/* command line */
+				flags,	/* start flags */
+				w32env,	/* environment */
+				inferior_cwd, /* current directory */
+				disable_randomization,
+				&si,
+				&pi);
+      if (!ok)
 	ret = (unsigned) GetLastError ();
-      return true;
+
+      return ok;
     });
   if (tty != INVALID_HANDLE_VALUE)
     CloseHandle (tty);

base-commit: a16034bf6417dc2259fef43fd5bcc2dd1dac562f
-- 
2.43.0

      reply	other threads:[~2024-02-13 21:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-12 20:01 [PATCH 0/3] "run" and "attach" failure handling problems Pedro Alves
2024-02-12 20:01 ` [PATCH 1/3] Fix "run" failure with GDBserver Pedro Alves
2024-02-13 15:19   ` Lancelot SIX
2024-02-13 21:11     ` Pedro Alves
2024-02-12 20:01 ` [PATCH 2/3] Improve vRun error reporting Pedro Alves
2024-02-13 12:56   ` Pedro Alves
2024-02-13 15:36   ` Lancelot SIX
2024-02-12 20:01 ` [PATCH 3/3] Windows: Fix run/attach hang after bad run/attach Pedro Alves
2024-02-12 20:14   ` Hannes Domani
2024-02-13 12:20     ` Pedro Alves
2024-02-13 21:14       ` Pedro Alves [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f2bc98e3-4b81-48a7-a56f-928b501dc09e@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    --cc=ssbssa@yahoo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).