unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Platon Pronko <platon7pronko@gmail.com>
To: 34821@debbugs.gnu.org
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	[thread overview]
Message-ID: <cdacd65a-bb3c-a51e-d9b9-a96ae516988c@gmail.com> (raw)
In-Reply-To: <6e2317c9-5063-3718-740e-f53c70f5db5b@gmail.com>

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?







  parent reply	other threads:[~2019-04-07 16:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-12  7:53 bug#34821: discard_input_tty does not discard pending input, resulting in garbage inserted into the buffer Platon Pronko
2019-03-13  8:04 ` Platon Pronko
2019-04-07 16:06 ` Platon Pronko [this message]
2019-04-07 16:37   ` Eli Zaretskii
2019-04-07 17:14     ` Platon Pronko
2019-04-07 18:24       ` Eli Zaretskii
2019-04-07 19:06         ` Platon Pronko
2019-04-07 19:25           ` Eli Zaretskii
2020-05-24  6:23             ` Platon Pronko
2020-05-24  7:08               ` Stefan Kangas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cdacd65a-bb3c-a51e-d9b9-a96ae516988c@gmail.com \
    --to=platon7pronko@gmail.com \
    --cc=34821@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).