Currently, the buffer of 128KB is passed to GetConsoleProcessList().
This causes page fault in the select() loop for console due to:
https://github.com/microsoft/terminal/issues/18264
because the previous code calls GetConsoleProcessList() with large
buffer and PeekConsoleInput() with small buffer alternately.
With this patch, the minimum buffer size is used that is determined
by GetConsoleProcessList() with small buffer passed.

Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256841.html
Fixes: 72770148ad0a ("Cygwin: pty: Prevent pty from changing code page of 
parent console.")
Reported-by: Steven Buehler <buehle...@outlook.com>
Signed-off-by: Takashi Yano <takashi.y...@nifty.ne.jp>
---
 winsup/cygwin/fhandler/termios.cc | 14 ++++++++++++--
 winsup/cygwin/release/3.5.5       |  3 +++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/termios.cc 
b/winsup/cygwin/fhandler/termios.cc
index 585e6ac4a..3cbdf7fca 100644
--- a/winsup/cygwin/fhandler/termios.cc
+++ b/winsup/cygwin/fhandler/termios.cc
@@ -870,8 +870,18 @@ fhandler_termios::get_console_process_id (DWORD pid, bool 
match,
   DWORD *list = (DWORD *) tp.c_get ();
   const DWORD buf_size = NT_MAX_PATH / sizeof (DWORD);
 
-  DWORD num = GetConsoleProcessList (list, buf_size);
-  if (num == 0 || num > buf_size)
+  DWORD buf_size1 = 1;
+  DWORD num;
+  /* The buffer of too large size does not seem to be expected by new condrv.
+     https://github.com/microsoft/terminal/issues/18264#issuecomment-2515448548
+     Use the minimum buffer size in the loop. */
+  while ((num = GetConsoleProcessList (list, buf_size1)) > buf_size1)
+    {
+      if (num > buf_size)
+       return 0;
+      buf_size1 = num;
+    }
+  if (num == 0)
     return 0;
 
   DWORD res_pri = 0, res = 0;
diff --git a/winsup/cygwin/release/3.5.5 b/winsup/cygwin/release/3.5.5
index d41d168c6..7ccf28abf 100644
--- a/winsup/cygwin/release/3.5.5
+++ b/winsup/cygwin/release/3.5.5
@@ -48,3 +48,6 @@ Fixes:
 
 - sched_setscheduler(2) allows to change the priority if the policy is
   equal to the value returned by sched_getscheduler(2).
+
+- Fix frequent page fault caused in Windows Terminal.
+  Addresses: https://cygwin.com/pipermail/cygwin/2024-December/256841.html
-- 
2.45.1

Reply via email to