unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Chris Vine <vine35792468@gmail.com>
To: guile-devel@gnu.org
Subject: Re: Subprocess API.
Date: Mon, 25 Sep 2017 19:59:39 +0100	[thread overview]
Message-ID: <20170925195939.01638900@dell.homenet> (raw)
In-Reply-To: <874lrqlmz5.fsf@gmail.com>

On Mon, 25 Sep 2017 19:14:22 +0200
Mathieu Othacehe <m.othacehe@gmail.com> wrote:
> Hi Chris,
> 
> > This works exactly as you would expect from its POSIX equivalents
> > and has the advantage that you can read from the pipe as the
> > sub-process is proceeding rather than just collect at the end.  
> 
> Thank you ! Following your suggestion, I ended-up with :
> 
> --8<---------------cut here---------------start------------->8---
> (let* ((err-pipe (pipe))
>        (out-pipe (pipe))
>        (read-out (car out-pipe))
>        (write-out (cdr out-pipe))
>        (read-err (car err-pipe))
>        (write-err (cdr err-pipe))
>        (pid (run-concurrently+
>              (apply tail-call-program "...")
>              (write-out 1)
>              (write-err 2)))
>        (ret (status:exit-val (cdr (waitpid pid)))))
>   (close-port write-out)
>   (close-port write-err)
>   (let ((output (read-string read-out))
>         (error (read-string read-err)))
>     (close-port read-out)
>     (close-port read-errs 
>     (case ret
>       ((0) output)
>       (else (raise ...)))))
> --8<---------------cut here---------------end--------------->8---
> 
> which seems to work. However, run-concurrently+ uses "primitive-fork"
> which is forbiden in a multi-thread context (sadly, mine).
> 
> Do you have any idea on how to overcome this ?

Any launching of a new process requires a fork and if (as appears to
be your intention) you want to replace the process image with a new
one, an exec.  As you appear to know, POSIX allows only
async-signal-safe functions to be called in a multi-threaded program
between the fork and the exec, although glibc does relax this somewhat.

Since anything you do in guile between the fork and the exec has the
potential to allocate memory, that appears to mean that, as you say, you
cannot call primitive-fork in a guile program at a time when it is
running more than one thread.  If so, I do not know how to circumvent
that: you could consider launching the new process in C code via the
guile FFI so you can ensure that no non-async-signal-safe code is
called at the wrong time; but presumably you would still have by some
means to prevent the garbage collector from being able to start a
memory reclaiming run in the new process after the fork and before the
exec, and again I do not know how you would do that.  You would also
need to block system asyncs before forking (and unblock after the fork
in the original process) but that is trivial to do.

As regards your code, if you do not need to distinguish between stdout
and stderr, you would do better to have only one pipe and use the write
port of the pipe for both of descriptors 1 and 2.  That means that you
could read the pipe while the new process is proceeding rather than
after it has finished (which risks filling up the pipe): just loop
reading the read end of the pipe until an eof-object is received, and
then call waitpid after that.

Chris



  reply	other threads:[~2017-09-25 18:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-23  9:58 Subprocess API Mathieu Othacehe
2017-09-23 14:19 ` Chris Vine
2017-09-25 17:14   ` Mathieu Othacehe
2017-09-25 18:59     ` Chris Vine [this message]
2017-09-25 20:34       ` Chris Vine

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170925195939.01638900@dell.homenet \
    --to=vine35792468@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).