Subject: bug#32537: 26.1.50; Tramp: Cursor jumps when typing during asynchronous find-file

From: Gemini Lasswell <[email-masked]>
Date: Wed, 29 Aug 2018 10:55:35 -0700

Eli Zaretskii <[email-masked]> writes:

> Hmm... does that mean tramp-sh-handle-file-attributes runs with
> *scratch* is its current buffer?  It was *scratch* that you were
> typing into when this point jumps happened, right?  It's strange that
> Tramp uses the current buffer for its processing, but Michael should
> know.

Yes, I was typing into *scratch*.  I'm a gdb newbie, so here's what I
did:

1. From a shell, cd to src, run ./emacs -Q
2. M-x global-font-lock-mode RET
3. From another shell, cd to src, determine PID of Emacs and then run
   gdb -p PID
4. Enter following commands to gdb:

source .gdbinit
set logging on
set height 0
p current_thread->m_current_buffer
break Fmake_thread
cont

5. Return to Emacs and enter:
   C-x C-f C-a C-k /scp:server:src/emacs/lisp/emacs-lisp/b*.el RET
6. In gdb, enter the following:

clear
break set_point_both if current_thread->m_current_buffer == $1
commands
xbacktrace
cont
end
break temp_set_point_both if buffer == $1
commands
xbacktrace
cont
end
cont

8. Return to Emacs and type until the cursor makes a backward jump.
9. C-x C-c

> Wait a minute, why does self-insert-command run in a non-main thread?
> Could it be that somehow a non-main thread started receiving and
> interpreting your keyboard input?  (The "find-file /scp:" thread is
> not the main thread, right?)

It looks to me like Thread 1 is the main thread, and the thread names
printed by gdb don't match what Emacs thinks they are.  Thread 1 starts
out as "emacs" but its name changes after I continue from the
Fmake_thread breakpoint:

Thread 1 "emacs" hit Breakpoint 3, Fmake_thread (function=XIL(0x4867b15), 
    name=XIL(0x45b85a4)) at thread.c:768
768	{
Deleted breakpoint 3 
Breakpoint 4 at 0x635530: file intervals.c, line 1826.
Type commands for breakpoint(s) 4, one per line.
End with a line saying just "end".
Breakpoint 5 at 0x635360: file intervals.c, line 1729.
Type commands for breakpoint(s) 5, one per line.
End with a line saying just "end".
Continuing.
[New Thread 0x7f1cad3e1700 (LWP 9151)]

Thread 1 "find-file /scp:" hit Breakpoint 5, temp_set_point_both (
    buffer=0xdb5800 <bss_sbrk_buffer+458720>, charpos=charpos@entry=146, 
    bytepos=bytepos@entry=146) at intervals.c:1729
1729	{
"redisplay_internal (C function)" (0x0)

And then later in gdb.txt this thread shows up:

Thread 6 "emacs" hit Breakpoint 5, temp_set_point_both (
    buffer=0xdb5800 <bss_sbrk_buffer+458720>, charpos=charpos@entry=151, 
    bytepos=bytepos@entry=151) at intervals.c:1729
1729	{
"redisplay_internal (C function)" (0x0)
"message" (0xaca48708)
"apply" (0xaca48700)
"tramp-sh-handle-file-local-copy" (0xaca492d8)
"apply" (0xaca492d0)
"tramp-sh-file-name-handler" (0xaca49700)
"apply" (0xaca496f8)
"tramp-file-name-handler" (0xaca4a750)
"file-local-copy" (0xaca4aa70)
"tramp-handle-insert-file-contents" (0xaca4b278)
"apply" (0xaca4b3c0)
"tramp-sh-file-name-handler" (0xaca4b668)
"apply" (0xaca4b838)
"tramp-file-name-handler" (0xaca4c878)
"insert-file-contents" (0xaca50eb8)
"find-file-noselect-1" (0xaca51270)
"find-file-noselect" (0xaca51810)
0x4860a20 PVEC_COMPILED