Subject: bug#70938: 30.0.50; Edebug appropriates overriding-terminal-local-map

From: Stephen Berman <[email-masked]>
Date: Tue, 14 May 2024 15:24:10 +0200

While I was edebugging in todo-mode.el (bug#70937) Emacs got into a
state where Edebug kicked in even after I removed the instrumentation
from the code.  AFAICT from further debugging, the issue has to do with
the way todo-mode.el uses set-transient-map.  To provide a reproducer
that does not involve todo-mode.el I have extracted and simplified the
triggering function from that file and included it in the attached file.

0. emacs -Q -l test-edebug-override-map.el
1. M-x srb-test
2. At the prompt type one of the keys listed between the curly braces,
   which removes that group and prompts again; keeping typing till there
   is no further prompt, then a message displays the typed keys in the
   order they were typed in.
3. Now visit test-edebug-override-map.el and with point in the function
   srb-test--next type `C-u C-M-x' to instrument it for Edebug.
4. Type M-s srb-test again and when Edebug stops execution, type `f'
   twice to step through the entire function so Edebug releases control.
5. Now with point again in srb-test--next type `C-M-x' to remove the
   instrumentation.  You can confirm this by repeating steps 1-2 without
   Edebug kicking.
6. Now type any of the keys that were listed in the prompt on invoking
   srb-test (these keys are the strings in the value of
   srb-test--key-groups).
=> This makes Edebug kick in with execution stopped at one of the two
   occurrences of the sexp `(setq keys-so-far (concat keys-so-far " "
   (funcall this-key)))' in srb-test--next.  The same thing happens with
   sequences containg one of these keys, e.g. `M-x k'.

I did find a way to prevent Edebug from kicking in: uncomment the lines
in the test file containing "exitfn"; this explicitly deactivates the
transient map when the function returns, but it also makes the function
a noop, so it's no fix for the problem.

After step 4., when I type `C-h b' to show all current key bindings, the
*Help* buffer contains a listing like this:

 Overriding Bindings:

Key             Binding

c .. d		#<interpreted-function AA2>
h		#<interpreted-function BAB>
i		#<interpreted-function 8CC>
k		#<interpreted-function 9AC>
n		#<interpreted-function A7F>
p		#<interpreted-function 917>
r		#<interpreted-function B53>
t		#<interpreted-function BD6>
y		#<interpreted-function 9F1>

The codes AA2, etc. are links, and clicking on these pops up a *Help
Source* buffer containing elisp code like this:

#[nil
  ((edebug-enter 'edebug-anon5 (list)
		 #'(lambda nil :closure-dont-trim-context
		     (edebug-after (edebug-before 0) 7
				   (setq keys-so-far
					 (edebug-after
					  (edebug-before 1) 6
					  (concat
					   (edebug-after 0 2
							 keys-so-far)
					   " "
					   (edebug-after
					    (edebug-before 3) 5
					    (funcall
					     (edebug-after 0 4
							   this-key)))))))

This is evidently from the instrumented function.  What I haven't been
able to determine is why this remains even after the instrumentation has
been removed; it seems that Edebug has appropriated
overriding-terminal-local-map.  The only way I have found to release it
is `M-x unload-library RET edebug'.

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.41, cairo version 1.18.0) of 2024-05-08 built on strobelfs
Repository revision: e020f4e9ce5d98438033fea098d943c311b0fa3d
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101013
System Description: Linux From Scratch r12.1-53

Configured using:
 'configure -C 'CFLAGS=-Og -g3' PKG_CONFIG_PATH=/opt/qt5/lib/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LCMS2 LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PNG
RSVG SECCOMP SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER
WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB