2

I'm trying to call a process with a string to its stdin, with Boost-1.64.0. The current code is :

  bp::opstream inStream ;
  bp::ipstream outStream;
  bp::ipstream errStream;

  bp::child child(
    command, // the command line
    bp::shell,
    bp::std_out > outStream,
    bp::std_err > errStream,
    bp::std_in  < inStream);


  // read the outStream/errStream in threads

  child.wait();

The problem is that the child executable is waiting for its stdin EOF. Here child.wait() is hanging indefinitely…

I tried to used asio::buffer, std_in.close(),… But no luck. The only hack I found was to delete() the inStream… And that's not really reliable.

How am I supposed to "notify" the child process and close its stdin with the new boost::process library ?

Thanks !

Salamandar
  • 589
  • 6
  • 16

2 Answers2

3

I tried to used asio::buffer, std_in.close()

This works. Of course it only works if you pass it to a launch function (bp::child constructor, bp::system, etc).

If you need to pass data, and then close it, simply close the associated filedescriptor. I do something like this:

boost::asio::async_write(input, bp::buffer(_stdin_data), [&input](auto ec, auto bytes_written){
    if (ec) {
        logger.log(LOG_WARNING) << "Standard input rejected: " << ec.message() << " after " << bytes_written << " bytes written";
    }
    may_fail([&] { input.close(); });
});

Where input is

bp::async_pipe input(ios);

Also, check that the process is not actually stuck sending the output! If you fail to consume the output it would be buffering and waiting if the buffer is full.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • You saved my week. I suppose may_fail is your custom handler for exceptions, so I call input.close() directly. And you have to call ios.run() too :) – Salamandar May 29 '17 at 11:02
  • Yup! Sorry for copying incomplete code this time :( http://paste.ubuntu.com/24701992/ – sehe May 29 '17 at 11:19
2

Closing the pipe by calling inStream.close(); when you're done writing to it. You can also close it while launching with bp::std_in.close().

The asio solution of course also works and avoids the danger of deadlocks.

  • Can you add to this answer? The OP used a boost::process::opstream object, which does not have a close() function. You link goes to documentation for a basic_pipe object, which does not seem to be a one-to-one replacement for boost::process::opstream. – DCTLib Sep 24 '18 at 11:21
  • You can access the underlying pipe by calling `opstream::pipe()`. – Klemens Morgenstern Oct 29 '18 at 11:29