diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc index d4c3f1020b..dc43cd9f59 100644 --- a/winsup/cygwin/fhandler/console.cc +++ b/winsup/cygwin/fhandler/console.cc @@ -818,6 +818,7 @@ fhandler_console::set_input_mode (tty::cons_mode m, const termios *t, GetConsoleMode (p->input_handle, &oflags); DWORD flags = oflags & (ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE); + con.curr_input_mode = m; switch (m) { case tty::restore: @@ -867,6 +868,7 @@ fhandler_console::set_output_mode (tty::cons_mode m, const termios *t, if (con.orig_virtual_terminal_processing_mode) flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; WaitForSingleObject (p->output_mutex, mutex_timeout); + con.curr_output_mode = m; switch (m) { case tty::restore: @@ -1109,12 +1111,12 @@ fhandler_console::bg_check (int sig, bool dontsignal) /* Setting-up console mode for cygwin app. This is necessary if the cygwin app and other non-cygwin apps are started simultaneously in the same process group. */ - if (sig == SIGTTIN) + if (sig == SIGTTIN && con.curr_input_mode != tty::cygwin) { set_input_mode (tty::cygwin, &tc ()->ti, get_handle_set ()); set_disable_master_thread (false, this); } - if (sig == SIGTTOU) + if (sig == SIGTTOU && con.curr_output_mode != tty::cygwin) set_output_mode (tty::cygwin, &tc ()->ti, get_handle_set ()); return fhandler_termios::bg_check (sig, dontsignal); @@ -2921,6 +2923,8 @@ fhandler_console::char_command (char c) } if (con.args[i] == 1) /* DECCKM */ con.cursor_key_app_mode = (c == 'h'); + if (con.args[i] == 9001) /* win32-input-mode (https://github.com/microsoft/terminal/blob/main/doc/specs/%234999%20-%20Improved%20keyboard%20handling%20in%20Conpty.md) */ + set_disable_master_thread (c == 'h', this); } /* Call fix_tab_position() if screen has been alternated. */ if (need_fix_tab_position) diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc index 040176ea48..8077082ee1 100644 --- a/winsup/cygwin/fhandler/pipe.cc +++ b/winsup/cygwin/fhandler/pipe.cc @@ -54,6 +54,16 @@ fhandler_pipe::set_pipe_non_blocking (bool nonblocking) NTSTATUS status; IO_STATUS_BLOCK io; FILE_PIPE_INFORMATION fpi; + bool was_blocking_read_pipe_new = was_blocking_read_pipe; + + if (get_device () == FH_PIPER && nonblocking && !was_blocking_read_pipe) + { + status = NtQueryInformationFile (get_handle (), &io, &fpi, sizeof fpi, + FilePipeInformation); + if (NT_SUCCESS (status)) + was_blocking_read_pipe_new = + (fpi.CompletionMode == FILE_PIPE_QUEUE_OPERATION); + } fpi.ReadMode = FILE_PIPE_BYTE_STREAM_MODE; fpi.CompletionMode = nonblocking ? FILE_PIPE_COMPLETE_OPERATION @@ -62,6 +72,11 @@ fhandler_pipe::set_pipe_non_blocking (bool nonblocking) FilePipeInformation); if (!NT_SUCCESS (status)) debug_printf ("NtSetInformationFile(FilePipeInformation): %y", status); + else + { + was_blocking_read_pipe = was_blocking_read_pipe_new; + is_blocking_read_pipe = !nonblocking; + } } int @@ -95,6 +110,8 @@ fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id) even with FILE_SYNCHRONOUS_IO_NONALERT. */ set_pipe_non_blocking (get_device () == FH_PIPER ? true : is_nonblocking ()); + was_blocking_read_pipe = false; + return 1; } @@ -289,6 +306,9 @@ fhandler_pipe::raw_read (void *ptr, size_t& len) if (!len) return; + if (is_blocking_read_pipe) + set_pipe_non_blocking (true); + DWORD timeout = is_nonblocking () ? 0 : INFINITE; DWORD waitret = cygwait (read_mtx, timeout); switch (waitret) @@ -721,6 +741,8 @@ fhandler_pipe::close () CloseHandle (query_hdl); if (query_hdl_close_req_evt) CloseHandle (query_hdl_close_req_evt); + if (was_blocking_read_pipe) + set_pipe_non_blocking (false); int ret = fhandler_base::close (); ReleaseMutex (hdl_cnt_mtx); CloseHandle (hdl_cnt_mtx); @@ -1377,6 +1399,7 @@ fhandler_pipe::spawn_worker (int fileno_stdin, int fileno_stdout, { fhandler_pipe *pipe = (fhandler_pipe *)(fhandler_base *) cfd; pipe->set_pipe_non_blocking (false); + need_send_noncygchld_sig = true; } /* If multiple writers including non-cygwin app exist, the non-cygwin @@ -1402,3 +1425,21 @@ fhandler_pipe::spawn_worker (int fileno_stdin, int fileno_stdout, t->kill_pgrp (__SIGNONCYGCHLD); } } + +void +fhandler_pipe::sigproc_worker (void) +{ + cygheap_fdenum cfd (false); + while (cfd.next () >= 0) + if (cfd->get_dev () == FH_PIPEW) + { + fhandler_pipe *pipe = (fhandler_pipe *)(fhandler_base *) cfd; + if (pipe->need_close_query_hdl ()) + pipe->close_query_handle (); + } + else if (cfd->get_dev () == FH_PIPER) + { + fhandler_pipe *pipe = (fhandler_pipe *)(fhandler_base *) cfd; + pipe->is_blocking_read_pipe = true; + } +} diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h index 3d819f49b5..000004479b 100644 --- a/winsup/cygwin/local_includes/fhandler.h +++ b/winsup/cygwin/local_includes/fhandler.h @@ -1197,6 +1197,8 @@ class fhandler_pipe: public fhandler_pipe_fifo private: HANDLE read_mtx; pid_t popen_pid; + bool was_blocking_read_pipe; + bool is_blocking_read_pipe; HANDLE query_hdl; HANDLE hdl_cnt_mtx; HANDLE query_hdl_proc; @@ -1287,6 +1289,7 @@ class fhandler_pipe: public fhandler_pipe_fifo } static void spawn_worker (int fileno_stdin, int fileno_stdout, int fileno_stderr); + static void sigproc_worker (void); }; #define CYGWIN_FIFO_PIPE_NAME_LEN 47 @@ -2160,6 +2163,8 @@ class dev_console char *cons_rapoi; bool cursor_key_app_mode; bool disable_master_thread; + tty::cons_mode curr_input_mode; + tty::cons_mode curr_output_mode; bool master_thread_suspended; int num_processed; /* Number of input events in the current input buffer already processed by cons_master_thread(). */ diff --git a/winsup/cygwin/release/3.5.5 b/winsup/cygwin/release/3.5.5 new file mode 100644 index 0000000000..904119a387 --- /dev/null +++ b/winsup/cygwin/release/3.5.5 @@ -0,0 +1,9 @@ +Fixes: +------ + +- Fix undesired behaviour of console master thread in win32-input-mode + which is supported by Windows Termainal. + Addresses: https://cygwin.com/pipermail/cygwin/2024-August/256380.html + +- Fix a regression in 3.5.4 that writing to pipe extremely slows down. + Addresses: https://cygwin.com/pipermail/cygwin/2024-August/256398.html diff --git a/winsup/cygwin/sigproc.cc b/winsup/cygwin/sigproc.cc index a89f09d087..5c5ff73f0a 100644 --- a/winsup/cygwin/sigproc.cc +++ b/winsup/cygwin/sigproc.cc @@ -1475,14 +1475,7 @@ wait_sig (VOID *) } break; case __SIGNONCYGCHLD: - cygheap_fdenum cfd (false); - while (cfd.next () >= 0) - if (cfd->get_dev () == FH_PIPEW) - { - fhandler_pipe *pipe = (fhandler_pipe *)(fhandler_base *) cfd; - if (pipe->need_close_query_hdl ()) - pipe->close_query_handle (); - } + fhandler_pipe::sigproc_worker (); break; } if (clearwait && !have_execed)