delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2022/06/17/09:40:03

X-Recipient: archive-cygwin AT delorie DOT com
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 07E953857344
Authentication-Results: sourceware.org;
dmarc=pass (p=none dis=none) header.from=gmail.com
Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:from:date:message-id:subject:to;
bh=gUvbIrYvpd9Yz5aBv5z8dXsPNqAWWS3Gh3rC5viW1RU=;
b=YmrHIcKaspIaNE5WrAt3gh4BU22khFnBmV81JHN7WMWG0TJpin7u5tnDm2Lvn/0Y7v
Ou1IlsbvFEts/cz6RmYyyeClcrkIefmGG9HcTtxXeiVKI7gQomKCp2t/SYAY6+VDk8vQ
F/wpC689Ix7v2w8q2GvVARXd0PBHpmRnXg+0M2zkc3MCvQMtqjB6TUblKLuvR69V9old
xSlLJx9NLgJh46Fk1Jm1juhdk1DVFcizi1UmNDBo84MlaNzNXeGrIq7yOTDb2kQcLZlA
tVIJvkUaUDaZpVEGk4M9vqyZYh2COKi31QMRT2HxBg3IDeyKwK2QXSZFHOxD4jaXjxAn
NYlw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:from:date:message-id:subject:to;
bh=gUvbIrYvpd9Yz5aBv5z8dXsPNqAWWS3Gh3rC5viW1RU=;
b=6C09WTbYPWgzFde/Qj7wSCuj1B0xQWp396z4738JKguqBZZDPTWo5THqeG2Mb/E5XE
tVI9i8CEiEA6UZ9hI1r1Biuc9ZQ7Eqc2ZuTnyusbNJs4CP+g3oWJZ6fZNtZ27DXFMmsj
YZClYtThD63rBa0mN+rNwxvdlzfxWswZTNnKYhlynjtvydg0EDxKOY+SA4b039ueBqZ+
T7PDFtmy5SSxe4A2FDRq79gd7/SKpVptYYCqJmEO8YbzkJeFe5xe0rO6reTrg9WbHKUH
lZQh9okKMwTbpLTEYRvP0imFVrH7n2nilGQ8CHTW6gaNVJMJiPs0Pbs4lFS/dO1u0bma
3jQw==
X-Gm-Message-State: AJIora+QtkeTRTjvOShSfiYjYeX0L8UIzXU5iDNN1M7tj+fE4fbux7ad
dvkcuMGlILsdsXpQMufHFYcq35c932nCQeLwrTTgLyc38No=
X-Google-Smtp-Source: AGRyM1u1VdjyH5xRe4VX3J7kMTXG8iMd2B0dqJ/xzgJWpqXEUZ/SaBhX0cHsdwN0Rh0ovMRZoV10O8aTzlW0rwkGkTY=
X-Received: by 2002:a05:6870:349:b0:f1:928d:c803 with SMTP id
n9-20020a056870034900b000f1928dc803mr5200592oaf.19.1655473162648; Fri, 17 Jun
2022 06:39:22 -0700 (PDT)
MIME-Version: 1.0
From: Tomi Belan <tomi DOT belan AT gmail DOT com>
Date: Fri, 17 Jun 2022 15:38:57 +0200
Message-ID: <CACUV5ofVqAs37Fv1uzAkCNTSQtm0-5BxQ01UyCWWUss-MBTUpg@mail.gmail.com>
Subject: Long window title escape sequence breaks console output
To: cygwin AT cygwin DOT com
X-Spam-Status: No, score=0.9 required=5.0 tests=BAYES_00, DKIM_SIGNED,
DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,
SPF_HELO_NONE, SPF_PASS, TXREP,
T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on
server2.sourceware.org
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.29
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com>

When a program prints the "\e]0;xxx\a" escape sequence to change the
window title, but the window title string is longer than approx. 256
characters, it breaks the terminal. It will start ignoring any console
output until approximately the next "\e" escape sequence, such as a
shorter title change.

Test script:

#!/bin/bash
printf '(1) setting short title\n'
printf '\e]0;short title\a'
printf '(2) short title was set\n'
sleep 1
printf '(3) setting long title\n'
printf '\e]0;long title long title long title long title long title
long title long title long title long title long title long title long
title long title long title long title long title long title long
title long title long title long title long title long title long
title long title\a'
printf '(4) long title was set\n'
sleep 1
printf '(5) setting short title\n'
printf '\e]0;short title\a'
printf '(6) short title was set\n'

