Subject: bug#51788: cua + ansi-term hangs

From: Samuel Muñoz Hidalgo <[email-masked]>
Date: Fri, 12 Nov 2021 12:22:18 +0100

Hello,

I've tried to implement a small utility function in elisp and I end up with
emacs hanged whenever certain conditions are met.

The utility has to do with sending the selected text to a term buffer and
when in the term buffer if C-c  (or C-x) is pressed, it hangs.

I 've traced the behavior and my guess it that is has to do with cua-mode.

EMACS VERSION:
I've been able to replicate the behaviour in :
- GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo
version 1.16.0)
- GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.14) of
2020-03-26, modified by Debian

CONTEXT:

1- Open an empty emacs instance:
```
/usr/bin/emacs -q
```

2- Paste this code in a buffer (*scratch*)
```
(cua-mode t)

(defun terminal-send-selection()
  (interactive)
  (call-interactively #'kill-ring-save)
  (ansi-term "/usr/bin/bash" "test")
  (term-paste)
  )

(global-set-key [f2] 'terminal-send-selection)
```

3- Eval the buffer with the code `eval-buffer`

CASE 1 (OK):
1- Select a text in a buffer and 'M-x' `terminal-send-selection`. It opens
a terminal with the selected text pasted in it.
3- Type C-c, then prefix key is activated.
4- Type C-c again and works as expected, the pasted text is executed in
bash.

CASE 2 (PROBLEM):
1- Select a text in a buffer and use the keybind F2 instead of calling the
function manually. It opens a terminal with the selected word pasted in it.
3- Type C-c or (C-x) and emacs freezes with a 100% CPU usage in a single
core.
4- Then on a external shell:
```
kill -USR2 `pidof emacs`
```
And I get in the minibuffer.
```
Error in pre-command-hook (term-set-goto-process-mark): (quit)
```
Emacs is still hanged.
5- Repeat the command in the external shell.
```
kill -USR2 `pidof emacs`
```
And I get in *Backtrace*
```
Debugger entered--Lisp error: (quit)
```
The editor is not hanged anymore. But the behaviour is undesired.

CASE 3 (OK):
1- Don't activate cua-mode. Redo al the process but without copying the
line`(cua-mode t)`.
2- Then case 2 work as expected.

OTHER APPROACHES:
Instead of using the function "term-paste" I have also tried with.

1 - Using the function "comint-send-string".
2 - Pasting directly the test in the ansi-term buffer.

With the same results.

Why does the CASE 2 happens?
As the code is so simple I think this has to be a bug. Any help or insights
are welcome.