Subject: Undocumented interaction with descendants of process created by Emacs (very annoying)

From: Teika Kazura <[email-masked]>
Date: Sun, 01 Dec 2013 17:30:03 +0900 (JST)

Hi, Emacs developers. Emacs interacts with the forks of a process created by Emacs, but in ways **not documented**, leading to unexpected, very annoying results. Both synchronous and async cases apply, but in different ways.

* Environment:
Gentoo Linux, Emacs 24.3, (at least since 24.1).

* Description:
(I only know, and can test Linux.)
First, synchorous cases. Assume you create a process P with `call-process'. Then Emacs basically waits for the termination of all descendants, not only P. More precisely, it's until all descendants' stdout and stderr, attached to Emacs, get closed. (So probably Emacs waits for the closing of the pipes it opened for P.)

Here, P's output BUFFER doesn't matter. Let's review the grammar:
  (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)
I mean, BUFFER (and INFILE) doesn't matter. Emacs waits even if they are nil.

Test codes. First, "sync.sh":
------------------------------------------------------------------------
#!/bin/bash

(
    # This is inside of a fork
    sleep 2 ; exec 1>/dev/null
    sleep 1 ; exec 2>/dev/null
    sleep 4
    exit
) &

sleep 1
------------------------------------------------------------------------
Run it with the following:

(let ((now (cadr (current-time))))
  (call-process "/path/to/sync.sh" nil nil nil)
  (message "elapesd %s secs" (- (cadr (current-time)) now))
)

P itself dies in 1 sec, but the "let" ends after 4 secs.

Now, the asynchronous case. When an asynchronous process P, created by `start-process', terminates, all its descendants (perhaps all processes in the process group?) receive SIGHUP from Emacs. It is the default behavior, but if you set `process-connection-type' to nil, then P's subprocesses are not affected.

For asynchronous cases, detaching stdout and stderr has no effect.

Suggestions:
* At least these should be documented, but you may like to modify the implementation. I don't know much about process and inter-process communication, and I can't say anything about it.
* If my description applies to all platforms, then I can contribute a draft of info file and C-src docstrings. (My draft probably will need a review by experts, as I indicated above.) Or, I can help you by formatting your technically correct plain text doc to info.
* (Not related the bug report itself) The names `call-process' and `start-process' are confusing, although you can remember them. I propose to change them to `start-sync-process' and `start-async-process'. (Ineviably their grammars differ, namely in INFILE and NAME. But it's also confusing that the argument positions of the path to the program differ. It's better to fix them too.)

Thank you for reading, and developing Emacs.
Best regards,
Teika (Teika kazura)