Process.V2 may block with wrong way use of "process_stdio"

Open
#471 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
cpp

Research direction

Reproduce the Windows example using boost/process.hpp, asio::readable_pipe, and process_stdio, comparing the separately declared pipe with the inline process_stdio construction. Inspect process_io_binding and stdio.hpp first, then verify pipe reads and handle state after the detached Git process exits. Done means the reported blocking behavior is explained and the intended usage or implementation is clarified with a regression test or documentation change.

Written by the indexing model from the issue text.

Description

#define WIN32_LEAN_AND_MEAN
#define BOOST_PROCESS_VERSION 2
#include <boost/process.hpp>
#include <iostream>

#if defined(BOOST_PROCESS_V2_WINDOWS)
#pragma comment(lib,"ntdll.lib")

//for MSVC 143
#if defined(_MT) //MultiThread Static
#   if defined(_DEBUG)
#       pragma comment(lib,"libboost_process-vc143-mt-sgd-x64-1_87.lib")
#   endif
#endif
#endif

using namespace boost;
namespace bp = boost::process::v2;
int main()
{
    asio::io_context ctx;
    std::string output;
    output.resize(1000);
    system::error_code ec;

    {
        //bad way
        asio::readable_pipe rp2{ ctx };

        //this io must declare inside proc like bp::process proc(ctx,xx,xxx,bp::process_stdio{{...},{...},{..}}}
        bp::process_stdio io = bp::process_stdio{ nullptr, rp2, nullptr };
        //due to process_io_binding@stdio.hpp hold this pip handle with a unique_ptr,
        //this make the life time as long as the var 'proc' bellow
        //and cannot clean the handle passed to the child,after CreateProcessW
        //thus read_some can't get ERROR_PIPE_BROKEN(because there still have a valid PIPE handle in 'io')

        //use SysinternalsSuite/procexp64 FIND->FIND Handle or DLL to search 'asio' 
        //after child process exit.it should not valid.
        bp::process proc(ctx, R"(C:\Program Files\Git\bin\git.exe)", { "-v" }, io);
        proc.detach();
        while (true)
        {
            //bad:never revice ERROR_PIPE_BROKEN
            auto size = rp2.read_some(asio::buffer(output), ec);
            if (ec)  break;
            std::cout<<output.substr(0, size);
        }
        std::cout<<"badend\n\n";
    }

    {
        //good way
        asio::readable_pipe rp{ ctx };
        bp::process proc(ctx, R"(C:\Program Files\Git\bin\git.exe)", { "-v" }, bp::process_stdio{ nullptr, rp, nullptr });
        proc.detach();
        while (true)
        {
            auto size = rp.read_some(asio::buffer(output), ec);
            if (ec)  break;
            std::cout<<output.substr(0, size);
        }
        std::cout<<"goodend\n\n";
    }

    return 0;
}
Dominant language
C++
Stars
145
Forks
151
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/process

All issues in boostorg/process

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.