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))