Subject: bug#34821: closed (Re: bug#34821: discard_input_tty does not discard pending input, resulting in garbage inserted into the buffer)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 24 May 2020 07:09:01 +0000

Your bug report

#34821: discard_input_tty does not discard pending input, resulting in garbage inserted into the buffer

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
34821: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=34821
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#34821: discard_input_tty does not discard pending input, resulting in garbage inserted into the buffer
From: Stefan Kangas <[email-masked]>
To: Platon Pronko <[email-masked]>, [email-masked]
Date: Sun, 24 May 2020 00:08:37 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Platon Pronko <[email-masked]> writes:

> I am unable to reproduce the issue with the latest build (28.0.50). I
> think this issue can be closed now.

Thanks, I'm closing the bug with this email.

Best regards,
Stefan Kangas

Subject: discard_input_tty does not discard pending input, resulting in garbage inserted into the buffer
From: Platon Pronko <[email-masked]>
To: [email-masked]
Date: Tue, 12 Mar 2019 10:53:29 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi!

Steps to reproduce:

1. Add dummy autosave file (needed for additional delay when starting and reliable bug reproduction):
$ touch /tmp/#test.txt#

2. Launch daemon:
./src/emacs -Q --fg-daemon

3. Start the client:
./lib-src/emacsclient -t /tmp/test.txt

4. type 'a' while emacsclient is starting

5. observe "65;5403;1c" inserted at the start of the new file.

I hunted for the bug and traced it down to terminal-init-xterm and xterm--query. It makes a "Secondary Device Attributes" query, and expects a response with a specific prefix - but if there were characters waiting in input buffer the handler will never see that prefix, and all the bytes will be sent directly to default keyboard handler and inserted into the buffer.

To mitigate the problem xterm--query calls discard-input before sending the query, however for some reason the bytes are still there when read_char is called.

I added a workaround in xterm--query, simple `(while (read-event nil nil 0.01))` (see patch attached). Setting `(setq xterm-query-timeout nil)` switches querying to async mode and also helps (the typed character is not discarded that way, which is even better).

However, I suspect that the real problem lurks in the discard_tty_input, that for some reason does not actually discard the input - but I'm not sure how to debug it.

System details below:

In GNU Emacs 27.0.50 (build 27, x86_64-pc-linux-gnu, GTK+ Version 3.24.5)
  of 2019-03-12 built on the-big-maker
Repository revision: d2270d8fc93b5fb0b82fec4d85d122ea4e38dff3
Repository branch: master
System Description: Arch Linux

Recent messages:
Starting Emacs daemon.
test.txt has auto save data; consider M-x recover-this-file
When done with a buffer, type C-x #
Quit
completing-read-default: Command attempted to use minibuffer while in minibuffer

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND GPM DBUS GSETTINGS GLIB
NOTIFY INOTIFY ACL GNUTLS LIBXML2 FREETYPE M17N_FLT LIBOTF XFT ZLIB
TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM THREADS LIBSYSTEMD JSON PDUMPER
LCMS2 GMP

Important settings:
   value of $LC_TIME: en_SE.UTF-8
   value of $LANG: en_US.UTF-8
   locale-coding-system: utf-8-unix

Major mode: Text

Minor modes in effect:
   tooltip-mode: t
   global-eldoc-mode: t
   electric-indent-mode: t
   mouse-wheel-mode: t
   tool-bar-mode: t
   menu-bar-mode: t
   file-name-shadow-mode: t
   global-font-lock-mode: t
   font-lock-mode: t
   auto-composition-mode: t
   auto-encryption-mode: t
   auto-compression-mode: t
   line-number-mode: t
   transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny seq byte-opt gv
bytecomp byte-compile cconv dired dired-loaddefs format-spec rfc822 mml
easymenu mml-sec password-cache epa derived epg epg-config gnus-util
rmail rmail-loaddefs time-date mm-decode mm-bodies mm-encode mail-parse
rfc2231 mailabbrev gmm-utils mailheader cl-loaddefs cl-lib sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils term/xterm
xterm server elec-pair mule-util tooltip eldoc electric uniquify
ediff-hook vc-hooks lisp-float-type mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page menu-bar rfn-eshadow isearch timer select
scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame cl-generic cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms cp51932
hebrew greek romanian slovak czech european ethiopic indian cyrillic
chinese composite charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded nadvice loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote threads dbusbind inotify lcms2 dynamic-setting
system-font-setting font-render-setting move-toolbar gtk x-toolkit x
multi-tty make-network-process emacs)

Memory information:
((conses 16 50376 6377)
  (symbols 48 6051 1)
  (strings 32 15598 1886)
  (string-bytes 1 511783)
  (vectors 16 7797)
  (vector-slots 8 77209 12548)
  (floats 8 28 256)
  (intervals 56 175 0)
  (buffers 992 13))

diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el
index c4b0a8fb6e..30919988e9 100644
--- a/lisp/term/xterm.el
+++ b/lisp/term/xterm.el
@@ -804,6 +804,7 @@ xterm--query
       ;; Pending input can be mistakenly returned by the calls to
       ;; read-event below: discard it.
       (discard-input)
+      (while (read-event nil nil 0.01))
       (send-string-to-terminal query)
       (while handlers
         (let ((handler (pop handlers))