[PATCH] Cygwin: pty: Fix nat_pipe_owner_pid when gdb runs non-cygwin app

Takashi Yano <[email protected]> Wed, 8 Jul 2026 22:16:06 +0900
Newsgroups gmane.os.cygwin.patches
Message-ID <[email protected]>
Previously, nat_pipe_owner_pid was wrongly set to 0 when the inferior
of gdb is a non-cygwin app. Due to this bug, running non-cygwin app
repeatedly in gdb causes unexpected crash.

This happens because previsou code in setup_for_non_cygwin_app() set
nat_pipe_owner_pid to exec_dwProcessId which is correct for the case
that the caller is the stub process of non-cygwin app. However, if the
caller is gdb, since the owner should be gdb itself, nat_pipe_owner_pid
should be set to myself->dwProcessId.

Fixes: 1e6c51d74136 ("Cygwin: pty: Reorganize the code path of setting up and closing pcon.")
Signed-off-by: Takashi Yano <[email protected]>
Reviewed-by:
---
 winsup/cygwin/fhandler/pty.cc | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index 1b453a499..37266d8dd 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -4734,7 +4734,11 @@ fhandler_pty_slave::setup_for_non_cygwin_app (bool nopcon,
       fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
       ptys->get_ttyp ()->switch_to_nat_pipe = true;
       if (!process_alive (ptys->get_ttyp ()->nat_pipe_owner_pid))
-	ptys->get_ttyp ()->nat_pipe_owner_pid = myself->exec_dwProcessId;
+	/* In normal case where the current process is the stub process for
+	   non-cygwin app, set owner to exec_dwProcessId (non-cygwin app).
+	   However, in gdb case, gdb itself should be the owner. */
+	ptys->get_ttyp ()->nat_pipe_owner_pid =
+	  myself->exec_dwProcessId ? : myself->dwProcessId;
     }
   bool pcon_enabled = false;
   if (!nopcon)
-- 
2.51.0