Browse Source

In readFromChild() for Windows: Call PeekNamedPipe() to check for available data.

Reason: ReadFile() doesn't always return if there's no data and the child exits after ReadFile()
is called.
adeniz_1
Alban Deniz 11 months ago
parent
commit
02aafe6f93
1 changed files with 55 additions and 10 deletions
  1. 55
    10
      child.cpp

+ 55
- 10
child.cpp View File

// Blocking read from the child. // Blocking read from the child.
#ifdef _WIN32 #ifdef _WIN32
DWORD nBytesRead; DWORD nBytesRead;
if (!ReadFile(inputHandle,
bufferPtr,
bufferCapacity,
&nBytesRead,
NULL)) {
try {
if (stopReaderFlag) {
break;
}
DWORD bytesAvailable;
if (!PeekNamedPipe(inputHandle,
NULL,
0,
NULL,
&bytesAvailable,
NULL)) {
throw std::runtime_error("Error from PeekNamedPipe: " +
getErrorText());
}
// Is data available?
if (0 == bytesAvailable) {
// No. Wait and try again.
pollTimer.pause();
continue;
}
if (!ReadFile(inputHandle,
bufferPtr,
bufferCapacity,
&nBytesRead,
NULL)) {
if (stopReaderFlag) {
break;
}
// Broken pipe occurs when the child exits; this is not
// necessarily an error condition.
if (GetLastError() != ERROR_BROKEN_PIPE) {
errorText = "Error reading from the child process: ";
errorText += getErrorText();
}
break;
}
} catch(...) {
// Some error occurred.
if (stopReaderFlag) {
break;
}
// Broken pipe occurs when the child exits; this is not
// necessarily an error condition.
// Thread was not commanded to stop; output error: Broken pipe
// occurs when the child exits; this is not an error
// condition.
if (GetLastError() != ERROR_BROKEN_PIPE) { if (GetLastError() != ERROR_BROKEN_PIPE) {
errorText = "Error reading from the child process: "; errorText = "Error reading from the child process: ";
} }
// Exit the thread.
break; break;
} }

Loading…
Cancel
Save