[PATCH] Cygwin: pty: preserve terminal replies after PTY adoption
Johannes Schindelin via GitGitGadget
gitgitgadget@gmail.com
Sun Sep 13 12:30:05 GMT 2026
From: Johannes Schindelin <johannes.schindelin@gmx.de>
This fixes an issue reported about MSYS2/Win32 interaction, but applies
to regular Cygwin as well. In the following, the original scenario is
described.
MSYS Vim launched by native Git from Bash in MinTTY can delete the first
line, enter Insert mode or insert RGB text at startup. PTY adoption sends
Vim's output directly to MinTTY, but input stays with ConHost until the
first Cygwin read/select. Vim inherits Git's foreground process group, so
the process-group callback does not trigger an earlier handoff.
Vim sends terminal queries before reading. ConHost interprets cursor
position reports as function keys, which the later input transfer delivers
to Vim as keystrokes. A color reply split across the handoff reaches Vim
without its introducer. Fragmented writes are valid; inconsistent routing
corrupts their contents.
Requesting noncanonical input with tcsetattr() provides a control point
before these queries. Handing input to the Cygwin PTY there aligns both
directions before replies can be misdirected, rather than waiting for
consumption. Only the foreground process on an active pseudoconsole takes
this path; canonical, background and pcon-disabled calls are unaffected.
The existing synchronized transfer preserves queued input and line
discipline. Balancing the temporary reader registration without transferring
back keeps input on the Cygwin pipe without a permanent reader claim or
changes to native-owner cleanup. Releasing the output mutex first avoids
holding it across the input-transfer path.
Addresses: https://github.com/git-for-windows/git/issues/6403
Fixes: 6eed1ef74869 ("Cygwin: pty: detect pcon-backed pty for non-Cygwin-spawned children")
Assisted-by: GPT-6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
Cygwin: pty: preserve terminal replies after PTY adoption
I just merged this into Git for Windows' fork of the MSYS2 runtime, as
it is a somewhat pressing problem due to Git for Windows v2.56.0-rc0
already being overdue.
This patch is based on cygwin-3_6-branch, but is easily cherry-picked
onto master, too.
Published-As: https://github.com/cygwingitgadget/cygwin/releases/tag/pr-10%2Fdscho%2Ffix-vim-terminal-replies-v1
Fetch-It-Via: git fetch https://github.com/cygwingitgadget/cygwin pr-10/dscho/fix-vim-terminal-replies-v1
Pull-Request: https://github.com/cygwingitgadget/cygwin/pull/10
winsup/cygwin/fhandler/pty.cc | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index c5081bcaf..f6e051b9e 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -1637,6 +1637,15 @@ fhandler_pty_slave::tcsetattr (int, const struct termios *t)
acquire_output_mutex (mutex_timeout);
get_ttyp ()->ti = *t;
release_output_mutex ();
+
+ /* Terminal queries can precede the first read/select after entering
+ noncanonical mode. Their replies must bypass the native console. */
+ if (!(t->c_lflag & ICANON) && get_ttyp ()->pcon_activated
+ && get_ttyp ()->getpgid () == myself->pgid)
+ {
+ mask_switch_to_nat_pipe (true, true);
+ mask_switch_to_nat_pipe (false, false);
+ }
return 0;
}
base-commit: c19c1e6ad9d8abad640ffcb95996642cc35955ec
--
cygwingitgadget
More information about the Cygwin-patches
mailing list