第一次读取后 Win32 匿名管道损坏
Win32 anonymous pipe broken after first read
我正在创建一个进程并通过 win32 api 读取它的标准输出和错误。
问题是,在第一次使用 ReadFile()
成功读取后,对于管道中剩余的数据,我总是会收到下一个管道损坏错误 (109)。 (所以我在第一次调用时只读了和缓冲区一样大的内容)
我怀疑这可能是因为我正在同步读取管道但是在 child 终止之后?
void read_pipes(std::string &output, HANDLE read_pipe) {
assert(read_pipe != nullptr && read_pipe != INVALID_HANDLE_VALUE);
char buffer[4096];
DWORD bytes_left_to_read, err_code; int i = 0;
do {
memset(&buffer, 0, 4096);
err_code = 0;
if (!ReadFile(read_pipe, buffer, 4096 - 1, &bytes_left_to_read, NULL)) {
err_code = GetLastError();
}
assert(err_code != ERROR_IO_PENDING);
if (err_code != 0 && err_code != ERROR_MORE_DATA) {
char sys_err_msg[128];
sys_err_msg[128 - 1] = '[=10=]';
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, err_code, MAKELANGID(0x09, 0x01), sys_err_msg, 127, nullptr);
std::cerr << "Failed to read failed mmdb_importer log, because " << sys_err_msg << ";" << err_code
<< std::endl;
break;
}
output.append(buffer);
} while (true);
以及调用读取的部分代码(完整在http://pastebin.com/vakLULyf下)
if (WaitForSingleObject(mddb_importer_info.hProcess, 60000) == WAIT_TIMEOUT) {
//Kill the importer, probably hangs
TerminateProcess(mddb_importer_info.hProcess, EXIT_FAILURE);
}
GetExitCodeProcess(mddb_importer_info.hProcess, &mddb_importer_return);
//assert (mddb_importer_return != STILL_ACTIVE);
switch (mddb_importer_return) {
case EXIT_SUCCESS:
file_to_upload.uploaded = true;
break;
default:
assert(file_to_upload.uploaded == false);
{
read_pipes(std::ref(file_to_upload.log), mddb_importer_stderr_r);
std::clog << "MDDB_Importer failed with err: : " << file_to_upload.log << std::endl;
read_pipes(std::ref(file_to_upload.log), mddb_importer_stdout_r);
std::clog << "And total size of log " << file_to_upload.log.length() << std::endl;
}
break;
}
是的,看起来我弄错了顺序,但由于我不想永远等待一个可能挂起的进程,我现在正在通过线程读取管道,我之前启动了我的 read_pipes 函数超时等待,std::async。
片段为例:
if (!CloseHandle(mddb_importer_stderr_w)) throw std::runtime_error("Can not create mddb_importer pipes");
auto err = std::async(std::launch::async, read_pipes, mddb_importer_stderr_r);
auto out = std::async(std::launch::async, read_pipes, mddb_importer_stdout_r);
if (WaitForSingleObject(mddb_importer_info.hProcess, 60000) == WAIT_TIMEOUT) {
//Kill the importer, probably hangs
TerminateProcess(mddb_importer_info.hProcess, EXIT_FAILURE);
}
GetExitCodeProcess(mddb_importer_info.hProcess, &mddb_importer_return);
//assert (mddb_importer_return != STILL_ACTIVE);
const std::string err_string = err.get();
我正在创建一个进程并通过 win32 api 读取它的标准输出和错误。
问题是,在第一次使用 ReadFile()
成功读取后,对于管道中剩余的数据,我总是会收到下一个管道损坏错误 (109)。 (所以我在第一次调用时只读了和缓冲区一样大的内容)
我怀疑这可能是因为我正在同步读取管道但是在 child 终止之后?
void read_pipes(std::string &output, HANDLE read_pipe) {
assert(read_pipe != nullptr && read_pipe != INVALID_HANDLE_VALUE);
char buffer[4096];
DWORD bytes_left_to_read, err_code; int i = 0;
do {
memset(&buffer, 0, 4096);
err_code = 0;
if (!ReadFile(read_pipe, buffer, 4096 - 1, &bytes_left_to_read, NULL)) {
err_code = GetLastError();
}
assert(err_code != ERROR_IO_PENDING);
if (err_code != 0 && err_code != ERROR_MORE_DATA) {
char sys_err_msg[128];
sys_err_msg[128 - 1] = '[=10=]';
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, err_code, MAKELANGID(0x09, 0x01), sys_err_msg, 127, nullptr);
std::cerr << "Failed to read failed mmdb_importer log, because " << sys_err_msg << ";" << err_code
<< std::endl;
break;
}
output.append(buffer);
} while (true);
以及调用读取的部分代码(完整在http://pastebin.com/vakLULyf下)
if (WaitForSingleObject(mddb_importer_info.hProcess, 60000) == WAIT_TIMEOUT) {
//Kill the importer, probably hangs
TerminateProcess(mddb_importer_info.hProcess, EXIT_FAILURE);
}
GetExitCodeProcess(mddb_importer_info.hProcess, &mddb_importer_return);
//assert (mddb_importer_return != STILL_ACTIVE);
switch (mddb_importer_return) {
case EXIT_SUCCESS:
file_to_upload.uploaded = true;
break;
default:
assert(file_to_upload.uploaded == false);
{
read_pipes(std::ref(file_to_upload.log), mddb_importer_stderr_r);
std::clog << "MDDB_Importer failed with err: : " << file_to_upload.log << std::endl;
read_pipes(std::ref(file_to_upload.log), mddb_importer_stdout_r);
std::clog << "And total size of log " << file_to_upload.log.length() << std::endl;
}
break;
}
是的,看起来我弄错了顺序,但由于我不想永远等待一个可能挂起的进程,我现在正在通过线程读取管道,我之前启动了我的 read_pipes 函数超时等待,std::async。 片段为例:
if (!CloseHandle(mddb_importer_stderr_w)) throw std::runtime_error("Can not create mddb_importer pipes");
auto err = std::async(std::launch::async, read_pipes, mddb_importer_stderr_r);
auto out = std::async(std::launch::async, read_pipes, mddb_importer_stdout_r);
if (WaitForSingleObject(mddb_importer_info.hProcess, 60000) == WAIT_TIMEOUT) {
//Kill the importer, probably hangs
TerminateProcess(mddb_importer_info.hProcess, EXIT_FAILURE);
}
GetExitCodeProcess(mddb_importer_info.hProcess, &mddb_importer_return);
//assert (mddb_importer_return != STILL_ACTIVE);
const std::string err_string = err.get();