Subject: bug#63078: 26.1; Sentinels are sometimes unexpectedly only invoked in script mode

From: Eli Zaretskii <[email-masked]>
Date: Wed, 26 Apr 2023 09:22:02 +0300

> From: Markus Triska <[email-masked]>
> Date: Wed, 26 Apr 2023 01:15:36 +0200
> 
> to reproduce this issue, please save the following C code in proc.c:
> 
>     #include <stdio.h>
>     #include <stdlib.h>
>     int main() {
>       printf("hello");
>       exit(0);
>     }
> 
> and compile it, yielding the program file "./proc":
> 
>     $ gcc proc.c -o proc 
> 
> Further, please store the following Elisp definitions in proc.el, in
> the same directory as the above program:
> 
>     (add-to-list 'exec-path ".")
> 
>     (defvar p-stopped nil)
> 
>     (defun my-sentinel (proc str)
>       (when (string-match "^\\(?:finished\n\\|exited abnormally\\|killed\n\\)"
>                           str)
>         (setq p-stopped t)))
> 
>     (defun wait-for-process-end (proc)
>       (while (not p-stopped)
>         (message "process status is: %s" (process-status proc))
>         (accept-process-output proc 0.1 nil t)))
> 
>     (defun f ()
>       (with-temp-buffer
>         (let ((proc (start-process "p" (current-buffer) "proc")))
>           (setq p-stopped nil)
>           (set-process-sentinel proc 'my-sentinel)
>           (wait-for-process-end proc))
>         (message "process said: %s" (buffer-string))))
> 
> The following invocation and result shows a case where the program
> behaves exactly as intended: It waits for the process sentinel to
> signal that no more output can arrive from the process, and shows the
> output it got while the process was producing output:
> 
>     $ emacs -Q --script proc.el --eval "(f)"
>     process status is: exit
>     process said: hello
> 
> In contrast, the following invocation produces, in the echo area, the
> message "process status is: exit", and Emacs hangs unexpectedly:
> 
>     $ emacs -Q -l proc.el --eval "(f)"
> 
> The behaviour I expect in this case is analogous to the other
> invocation: I do expect the message we see, followed by the message
> "process said: hello", as in the case above. However, this second
> message only appears if I manually interrupt Emacs with C-g.

I cannot reproduce the problem with Emacs 28.2: I get "process said:
hello" in the echo-area, as expected.  Did you try with the -nw
command-line switch, and if you do, does it change anything?

It could be some timing problem: you start a program that exits almost
immediately, so there could be some race condition between the program
and your code.