From: Eshel Yaron Date: Thu, 9 Nov 2023 19:47:57 +0000 (+0100) Subject: Handle SIGHUP in daemon mode when killing a tty top-level X-Git-Tag: V9.1.18-0.26.2~1 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=68db64e554fa7e551d1c3e561b1ce9aa15a02aaf;p=sweep.git Handle SIGHUP in daemon mode when killing a tty top-level When Emacs is running as a daemon, and deletes the process of a top-level that uses a tty, the top-level receives SIGHUP. But since the top-level is a thread is the same process as Emacs itself, this results in 'kill-emacs'. To prevent this unfortunate outcome, set a no-op handler for SIGHUP before deleting the top-level process and restore the original handler afterwards. * sweep.pl (sweep_nohup/2): New predicate. * sweeprolog.el (sweeprolog-top-level-filter): New function. (sweeprolog-top-level-buffer): Use it. --- diff --git a/sweep.pl b/sweep.pl index 954b3b5..51ebf4d 100644 --- a/sweep.pl +++ b/sweep.pl @@ -106,7 +106,8 @@ sweep_options_collection/2, sweep_option_arguments_collection/2, sweep_functions_collection/2, - sweep_function_functors_collection/2 + sweep_function_functors_collection/2, + sweep_nohup/2 ]). :- use_module(library(pldoc)). @@ -2179,3 +2180,6 @@ sweep_extract_lambda_1(Clause, ClauseVarNames, Func, Shared, Args, GoalPos, Offs -> Exists = "true" ; Exists = [] ). + +sweep_nohup(1, _) :- on_signal(hup, _, atom). +sweep_nohup(0, _) :- on_signal(hup, _, default). diff --git a/sweeprolog.el b/sweeprolog.el index 3387a0f..02246c6 100644 --- a/sweeprolog.el +++ b/sweeprolog.el @@ -3415,6 +3415,19 @@ function with PROC and MSG." #'sweeprolog-top-level-sentinel) (add-hook 'kill-buffer-hook #'comint-write-input-ring nil t))) +(defun sweeprolog-top-level-filter (process string) + (let ((sweeprolog-top-level-output-filter t)) + (comint-output-filter process string)) + (when (string-match (rx "Sweep top-level thread exited") string) + (with-current-buffer (process-buffer process) + (setq sweeprolog-top-level-thread-id nil)) + (if (or (eq (process-type process) 'network) + (not (daemonp))) + (delete-process process) + (sweeprolog--query-once "sweep" "sweep_nohup" 1) + (delete-process process) + (sweeprolog--query-once "sweep" "sweep_nohup" 0)))) + (defun sweeprolog-top-level-buffer (&optional name) "Return a Prolog top-level buffer named NAME. @@ -3447,14 +3460,7 @@ top-level." sweeprolog-prolog-server-port)) (sweeprolog--query-once "sweep" "sweep_accept_top_level_client" nil))) (let ((proc (get-buffer-process buf))) - (set-process-filter proc - (lambda (process string) - (let ((sweeprolog-top-level-output-filter t)) - (comint-output-filter process string)) - (when (string-match (rx "Sweep top-level thread exited") string) - (delete-process process) - (with-current-buffer buf - (setq sweeprolog-top-level-thread-id nil))))) + (set-process-filter proc #'sweeprolog-top-level-filter) (unless comint-last-prompt buf (accept-process-output proc 1)) (set-process-query-on-exit-flag proc nil) (setq-local comint-input-ring-file-name