Bad output (alternative 1):

(1) setting short title
(2) short title was set
(3) setting long title
(6) short title was set

Bad output (alternative 2):

(1) setting short title
(2) short title was set
(3) setting long title
0;short title(6) short title was set

Notice that lines (4) and (5) are just completely gone.

I reproduced the bug in all these scenarios:

a) Run cmd.exe (either directly, or in Windows Terminal, or in
Wezterm). Run "c:\cygwin\bin\bash.exe -i -l" to start cygwin. Run
"bash test.sh" in cygwin.
b) Run Windows Terminal with a MSYS2 profile
(https://www.msys2.org/docs/terminals/). Run "bash test.sh" in msys2.
c) Run cmd.exe (either directly, or in Windows Terminal, or in
Wezterm). Run "c:\msys64\usr\bin\ssh.exe some_linux_server". Run "bash
test.sh" on the remote side.

But the bug does NOT happen in these scenarios:

d) Run cygwin in Mintty with the "Cygwin64 Terminal" start menu
shortcut. Run "bash test.sh" in cygwin.
e) Run MSYS2 in Mintty with the "MSYS2 MSYS" start menu shortcut. Run
"bash test.sh" in msys2.
f) Run MSYS2 in Mintty with the "MSYS2 MSYS" start menu shortcut. Run
"ssh some_linux_server". Run "bash test.sh" on the remote side.
g) Run cmd.exe (either directly, or in Windows Terminal, or in
Wezterm). Run "ssh some_linux_server" (using Windows 10's built-in ssh
command C:\Windows\System32\OpenSSH\ssh.exe). Run "bash test.sh" on
the remote side.
h) Run "wezterm.exe ssh some_linux_server" to use Wezterm's built in
ssh client. Run "bash test.sh" on the remote side.

The bug ONLY happens if you use a non-mintty terminal AND any
Cygwin-based exe is involved (a bash.exe or ssh.exe from Cygwin or
MSYS2). It confused me for a long time because I didn't know if it was
a terminal bug, a ConPty bug, a MSYS2 bug, or a bash bug. But when I
discovered the msys2 ssh.exe does not act the same as the win10
ssh.exe, it finally pointed me in the right direction. It is in fact a
bug in Cygwin, specifically winsup.

I think I also managed to track down the exact root cause. "strace
--mask=0xffffff bash test.sh" was helpful for that, but be careful
because strace's own output can also get eaten.

For non-pty stdout (not running in Mintty), winsup parses the text for
escape sequences so that it may convert them to legacy win32 console
API calls if needed. When in the "gettitle" state, winpty appends all
characters to "wpbuf". [1] When (*src < ' '), it ends the "gettitle"
state. For legacy consoles it could call SetConsoleTitleW [2], but in
my case on Windows 10 it just flushes wpbuf to the output [3] and lets
Windows (ConPty, I think?) parse it again [4]. The bug is that wpbuf
has a limited size of 256 and any more characters are ignored [5]. So
what happens is that Cygwin truncates the "\e]0;long title...long
title\a" escape sequence and doesn't print the final \a. This makes
the terminal (or maybe ConPty?) think it's still reading a window
title, and that's why it doesn't print lines (4) and (5). It recovers
when it sees the next escape sequence.

[1] http://www.cygwin.com/git?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler_console.cc;hb=fdbd1539329ad669606767ab5a63a16f825b4c45#l3564
[2] http://www.cygwin.com/git?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler_console.cc;hb=fdbd1539329ad669606767ab5a63a16f825b4c45#l3792
[3] http://www.cygwin.com/git?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler_console.cc;hb=fdbd1539329ad669606767ab5a63a16f825b4c45#l3569
[4] https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#window-title
[5] http://www.cygwin.com/git?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler_console.cc;hb=fdbd1539329ad669606767ab5a63a16f825b4c45#l79

This theory explains why it worked in Mintty (it uses the
fhandler_pty_slave code path), why it affects both Cygwin and MSYS2
(they're both Cygwin-based), and why 256 characters is the threshold
(WPBUF_LEN) rather than 1024 (TITLESIZE) or unlimited.

I hope you enjoyed the bug report. Let me know if you have any questions.

Tomi

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019