Subject: 28.2; `x-focus-frame' causes spurious input events causes edebug not showing intermediate results
From: "Farblos" <[email-masked]>
To: [email-masked]
Date: Sat, 14 Jan 2023 22:55:15 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

This is Emacs 28.2 from the Debian emacs-lucid package running on fvwm3
on Xorg.  I guess that either the lucid toolkit or fvwm3 is part of the
problem here, since I cannot reproduce this on a gtk3 Emacs.

To demonstrate the problem:

  emacs -Q
  M-x find-function fundamental-mode RET
  C-u C-M-x                           ; instrument function
  C-x 4 b *Messages* RET              ; show message buffer
  C-x o
  M-x fundamental-mode RET            ; call function

Then step through function `fundamental-mode' in edebug using SPC.
While doing so I do not get the intermediate evaluation results
displayed, but rather only the message "Stop".  My message buffer looks
like this after stepping through the function:

  fundamental-mode
  Stop

  Result: nil
  Stop

  Stop

  Result: nil
  Stop

Meaning: These permanent "Stop" messages hide the intermediate
results.  I haven't found much information on this on the net,
only the following thread on gnu.emacs.help:

  https://groups.google.com/g/gnu.emacs.help/c/ojSN-PgNvxg

The cause seems to be the call to function `x-focus-frame' in
function `edebug--display-1':

  (if (not (memq (framep (selected-frame)) '(nil t pc)))
      (x-focus-frame (selected-frame)))

When I instrument that snippet to

  (when (not (memq (framep (selected-frame)) '(nil t pc)))
    (message "input-pending-pre: %S" (input-pending-p))
    (x-focus-frame (selected-frame))
    (message "input-pending-pst: %S" (input-pending-p)))

my message buffer looks like this instead:

  fundamental-mode
  input-pending-pre: t
  input-pending-pst: t
  Stop

  input-pending-pre: nil
  input-pending-pst: t
  Result: nil
  Stop

  input-pending-pre: nil
  input-pending-pst: t
  Stop

  input-pending-pre: nil
  input-pending-pst: t
  Result: nil
  Stop

That spurious pending input caused by `x-focus-frame' triggers
the following snippet later in function `edebug--display-1':

  (when (input-pending-p)
    (setq edebug-stop t)
    (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
    (edebug-stop))

which causes the "Stop" messages.

The following hack works around the problem, at least most of the time,
as the exact behavior also seems to depend on timing:

  (define-advice x-focus-frame (:after (&rest _dummy) emacs-fix)
    "Discards any spurious input to avoid stop events in edebug."
    (let ((backtrace-frame (backtrace-frame 1 'x-focus-frame)))
      (when (and (listp backtrace-frame)
                 (eq (nth 1 backtrace-frame) 'edebug--display-1))
        ;; a little sleep seems to make work-around safer
        (sleep-for 0 50)
        (discard-input))))

If you cannot reproduce (which is rather likely) pls let me know what I
should do to follow up on this.

In GNU Emacs 28.2 (build 2, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw3d scroll bars)
 of 2022-12-20, modified by Debian built on x86-csail-01
Windowing system distributor 'The X.Org Foundation', version 11.0.12101005
System Description: Debian GNU/Linux bookworm/sid

Configured using:
 'configure --build x86_64-linux-gnu --prefix=/usr
 --sharedstatedir=/var/lib --libexecdir=/usr/libexec
 --localstatedir=/var/lib --infodir=/usr/share/info
 --mandir=/usr/share/man --with-libsystemd --with-pop=yes
 --enable-locallisppath=/etc/emacs:/usr/local/share/emacs/28.2/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/28.2/site-lisp:/usr/share/emacs/site-lisp
 --with-sound=alsa --without-gconf --with-mailutils
 --with-native-compilation --build x86_64-linux-gnu --prefix=/usr
 --sharedstatedir=/var/lib --libexecdir=/usr/libexec
 --localstatedir=/var/lib --infodir=/usr/share/info
 --mandir=/usr/share/man --with-libsystemd --with-pop=yes
 --enable-locallisppath=/etc/emacs:/usr/local/share/emacs/28.2/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/28.2/site-lisp:/usr/share/emacs/site-lisp
 --with-sound=alsa --without-gconf --with-mailutils
 --with-native-compilation --with-x=yes --with-x-toolkit=lucid
 --with-toolkit-scroll-bars --without-gsettings 'CFLAGS=-g -O2
 -ffile-prefix-map=/build/emacs-OltuXQ/emacs-28.2+1=. -fstack-protector-strong
 -Wformat -Werror=format-security -Wall' 'CPPFLAGS=-Wdate-time
 -D_FORTIFY_SOURCE=2' LDFLAGS=-Wl,-z,relro'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM HARFBUZZ JPEG JSON LCMS2
LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
X11 XAW3D XDBE XIM XPM LUCID ZLIB

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

Major mode: Outline

Minor modes in effect:
  shell-dirtrack-mode: t
  display-time-mode: t
  delete-selection-mode: t
  tooltip-mode: t
  show-paren-mode: t
  mouse-wheel-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

Load-path shadows:
~/work/elisp/refold hides ~/etc/elisp/refold
~/work/elisp/precious-text hides ~/etc/elisp/precious-text
~/work/elisp/patch hides ~/etc/elisp/patch
~/work/elisp/bbdb-diary hides ~/etc/elisp/bbdb-diary
/usr/share/emacs/site-lisp/elpa/htmlize-1.56/htmlize hides /usr/share/emacs/site-lisp/elpa-src/htmlize-1.56/htmlize
/usr/share/emacs/site-lisp/elpa/htmlize-1.56/htmlize-autoloads hides /usr/share/emacs/site-lisp/elpa-src/htmlize-1.56/htmlize-autoloads
/usr/share/emacs/site-lisp/elpa/htmlize-1.56/htmlize-pkg hides /usr/share/emacs/site-lisp/elpa-src/htmlize-1.56/htmlize-pkg
/usr/share/emacs/site-lisp/elpa/markdown-mode-2.5/markdown-mode-pkg hides /usr/share/emacs/site-lisp/elpa-src/markdown-mode-2.5/markdown-mode-pkg
/usr/share/emacs/site-lisp/elpa/markdown-mode-2.5/markdown-mode hides /usr/share/emacs/site-lisp/elpa-src/markdown-mode-2.5/markdown-mode
/usr/share/emacs/site-lisp/elpa/markdown-mode-2.5/markdown-mode-autoloads hides /usr/share/emacs/site-lisp/elpa-src/markdown-mode-2.5/markdown-mode-autoloads
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-xml-common hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-xml-common
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-http hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-http
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-poem hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-poem
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-backlink hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-backlink
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-wiki hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-wiki
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-split hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-split
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-latex hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-latex
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-ipc hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-ipc
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-context hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-context
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-project hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-project
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-regexps hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-regexps
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-groff hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-groff
/usr/share/emacs/site-lisp/elpa/muse-3.20/httpd hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/httpd
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-pkg hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-pkg
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-import-xml hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-import-xml
/usr/share/emacs/site-lisp/elpa/muse-3.20/cgi hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/cgi
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-texinfo hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-texinfo
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-autoloads hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-autoloads
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-colors hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-colors
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-import-docbook hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-import-docbook
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-blosxom hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-blosxom
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-mode hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-mode
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-html hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-html
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-import-latex hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-import-latex
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-protocols hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-protocols
/usr/share/emacs/site-lisp/elpa/muse-3.20/htmlize-hack hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/htmlize-hack
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-xml hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-xml
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-message hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-message
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-protocol-iw hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-protocol-iw
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-latex2png hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-latex2png
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-ikiwiki hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-ikiwiki
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-mathml hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-mathml
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-cite hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-cite
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-docbook hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-docbook
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-journal hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-journal
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-book hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-book
/usr/share/emacs/site-lisp/elpa/muse-3.20/muse-publish hides /usr/share/emacs/site-lisp/elpa-src/muse-3.20/muse-publish
/usr/share/emacs/site-lisp/elpa/simple-httpd-1.5.1/simple-httpd hides /usr/share/emacs/site-lisp/elpa-src/simple-httpd-1.5.1/simple-httpd
/usr/share/emacs/site-lisp/elpa/simple-httpd-1.5.1/simple-httpd-pkg hides /usr/share/emacs/site-lisp/elpa-src/simple-httpd-1.5.1/simple-httpd-pkg
/usr/share/emacs/site-lisp/elpa/simple-httpd-1.5.1/simple-httpd-autoloads hides /usr/share/emacs/site-lisp/elpa-src/simple-httpd-1.5.1/simple-httpd-autoloads

Features:
(align compare-w diff-mode log-edit add-log bbdb-diary dabbrev log-view
pcvs-util org-element avl-tree generator ol-eww eww xdg url-queue mm-url
ol-rmail ol-mhe ol-irc ol-info ol-gnus nnselect gnus-search gnus-art
mm-uu mml2015 mm-view mml-smime smime dig gnus-sum shr kinsoku svg dom
gnus-group gnus-undo gnus-start gnus-dbus dbus xml gnus-cloud nnimap
nnmail mail-source utf7 netrc nnoo gnus-spec gnus-int gnus-range
gnus-win ol-docview doc-view image-mode exif ol-bibtex ol-bbdb ol-w3m
ol-doi org-link-doi org ob ob-tangle ob-ref ob-lob ob-table ob-exp
org-macro org-footnote org-src ob-comint org-pcomplete org-list
org-faces org-entities org-version ob-emacs-lisp ob-core ob-eval
org-table oc-basic bibtex ol org-keys oc org-compat org-macs
org-loaddefs format-spec shadow sort mail-extr emacsbug message rmc puny
dired dired-loaddefs rfc822 mml mml-sec epa epg rfc6068 epg-config
mm-decode mm-bodies mm-encode mail-parse rfc2231 gmm-utils mailheader
sendmail noutline outline bbdb-mua gnus nnheader gnus-util rmail
rmail-loaddefs rfc2047 rfc2045 ietf-drums mail-utils mm-util mail-prsvr
tex-mode latexenc bbdb-tex shell pcomplete files-x grep compile comint
ansi-color ring rect bbdb-migrate bbdb-com pcase crm mailabbrev bbdb
derived bbdb-site timezone vc-filewise ediff ediff-merg ediff-mult
ediff-wind ediff-diff ediff-help ediff-init ediff-util vc eieio-opt
shortdoc text-property-search ede/speedbar ede/files ede ede/detect
ede/base ede/auto ede/source eieio-base eieio-speedbar speedbar ezimage
dframe eieio-custom cedet mule-util info edebug cal-coptic cal-julian
help-fns radix-tree cl-print debug backtrace thingatpt misearch
multi-isearch vc-dispatcher parse-time iso8601 time-date vc-cvs vc-rcs
refold easy-mmode cus-edit pp cus-start wid-edit jka-compr appt
diary-lib diary-loaddefs cal-menu calendar cal-loaddefs comp comp-cstr
warnings rx cl-extra help-mode patch find-func time delsel cus-load
server advice finder-inf bbdb-loaddefs muse-autoloads package browse-url
url url-proxy url-privacy url-expand url-methods url-history url-cookie
url-domsuf url-util mailcap url-handlers url-parse auth-source cl-seq
eieio eieio-core cl-macs eieio-loaddefs password-cache json subr-x map
url-vars seq byte-opt gv bytecomp byte-compile cconv cl-loaddefs cl-lib
iso-transl tooltip eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode 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 lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer 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 emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting font-render-setting cairo x-toolkit x
multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 704166 122624)
 (symbols 48 28618 68)
 (strings 32 126343 6802)
 (string-bytes 1 4754220)
 (vectors 16 64650)
 (vector-slots 8 1765583 103981)
 (floats 8 572 696)
 (intervals 56 46854 1402)
 (buffers 992 46))