From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Platon Pronko Newsgroups: gmane.emacs.bugs Subject: bug#34821: discard_input_tty does not discard pending input, resulting in garbage inserted into the buffer Date: Sun, 7 Apr 2019 19:06:52 +0300 Message-ID: References: <6e2317c9-5063-3718-740e-f53c70f5db5b@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="57041"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.3 To: 34821@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Apr 07 18:08:19 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hDALA-000Efy-UV for geb-bug-gnu-emacs@m.gmane.org; Sun, 07 Apr 2019 18:08:17 +0200 Original-Received: from localhost ([127.0.0.1]:40912 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDAL9-00057O-VD for geb-bug-gnu-emacs@m.gmane.org; Sun, 07 Apr 2019 12:08:15 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:52087) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDAKz-000575-0r for bug-gnu-emacs@gnu.org; Sun, 07 Apr 2019 12:08:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hDAKx-0002RD-Sb for bug-gnu-emacs@gnu.org; Sun, 07 Apr 2019 12:08:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:35071) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hDAKx-0002R3-Ho for bug-gnu-emacs@gnu.org; Sun, 07 Apr 2019 12:08:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1hDAKv-00079G-Oj for bug-gnu-emacs@gnu.org; Sun, 07 Apr 2019 12:08:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Platon Pronko Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 07 Apr 2019 16:08:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 34821 X-GNU-PR-Package: emacs Original-Received: via spool by 34821-submit@debbugs.gnu.org id=B34821.155465322427389 (code B ref 34821); Sun, 07 Apr 2019 16:08:01 +0000 Original-Received: (at 34821) by debbugs.gnu.org; 7 Apr 2019 16:07:04 +0000 Original-Received: from localhost ([127.0.0.1]:48615 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hDAJz-00077h-WB for submit@debbugs.gnu.org; Sun, 07 Apr 2019 12:07:04 -0400 Original-Received: from mail-lj1-f172.google.com ([209.85.208.172]:42422) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1hDAJx-00077C-2G for 34821@debbugs.gnu.org; Sun, 07 Apr 2019 12:07:01 -0400 Original-Received: by mail-lj1-f172.google.com with SMTP id v22so9058268lje.9 for <34821@debbugs.gnu.org>; Sun, 07 Apr 2019 09:07:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=36TTsJpKmHVZ1K4Kq8oIMo8bEQyq9UMECgGxuKSq+rw=; b=PR+d3HkcLY6IHf96SFVXy44w+Q0NVEB9XqkYLVpLkmRdUZAjjz3V8n5tmbKdox/aGW wsZu3ZdJs4VSnZVcEQRx4nRnqs/JFT64weVSVzgjR/qKV36XxL14rGaV8v5/1cPHyj3s e34+jBSeVGZ6qzxHKhoQZ8R+AgVLST8Dukl0m15/mzZ+tIyaIqEqMBk6VSb1cpGgAjbE L9OkUNNvNwfa90n10XpCocS6gFq/FciyMHPlPLQEvjxDpQt0qFkZVSdlmVHkpdG7QkIf jSiNtvNErxoHs1s1yDhI4sXrCDO8SZPvQS2ZUuZNzjaDl7DgeH2SFr5orU51urvFfKLP GDhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=36TTsJpKmHVZ1K4Kq8oIMo8bEQyq9UMECgGxuKSq+rw=; b=ukKlSPxJCmODlZvE2OLgvg/Vxysf9gmG8p+td1Z9vg8u5dCJBaAelA752ySb9a5zK9 O4S711ArGTX1e15OKF3FHdiA0/Oc+7CQpoI3Op+gfpNLeT6X57n8xbCzmtpchP57wevX p8oOzWzDww7xJmKYpDXkz3zfaoZgdoU9tOOywc3AFICClcBsWDJG9J/kd9LSAZAYURwj b931tVVV2REIFwsxdWVlEm9JiekrjdSPhciMa6pVihODtQww+OXXGnXWKUEJ2JMtwSxH tFWBuCKsSDH3sg3EPZk/qhQzZ3YECKqFRUpySHcY1MCQI/nuJpUSOQH6uMEzfTRxgeBH dTZQ== X-Gm-Message-State: APjAAAUkp95rLRZTI8+bd+KW3e9Qj6J9aMrQfnXnEnkADgp0YPRc2fOq pX7nuN9MtnZrWLBoO0NEyIkTzaHv X-Google-Smtp-Source: APXvYqx1Vb0ycH11KdDqs8Qvn+1SGZqmeqSLNrzTf/wf+y29vlkg8usp2FenSoH52cjAMnCDzkVn8w== X-Received: by 2002:a2e:3803:: with SMTP id f3mr13778692lja.165.1554653214747; Sun, 07 Apr 2019 09:06:54 -0700 (PDT) Original-Received: from [192.168.1.65] (109-252-80-126.nat.spd-mgts.ru. [109.252.80.126]) by smtp.gmail.com with ESMTPSA id z13sm5798771ljk.41.2019.04.07.09.06.53 for <34821@debbugs.gnu.org> (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Sun, 07 Apr 2019 09:06:53 -0700 (PDT) In-Reply-To: <6e2317c9-5063-3718-740e-f53c70f5db5b@gmail.com> Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:157312 Archived-At: I decided to go with async approach, since it is the one that is at least theoretically fool-proof. Currently I am trying to debug the problem of "[>65;5600;1c" appearing sporadically (10%) of the times in the input buffer. The test case is below: 1. Start the daemon and set it to use async term query: $ ./src/emacs -Q --fg-daemon --eval "(setq xterm-query-timeout nil)" 2. Launch emacsclient: $ ./lib-src/emacsclient -t /tmp/test.txt 3. About 1 in 10 times, observe "[>65;5600;1c" appearing in the buffer. I tracked the problem to src/keyboard.c:read_key_sequence() function. In normal case, the following happens: 1. read_ke_sequence() is called for the first time. 2. Current input_decode_map is copied: indec.map =  indec.map = indec.parent = KVAR (current_kboard, Vinput_decode_map); 3. It calls read_char() and blocks. 4. When emacsclient is invoked, read_char() returns the "buffer" object as per this comment: /* When switching to a new tty (with a new keyboard),   read_char returns the new buffer, rather than -2   (Bug#5095).  This is because `terminal-init-xterm'   calls read-char, which eats the wrong_kboard_jmpbuf   return.  Any better way to fix this? -- cyd */ 5. `interrupted_kboard != current_kboard` condition is triggered, this read_char() result is discarded and we jump to replay_entire_sequence. 6. read_char() is called again. 7. (terminal-init-xterm) function is invoked. 8. (xterm-query) registeres a handlers for "\e[?" and "\e[>" escape sequences. 9. read_char() returns the "buffer" object the second time. 10. That read_char() result triggers `interrupted_kboard != current_kboard` condition again, and we jump to replay_entire_sequence again. 11. Immediately after that jump, we reset the input map: `indec.map =  indec.map = indec.parent = KVAR (current_kboard, Vinput_decode_map);`, thus pulling in handlers from step 8. 12. The subsequent read_char() calls return the next SDA response characters, match is found in indec.map and xterm--verision-handler is executed. For a situation when bug manifests steps 1-8 are the same, however at step 9 the difference happens - read_char() does not return a buffer, instead it returns the first character of the response (0x1b). `interrupted_kboard != current_kboard` is still true at that moment, thus this character is discarded. Subsequent SDA response characters (0x5b, 0x3e) fail to find a prefix match in input_decode_map, and are inserted into the buffer as-is. I tried changing condition `interrupted_kboard != current_kboard` to `interrupted_kboard != current_kboard && BUFFERP(key)`, but that does not trigger `goto replay_entire_sequence` and thus new handlers are not pulled in from Vinput_decode_map, and so the characters are still inserted into the buffer. Can somebody please advise me about the right course of action in this case? Should I figure out a way to load Vinput_decode_map when new character arrives and `interrupted_kboard != current_kboard`? Or should I look into why read_char() does not return a buffer the second time?