Skip to content

Commit

Permalink
Fix AbortIO
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffery committed Jan 7, 2025
1 parent 7777f37 commit 3324e50
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
32 changes: 24 additions & 8 deletions port/win/env_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1227,13 +1227,13 @@ IOStatus WinFileSystem::Poll(std::vector<void*>& io_handles,
}
}

win_handle->is_finished = true;
win_handle->cb(req, win_handle->cb_arg);

if (win_handle->overlapped.hEvent != NULL) {
CloseHandle(win_handle->overlapped.hEvent);
win_handle->overlapped.hEvent = NULL;
}

win_handle->is_finished = true;
win_handle->cb(req, win_handle->cb_arg);
}
return IOStatus::OK();
}
Expand All @@ -1245,6 +1245,12 @@ IOStatus WinFileSystem::AbortIO(std::vector<void*>& io_handles) {
continue;
}

if (win_handle->overlapped.hEvent == NULL) {
return IOStatus::InvalidArgument(
"AbortIO: Overlapped event is NULL for: " +
win_handle->file_data->GetName());
}

if (FALSE == CancelIoEx(win_handle->file_data->GetFileHandle(),
&win_handle->overlapped)) {
auto err = GetLastError();
Expand All @@ -1254,17 +1260,27 @@ IOStatus WinFileSystem::AbortIO(std::vector<void*>& io_handles) {
return IOErrorFromWindowsError(
"CancelIoEx failed: " + win_handle->file_data->GetName(), err);
}
}

if (win_handle->overlapped.hEvent != NULL) {
CloseHandle(win_handle->overlapped.hEvent);
win_handle->overlapped.hEvent = NULL;
} else {
// CancelIoEx succeeded
// Wait for the operation to complete
DWORD bytes_read = 0;
if (!GetOverlappedResult(win_handle->file_data->GetFileHandle(),
&win_handle->overlapped, &bytes_read, TRUE)) {
if (GetLastError() != ERROR_OPERATION_ABORTED) {
return IOErrorFromWindowsError(
"AbortIO failed: " + win_handle->file_data->GetName(),
GetLastError());
}
}
}

win_handle->is_finished = true;
FSReadRequest req;
req.status = IOStatus::Aborted();
win_handle->cb(req, win_handle->cb_arg);

CloseHandle(win_handle->overlapped.hEvent);
win_handle->overlapped.hEvent = NULL;
}
return IOStatus::OK();
}
Expand Down
25 changes: 9 additions & 16 deletions port/win/io_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1015,54 +1015,47 @@ IOStatus WinRandomAccessFileAsyncIo::ReadAsync(
file_base_->GetName());
}

HANDLE h_event = RX_CreateEvent(NULL, TRUE, FALSE, NULL);
if (h_event == NULL) {
return IOErrorFromWindowsError("CreateEvent failed", GetLastError());
}

IOHandleDeleter deletefn = [](void* args) -> void {
delete (static_cast<Win_IOHandle*>(args));
args = nullptr;
};

Win_IOHandle* win_handle = new Win_IOHandle(cb, cb_arg, req.offset, req.len,
req.scratch, file_base_);

win_handle->overlapped.hEvent = RX_CreateEvent(NULL, TRUE, FALSE, NULL);
if (win_handle->overlapped.hEvent == NULL) {
return IOErrorFromWindowsError("CreateEvent failed", GetLastError());
}
win_handle->overlapped.hEvent = h_event;

*io_handle = static_cast<void*>(win_handle);
*del_fn = deletefn;

IOStatus ios = IOStatus::OK();
DWORD bytes_read = 0;
bool close_handle = true;
if (FALSE == ReadFile(file_base_->GetFileHandle(), req.scratch,
static_cast<DWORD>(req.len), &bytes_read,
&win_handle->overlapped)) {
DWORD last_error = GetLastError();
// Too many outstanding asynchronous I/O requests
if (last_error == ERROR_NOT_ENOUGH_MEMORY) {
CancelIoEx(file_base_->GetFileHandle(), &win_handle->overlapped);
ios = IOErrorFromWindowsError("ReadFile failed: " + file_base_->GetName(),
last_error);
} else if (last_error == ERROR_HANDLE_EOF) {
if (last_error == ERROR_HANDLE_EOF) {
win_handle->is_finished = true;
req.result = Slice(req.scratch, 0);
req.status = IOStatus::OK();
cb(req, cb_arg);
} else if (last_error != ERROR_IO_PENDING) {
ios = IOErrorFromWindowsError("ReadFile failed: " + file_base_->GetName(),
last_error);
} else {
close_handle = false;
}

// else last_error is ERROR_IO_PENDING
} else {
win_handle->is_finished = true;
req.result = Slice(req.scratch, bytes_read);
req.status = IOStatus::OK();
cb(req, cb_arg);
}

if (close_handle && win_handle->overlapped.hEvent != NULL) {
if (win_handle->is_finished && win_handle->overlapped.hEvent != NULL) {
CloseHandle(win_handle->overlapped.hEvent);
win_handle->overlapped.hEvent = NULL;
}
Expand Down

0 comments on commit 3324e50

Please sign in to comment.