From 71d939c17417180c2d6c77032fece5bdef7181d1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 29 Jun 2021 06:46:00 -0700 Subject: doc-lookup: handle xdg-open not terminating. It is common for web browsers like firefox not to fork themselves into the background when initially run from the command line. Only when an additional instance is executed does that instance terminate immediately, passing the URL to the existing instance. (Which also does not constitute forking into the background, but does have the effect of an immediate exit.) User Paul A. Patience reports that some installations of xdg-open have the isssue of not handling this situation; these versions of xdg-open wait for the browser to terminate, which causes xdg-open to hang until the browser is closed if it is the initial instance. * stdlib/doc-lookup.tl (detached-run): New function. Like run, but forks into the background, running the process in a detached grandchild whose parent terminates, so that it becomes an orphan parented to the init daemon. We redirect *stdout* to *stdnull* because the first instance of the browser can spit ugly, meaningless diagnostics when it terminates. (open-url): Use detached-run instad of run. Don't check the return value for zero; there is no integer exit status. --- stdlib/doc-lookup.tl | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/stdlib/doc-lookup.tl b/stdlib/doc-lookup.tl index 46c60d6e..1d02049e 100644 --- a/stdlib/doc-lookup.tl +++ b/stdlib/doc-lookup.tl @@ -17,16 +17,22 @@ (ret :unknown)) u.sysname]))) +(defun detached-run (program args) + (match-case (fork) + (@(= 0) (if (zerop (fork)) + (exit* (let ((*stdout* *stdnull*)) + (run program args))) + (exit* 0))) + (@(< @res 0) (error "fork failed")))) + (caseql os-symbol ((:linux :macos :solaris :solaris10 :android) (defun open-url (url) - (if (zerop (run (caseql os-symbol - ((:linux :solaris :android) "xdg-open") - (:solaris10 "/usr/dt/bin/sdtwebclient") - (:macos "open")) - (list url))) - t - (error `~s: failed to open ~s` 'open-url url)))) + (detached-run (caseql os-symbol + ((:linux :solaris :android) "xdg-open") + (:solaris10 "/usr/dt/bin/sdtwebclient") + (:macos "open")) + (list url)))) ((:cygwin :cygnal) (with-dyn-lib "shell32.dll" (deffi shell-execute "ShellExecuteW" -- cgit v1.2.3