1

Following code hangs forever when GetOverlappedResult gets called, I have not much experience in windows async IO operations, I implemented it as per my understanding. I have used it to access virtual network interface (by openvpn - TAP/TUN interface whose kernel driver is installed properly).

I found the place where it hangs, but I don't know the reason why it hangs ?

nread = 0;
memset(data_buffer, '\0', nread);
OVERLAPPED overlapped_read;
memset(&overlapped_read, 0, sizeof(overlapped_read));
overlapped_read.Offset = 0;
overlapped_read.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

if ( ReadFile(fd, data_buffer, len, &nread, &overlapped_read) == false ) {
    if (GetLastError() != ERROR_IO_PENDING) {
        std::cerr << "ReadFile failed : " << GetLastError() << std::endl;
        return false;
    }
    else {
        DWORD dwRes = WaitForSingleObject(overlapped_read.hEvent, INFINITE);
        if(dwRes == WAIT_OBJECT_0) {
            if (!GetOverlappedResult(fd, &overlapped_read, &nread, FALSE)) {
                std::cout << "GetOverlappedResult failed : ErrorCode = "
                          << GetLastError() << std::endl;
                CloseHandle(overlapped_read.hEvent);    
                return false;
            }
            else {
                std:cout << "Read successfull." << std::endl;
            }
        }    
        else {    
            std::cout << "WaitForSingleObject failed : ErrorCode = "
                      << GetLastError() << std::endl;
            CloseHandle(overlapped_read.hEvent);    
            return false;    
        }
    }
    std::cout << "bytes read = " << nread << std::endl;    
    CloseHandle(overlapped_read.hEvent);    
    return TRUE;
}

Above code executed in an Infinite while loop inside a thread created using CreateThread API.

Do I have implemented async operation correctly ?

What could be the possible cases when GetOverlappedResult gets hangs ?

PS: Few ReadFile calls were successfull, it can read ethernet frame successfully, but hangs after approx. 15-20 calls.

  • 1
    Not sure, but you are leaking events on every call. Either put a CloseHandle() in there or, better, create the event in the thread function before the loop that calls this code so that the event is only created once and re-used, (which is what I guess you intended because you ResetEvent it). – Martin James Oct 14 '14 at 18:18
  • Also, you use of the 'hEvent' mechanism means that, effectively, this code is a synchronous read. – Martin James Oct 14 '14 at 18:20
  • I have used FILE_FLAG_OVERLAPPED flag, when file is opened/created. As per the msdn doc it suggest that file is opened for asynchronous operation, ReadFile/WriteFile API's also doesn't gets block so it's indeed asynchronous mechanism. or have I interpreted it wrongly ? – Harry Cruise Oct 16 '14 at 11:33
  • It is indeed asycnhronous until you hit 'WaitForSingleObject(overlapped_read.hEvent, INFINITE);', at which point is becomes synchronous. – Martin James Oct 18 '14 at 10:50

0 Answers0