All new sync process functions now terminate on an exception (#2233)

Now all the functions that call a process synchronously have the same
behaviour. Previously just readProcess, readProcessWithExitCode did
this, now callProcess and callCommand do too.

If a thread running one of these functions gets an exception, including
async exceptions (such as from timeout or killThread), then the
external process gets terminated.

Introduce a helper function to implement this behaviour. Currently it
is not exposed to users, but that could be changed easily.

Authored-by: Duncan Coutts <duncan@well-typed.com>
Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>
Improve the code for ignoring EPIPE (#2233)

Factor it out into an ignoreSigPipe util, and use it in both
readProcess and readProcessWithExitCode.

Authored-by: Duncan Coutts <duncan@well-typed.com>
Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>
Be even more careful with the threads that consume output (#2233)

There is a potential deadlock with withCreateProcess in the case that
there's an exception: cleanupProcess will try to hClose the various
handles, but if another thread holds the Handle lock then that hClose
will block.

Takano Akio fixed the main case of this (in patch
32223a9ab174c22e939c81e24b6f48223c7cb020) by terminating the process
(before closing the handles) This works because terminating the process
will eventually cause those other threads to finish and release the
Handle lock, so we can hClose.

However on Unix terminateProcess is not guaranteed to terminate the
process since it uses SIGTERM, which can be handled or ignored. So we
have to separately guarantee that the handles can be hClosed, and the
simplest way to do this is to ensure that the thread reading from the
handles get killed in the case there's an exception.

So we change forkWait to withForkWait that will kill off the thread if
the body gets an exception.

Authored-by: Duncan Coutts <duncan@well-typed.com>
Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>
Drop redundant hFlush & add ignoreSigPipe to hClose

This is a follow-up to 79ae975aab5866de38e1a01aca7ac5bd9cde2285
(addressing #2233)

Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>