Skip to content

Commit c1b763c

Browse files
committed
Async: Handle ERROR_NOT_FOUND in IOCP cancellations using CancelIOEx
1 parent 207570c commit c1b763c

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Libraries/Async/Internal/AsyncWindows.inl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,12 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
429429
{
430430
BOOL res = ::CancelIoEx(reinterpret_cast<HANDLE>(asyncAccept.handle),
431431
&asyncAccept.acceptData->overlapped.get().overlapped);
432-
if (res == FALSE)
432+
// CancelIOEx will return ERROR_NOT_FOUND if no operation to cancel has been found
433+
if (res == FALSE and GetLastError() != ERROR_NOT_FOUND)
433434
{
434435
return Result::Error("AsyncSocketAccept: CancelEx failed");
435436
}
437+
// CancelIoEx queues a cancellation packet on the async queue
436438
eventLoop.internal.hasPendingKernelCancellations = true;
437439
return Result(true);
438440
}
@@ -618,10 +620,12 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
618620
static Result cancelAsync(AsyncEventLoop& eventLoop, AsyncSocketSendTo& async)
619621
{
620622
BOOL res = ::CancelIoEx(reinterpret_cast<HANDLE>(async.handle), &async.overlapped.get().overlapped);
621-
if (res == FALSE)
623+
// CancelIOEx will return ERROR_NOT_FOUND if no operation to cancel has been found
624+
if (res == FALSE and GetLastError() != ERROR_NOT_FOUND)
622625
{
623626
return Result::Error("AsyncSocketSendTo: CancelEx failed");
624627
}
628+
// CancelIoEx queues a cancellation packet on the async queue
625629
eventLoop.internal.hasPendingKernelCancellations = true;
626630
return Result(true);
627631
}
@@ -650,10 +654,12 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
650654
static Result cancelAsync(AsyncEventLoop& eventLoop, AsyncSocketReceiveFrom& async)
651655
{
652656
BOOL res = ::CancelIoEx(reinterpret_cast<HANDLE>(async.handle), &async.overlapped.get().overlapped);
653-
if (res == FALSE)
657+
// CancelIOEx will return ERROR_NOT_FOUND if no operation to cancel has been found
658+
if (res == FALSE and GetLastError() != ERROR_NOT_FOUND)
654659
{
655660
return Result::Error("AsyncSocketReceiveFrom: CancelEx failed");
656661
}
662+
// CancelIoEx queues a cancellation packet on the async queue
657663
eventLoop.internal.hasPendingKernelCancellations = true;
658664
return Result(true);
659665
}
@@ -678,7 +684,8 @@ struct SC::AsyncEventLoop::Internal::KernelEvents
678684
static Result cancelAsync(AsyncEventLoop& eventLoop, AsyncSocketReceive& async)
679685
{
680686
BOOL res = ::CancelIoEx(reinterpret_cast<HANDLE>(async.handle), &async.overlapped.get().overlapped);
681-
if (res == FALSE)
687+
// CancelIOEx will return ERROR_NOT_FOUND if no operation to cancel has been found
688+
if (res == FALSE and GetLastError() != ERROR_NOT_FOUND)
682689
{
683690
return Result::Error("AsyncSocketReceive: CancelEx failed");
684691
}

0 commit comments

Comments
 (0)