Skip to content

EOF to stdin when launching process asynchronously on macOS #531

@atraastrum

Description

@atraastrum

On macOS 15.6.1 M3 the program below exits due to EOF flag being raised on std::cin after subprocess finishes.

#include <boost/process.hpp>
#include <boost/asio.hpp>
#include <cstdio>
#include <string>
#include <iostream>
#include <future>
#include <vector>

namespace asio = boost::asio;
namespace bp = boost::process::v2;

void runCommand(const std::string &cmd);

int main(int argc, char *argv[]) {
  std::vector<std::future<void>> tasks;
  std::string line;
  while(true) {
    if (!std::getline(std::cin, line)) {
      if (std::cin.eof()) {
        break;
      }
      std::printf("Error reading input?\n");
      std::cin.clear();
      continue;
    }

    if (line == "r") {
      tasks.push_back(std::async(std::launch::async, []() {
        runCommand("/bin/ls");
      }));
    }
  }

  return 0;
}

void runCommand(const std::string &cmd) {
  try {
    std::printf("Running subprocess: %s\n", cmd.c_str());
    asio::io_context ctx;
    bp::process subProcess(ctx, cmd, {}, bp::process_stdio{nullptr});
    subProcess.wait();

    std::printf("Finished running subprocess with exit code %d\n", subProcess.exit_code());
  } catch (const std::exception &ex) {
    std::printf("Error running subprocess: %s\n", ex.what());
  }
}

Compiler

Apple clang version 17.0.0 (clang-1700.0.13.5)
Target: arm64-apple-darwin24.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Boos version (from vcpkg)

1.89.0

Example run:

boost-process git:(cpp-boost-process) ✗ ./build/main
r
Running subprocess: /bin/ls
CMakeLists.txt	README.md	build		main.cpp	vcpkg.json	vcpkg_installed
Finished running subprocess with exit code 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions