From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.133]) by sourceware.org (Postfix) with ESMTPS id 610E53858D28 for ; Tue, 1 Aug 2023 21:05:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 610E53858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=towo.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=towo.net Received: from [192.168.178.72] ([91.64.130.150]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MyK1E-1pdVjN47K8-00yfWm for ; Tue, 01 Aug 2023 23:05:18 +0200 Message-ID: <44ea96ac-25c5-40b3-f8e1-2d6c02a4c8da@towo.net> Date: Tue, 1 Aug 2023 23:05:18 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: Rare character glitch in pty? To: cygwin-developers@cygwin.com References: <6f0bfdf8-a557-0540-0367-6846406f941d@towo.net> From: Thomas Wolff In-Reply-To: <6f0bfdf8-a557-0540-0367-6846406f941d@towo.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:ZIOME7Zp8ADcLQRJTDDZb1fldAdR0e3MdNDdMGzEknD4aMknzjK xQnJkVX6X5AnYTShD0pQbGN4QgMA/KCWiIDq+Of6TNoin6tyFhN1qUOdtcDcLy+c6NA8wVh YPp425VP3p6dBOZmRxJJdooGdg6QUDl3KbSJ2rxCcElTyCV3lfilxO/KKelWa6pRU7Zc5iw ANOB8MuBQOskV7B3bj2FA== UI-OutboundReport: notjunk:1;M01:P0:EgZfD6DVgf8=;KDf7g3GbA7FY+c048+O1mZ3Hthx mdHr99jr+3bBUfHHSkFLOGlWo1ePWOXIFEvcRvL0Eh/eEsundmceKZPQNsvwtiamk+4j5QH8C 715HCTURKR4Oh18oH9yz+4G4QB74UR8Uq0Cz2KpPJFAbruy/KHZ8XhBgGmu8of10d0srw1cdR oK4DYfmKuCQEBtfagkXbYNvYX09Jsh7CA8FxNMMtB/SgyQzUh+/K5Jbk5JW8swbaTZbb2XQLz d13gHec2ETdLdMuguAQoJT5yQ2UsQCmJkw5V/TSgZlBfHuXcRnTbD1Lg3aP6Nw9yCqaEesKbY T4Pz8jaKT9fDf48iK7tKrPm/m5pitRmade24taNYf0jN/uPBuDrOmrHHOPECkBoKmyv08GH6t H2aYI0iP5dcjLf1QPWbGi0nEOqiDv4Mr1CXTdpiJQc7nFTA8P288edihY+bbA77BeNEwUM3g4 ZbEUtwSyZaDV68xzas0KYule3LH+cLQ5ieUDCtSIjaaS7yBIVqlb+zApVz67DXWSZqc7zrky2 VPAnmsbCaFjs3iMZU8mx+6OwOVIYDtVpT+wIqrXHFnKbdLGcZ5Jg9W9T33PPt/Cce2j9jI0es 6NBP9kfJYv/wDUg7+8u9dKzkYq6H0HlYoTB6PG6znAIMMqYkH9UMs9g99yXoVFDfbdCkpVBCS LybdrM8lRF475MWE41BNmTkUYCEZWEzyKSFBbLf7I6K13MQBwgL5zhdAG0x7WBz4CjpSquzeB Nlc9mtSUhAa1zbXVa4FqOSRaNm0JKt9/Q5SFUO2/5Ng1yT11XjzYPq3Urru7iZgRvYz9y/po4 4iAOyz3DAlZjyoiQpCcHydOdC51NAGwQpdAkmusLr+m9I6OSjGfSLdZ6D8zKfNJHSZxw6Ywjz b+PHQ03SIxE16mw== X-Spam-Status: No, score=3.1 required=5.0 tests=BAYES_00,BODY_8BITS,KAM_DMARC_STATUS,KAM_LOTSOFHASH,NICE_REPLY_A,RCVD_IN_BARRACUDACENTRAL,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I had reported this to the the main cygwin list: Am 20.07.2023 um 08:33 schrieb Thomas Wolff via Cygwin: > There was a report that on switching terminal windows with the mouse, > focus event reports (as enabled with ESC[?1004h) would sometimes get > mangled. > This is further described in issue reports to mintty/wsltty > (https://github.com/mintty/wsltty/issues/335) and also to tmux > (https://github.com/tmux/tmux/issues/3601). > > I could reproduce the issue without either of tmux or WSL. However, it > happens only once in a few hundred instances: > The escape sequence is changed so that the initial ESC is replaced by > one of the two subsequent characters, so instead of ESC[O, either of > [[O or sometimes O[O would be sent to the foreground application. > > I traced the issue down to the pty that connects mintty and its client > application, verified that the correct sequence is passed into write() > at the pty and the mangled sequence appears at read() in the client > process. In addition to a minimised test program that behaves like > `cat -v`, I also also put the test output directly into the client > process within the mintty code. > This leaves the only conclusion that the issue must be happening > somehow within pty handling, maybe in the context of some timing > condition or Windows messages being passed to mintty. > Anyhow, I was about to prepare testing with older mintty versions, in > case I overlooked something, when the OP reported that the issue does > not happen with cygwin 3.2.0. > Is there any recent change that could possibly cause such weird > behaviour? > Thomas > I have now automated the test case to chase the issue and associate it with a cygwin version. As unlikely as it may appear, results suggest that there is a case within the pty handling that may change sequences sent through it once in a while, more likely on a system under heavy load. I took some of the latest cygwin snapshots (by the way, there are no newer ones on the snapshots page): I can reproduce the issue with     3.3.5-341.x86_64.snap 2022-02-17 11:53 UTC while it did not occur with     3.4.0-341.x86_64.snap 2022-01-27 14:34 UTC Weird enough, the failing newer one is a cygwin 3.3.5 although the older one is a 3.4.0 version. Checking the git log around that time, there are a number of changes related to pty as listed below. I added some ! or ? marks to indicate more or less likely commits to be a culprit of this issue, judged on commit messages and browsing of code changes. I would also try to identify a commit more precisely but unfortunately the cygwin build process seems broken for me as it does not create the dll anymore... Thomas commit 2f2b91550547001b005393097c5a4328451343d6 Author: Takashi Yano Date:   Thu Feb 24 00:57:37 2022 +0900     Cygwin: pty, console: Add a workaround for GDB SIGINT handling.     - The inferior of the GDB cannot be continued after SIGINT even       though nopass option is set. This seems because cygwin GDB does       not support hooking cygwin signal. Therefore, a workaround for       GDB is added. With this patch, only CTRL_C_EVENT is sent to the       GDB inferior by Ctrl-C and sending SIGINT is omitted. Note that       "handle SIGINT (no)pass" command does not take effect even with       or without this patch. commit 2cab4d0bb4af5ad7ec97914068d5fa1b8954909c Author: Takashi Yano Date:   Tue Feb 15 23:23:50 2022 +0900 ?     Cygwin: pty, console: Refactor the code processing special keys.     - This patch commonize the code which processes special keys in pty       and console to improve maintanancibility. As a result, some small       bugs have been fixed. commit bed1add783a13b3304b1625b962707f89e90e323 Author: Takashi Yano Date:   Sun Feb 13 05:12:18 2022 +0900     Cygwin: pty: Fix a bug in tty_min::segpgid().     - In tty_min::setpgid(), a pointer to fhandler instance is casted to       fhandler_pty_slave and accessed even if terminal is not a pty slave.       This patch fixes the issue. commit c4704c7c204e377859f657a3b06c112133adff09 Author: Takashi Yano Date:   Sun Feb 13 02:16:32 2022 +0900     Cygwin: pty: Discard input in from_master_nat pipe on signal as well.     - Currently, pty discards input only in from_master pipe on signal.       Due to this, if pty is started without pseudo console support and       start a non-cygwin process from cmd.exe, type adhead input is not       discarded on signals such as Ctrl-C. This patch fixes the issue. commit b958e1f03acd1732bb0a433a589efb645458aac4 Author: Takashi Yano Date:   Thu Feb 10 17:47:51 2022 +0900 ?     Cygwin: pty: Revise the code to wait for completion of forwarding.     - With this patch, the code to wait for completion of forwarding of       output from non-cygwin app is revised so that it can more reliably       detect the completion. commit bddd9c1c417e97791c44241cf94753e90464501c Author: Takashi Yano Date:   Thu Feb 10 17:22:11 2022 +0900 !?     Cygwin: pty: Prevent deadlock on echo output.     - If the slave process writes a lot of text output, doecho() can       cause deadlock. This is because output_mutex is held in slave::       write() and if WriteFile() is blocked due to pipe full, doecho()       tries to acquire output_mutex and gets into deadlock. With this !      patch, the deadlock is prevented on the sacrifice of atomicity       of doecho(). commit b04aea00f1bb503dcc364c83ee3ef3da3bc1305e Author: Takashi Yano Date:   Sat Feb 12 17:51:55 2022 +0900 ??     Cygwin: pty: Pass Ctrl-Z (EOF) to non-cygwin apps with disable_pcon.     - Previously, non-cygwin app running in pty started without pseudo       console support was suspended by Ctrl-Z rather than sending EOF.       Even worse, suspended app could not be resumed by fg command. With       this patch, Ctrl-Z (EOF for non-cygwin apps) is passed to non-cygwin       app instead of suspending that app. This patch also handles Ctrl-\       (QUIT) and Ctrl-D (EOF) as well. commit c05c36a7c8af5e97fa379abb7571ed35a0088e98 Author: Takashi Yano Date:   Thu Feb 10 16:53:47 2022 +0900 ??     Cygwin: pty, console: Fix Ctrl-C handling for non-cygwin apps.     - Currently, if cat is started from cmd.exe which is started in cygwin       console, Ctrl-C terminates not only cat but also cmd.exe. This also       happens in pty in which pseudo console is disabled. This patch fixes       the issue. commit 909ed837ccb932ea70f71cc41891fa2c8143133f Author: Takashi Yano Date:   Fri Jan 14 19:35:12 2022 +0900 ??     Cygwin: pty: Fix race issue between closing and opening master.     - If the from_master is closed before cleaning up other pipes, such       as from_slave_nat, the same pty may be allocated and pty master may       try to open the pipe which is not closed yet, and it will fail.       This patch fixes the issue. commit 3af461092e7ee9b76f3a1c18a6d95ed6e226df81 Author: Takashi Yano Date:   Thu Jan 13 19:44:43 2022 +0900 !?     Cygwin: pty: Stop closing and recreating attach_mutex.     - Closing attach_mutex and recreating it causes the race issue       between pty and console codes. With this patch, attach_mutex       is created only once in a process which opens pty, and never       closed in order to avoid this issue.     Addresses: https://cygwin.com/pipermail/cygwin-developers/2021-December/012548.html commit 4f490c4cef8fd527642dbb5ea5373b373193a49b Author: Takashi Yano Date:   Thu Jan 13 18:18:00 2022 +0900     Cygwin: pty: Fix memory leak in master_fwd_thread.     - If master_fwd_thread is terminated by cygthread::terminate_thread(),       the opportunity to release tmp_pathbuf is missed, resulting in a       memory leak. This patch fixes the issue. commit aa49985245e4e5b278778b0a47f6ce83cceb4ad4 Author: Takashi Yano Date:   Thu Jan 13 18:16:31 2022 +0900 ?     Cygwin: pty, console: Fix deadlock in GDB regarding mutex.     - GDB inferior may be suspended while the inferior grabs mutex.       This causes deadlock in terminal I/O. With this patch, timeout       for waiting mutex is set to 0 for the debugger process when the       process calls CreateProcess() with DEBUG_PROCESS flag to avoid       deadlock. This may cause the race issue in GDB, however, there       is no other way than that.     Addresses: https://cygwin.com/pipermail/cygwin-developers/2021-December/012542.html