On 31/01/2021 23:33, Eli Zaretskii wrote:
From: Maxim Nikulin <m.a.niku...@gmail.com>
Date: Sun, 31 Jan 2021 22:57:57 +0700
Cc: 44...@debbugs.gnu.org

To fix the problem it is better to use (make-process :connection-type
'pipe ...) that unfortunately has no higher level wrappers.

Wouldn't it work to let-bind process-connection-type to nil around the
function that starts the async subprocess?

Sorry, for me it easier to reason how to express it in terms of system
calls and terminal process groups than if let-bind could override a
variable when lexical-bind is set to true.

Well, I think we should try this, because if it works, it will show us
a way to fix the problem.  (I don't see how lexical-binding could
interfere with let-binding.)

I have prepared a patch that uses `make-process'. I hope, error reporting is improved a bit, but in the case of this particular problem failure is still quiet since xdg-open exits successfully.

I could not estimate effect of such change on windows, so pipe process
is used only on linux. I am unsure concerning mac however.

Another question is if failure message should be suppressed when `waiting-for-user-input-p' returns truth to keep user prompt and input unaffected.
commit 0001bff24864ee16598b4701a05cf40e9abc83af
Author: Max Nikulin <maniku...@gmail.com>
Date:   Wed Feb 17 16:35:58 2021 +0000

    org.el: Avoid xdg-open silent failure
    
    * lisp/org.el (org-open-file): Use 'pipe :connection-type instead of
    'pty to prevent killing of background process on handler exit.
    
    Problem happens only in some desktop environments where configured
    through `org-file-apps' or mailcap handlers launches actual viewer
    (as defined in .desktop files and obtained from mimeapps.list)
    in background.  E.g. xdg-open invokes "gio open" or kde-open5 for Gnome
    or KDE accordingly and these handlers launches e.g. eog or okular in
    background.  As soon as main process exits, temporary terminal session
    created by `start-process-shell-command' is terminated.  As a result
    background processes receive SIGHUP.
    
    Previously command were executed with no buffer, so the change
    does not affect "needsterminal" and "copiousoutput" mailcap features,
    they are not supported as earlier.
    
    If handler main process fails then show a message with exit reason.
    Output (including error messages) is ignored as before.
    Gtk application tends to report significant amount of failed asserts
    hardly informative for majority of users.

diff --git a/lisp/org.el b/lisp/org.el
index 7d8733448..a199a65c9 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -8645,6 +8645,15 @@ opened in Emacs."
    (when add-auto-mode
      (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
 
+(defun org--error-process-sentinel (proc event)
+  "Show a message if process failed (exited with non-zero code
+or killed by a signal.  Pass the function as :SENTINEL argument
+of `make-process'."
+  (unless (string-match "finished" event)
+    (message "Command %s: %s."
+             (mapconcat 'identity (process-command proc) " ")
+             (substring event 0 -1))))
+
 ;;;###autoload
 (defun org-open-file (path &optional in-emacs line search)
   "Open the file at PATH.
@@ -8766,7 +8775,17 @@ If the file does not exist, throw an error."
 
       (save-window-excursion
 	(message "Running %s...done" cmd)
-	(start-process-shell-command cmd nil cmd)
+	(if (eq system-type 'gnu/linux)
+	  ;; Handlers as "gio open" and kde-open5 start viewer in background
+	  ;; and exit immediately. Avoid start-process since it assumes
+	  ;; :connection-type 'pty and kills children processes with SIGHUP
+	  ;; when temporary terminal session is finished.
+	  (make-process
+	    :name "org-open-file" :connection-type 'pipe :noquery 't
+	    :buffer nil ; use "*Messages*" for debugging
+	    :sentinel 'org--error-process-sentinel
+	    :command (list shell-file-name shell-command-switch cmd))
+	  (start-process-shell-command cmd nil cmd))
 	(and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
      ((or (stringp cmd)
 	  (eq cmd 'emacs))

Reply via email to