Subject: bug#45518: Ctrl-G Fails to Interrupt Hung Tramp Remote-Compile in Emacs 27.1

From: Michael Albinus <[email-masked]>
Date: Wed, 17 Feb 2021 16:39:43 +0100

Duncan Greatwood <[email-masked]> writes:

> Michael -

Hi Duncan,

> Prompted by your question, I downloaded Tramp 2.5.0.1 from GNU ELPA
> and copied those .el files over the tramp files in .../lisp/net,
> applied the patch in tramp-integration.el and recompiled the macos
> application. I have the patched compile.el included as well, just to
> be clear.

I seriously recommend to use Emacs' package manager for installation,
and NOT to overwrite files in Emacs' installation dir. Experience tells,
that this is always good for trouble in the long run.

> I checked in run time, tramp-compile-advice-add and
> compilation-start-hook are defined as we'd wish.
>
> Unfortunately, I see no change in behaviour. If I click on a syntax
> error message while compiling test.cpp in the autotools project via
> tramp, the compile hangs until I ctrl-g out.
>
> Not sure why the difference vs. your setup, sorry.

Strange. I've reworked the Tramp patch a little bit. Function names have
changed, and more important, it has traces now. Could you please apply
this patch instead of the previous patch in tramp-integration.el? Rerun
your test with tramp-verbose set to 6, and send the Tramp debug buffer
if it still doesn't work as expected.

> Best regards,
> Duncan.

Best regards, Michael.

*** /tmp/edifffETTJu	2021-02-17 16:37:56.993381450 +0100
--- /home/albinus/src/tramp/lisp/tramp-integration.el	2021-02-17 14:12:36.736224836 +0100
***************
*** 41,46 ****
--- 41,47 ----
  (declare-function recentf-cleanup "recentf")
  (declare-function tramp-dissect-file-name "tramp")
  (declare-function tramp-file-name-equal-p "tramp")
+ (declare-function tramp-message "tramp")
  (declare-function tramp-tramp-file-p "tramp")
  (defvar eshell-path-env)
  (defvar ido-read-file-name-non-ido)
***************
*** 261,266 ****
--- 262,303 ----
  		  (delete (info-lookup->mode-cache 'symbol ',mode)
  			  (info-lookup->topic-cache 'symbol))))))))

+ ;;; Integration of compile.el:
+
+ ;; Compilation processes use `accept-process-output' such a way that
+ ;; Tramp's parallel `accept-process-output' blocks.  See last part of
+ ;; Bug#45518.  So we forbid them to run in parallel.  There must be a
+ ;; better solution, though.
+ (defun tramp-compile-disable-goto-error (proc)
+   "Don't allow remote file operations while compiling."
+   (with-current-buffer (process-buffer proc)
+     (when (file-remote-p default-directory)
+       (advice-add 'ignore :override 'compile-goto-error)
+       (tramp-message
+        (tramp-dissect-file-name default-directory) 4
+        "Disable `compile-goto-error'"))))
+
+ (defun tramp-compile-enable-goto-error (buffer _message)
+   "Reenable remote file operations."
+   (with-current-buffer buffer
+     (when (file-remote-p  default-directory)
+       (advice-remove 'ignore 'compile-goto-error)
+       (tramp-message
+        (tramp-dissect-file-name default-directory) 4
+        "Enable `compile-goto-error'"))))
+
+ (with-eval-after-load 'compile
+   (add-hook 'compilation-start-hook
+ 	    #'tramp-compile-disable-goto-error)
+   (add-hook 'compilation-finish-functions
+ 	    #'tramp-compile-enable-goto-error)
+   (add-hook 'tramp-integration-unload-hook
+ 	    (lambda ()
+ 	      (remove-hook 'compilation-start-hook
+ 			   #'tramp-compile-disable-goto-error)
+ 	      (remove-hook 'compilation-finish-functions
+ 			   #'tramp-compile-enable-goto-error))))
+
  ;;; Default connection-local variables for Tramp:
  ;; `connection-local-set-profile-variables' and
  ;; `connection-local-set-profiles' exists since Emacs 26.1.