Question about Using SetFileCompletionNotificationModes with IOCP in Non-blocking Winsock Operations

Harshal Goyal 20 Reputation points
2024-12-16T10:09:09.4833333+00:00

Hello everyone,

I'm currently working on a project where I am using WinSock to create both UDP and TCP sockets. The sockets are set in non-blocking mode using ioctl, and I am using overlapped operations with an I/O Completion Port (IOCP) to handle asynchronous network communication.

The Scenario:

I use WSASendTo for UDP and WSASend for TCP to send data. These operations are performed asynchronously, and I expect to receive callbacks on the IOCP once the operations are completed. However, I have encountered a situation where I still get a call back on the IOCP even if the operation completes synchronously which is as per documented behavior.

I came across the API function SetFileCompletionNotificationModes, which can be used to set specific modes for I/O completion notifications. One of the options for SetFileCompletionNotificationModes is the FILE_SKIP_COMPLETION_PORT_ON_SUCCESS flag, which, according to the documentation, prevents IOCP callbacks for operations that complete synchronously.

My Questions are:

  1. Is it always true that using FILE_SKIP_COMPLETION_PORT_ON_SUCCESS with SetFileCompletionNotificationModes will prevent callbacks from the IOCP on successful synchronous completion of WSASendTo/WSASend operations? Does it guarantee no callback when an operation completes synchronously?
  2. Is this the recommended way to handle synchronous completions? Should I rely on this API to avoid unnecessary IOCP callbacks, or is there a better or more standard way to manage this behavior?

I would appreciate any insights or recommendations from those who have experience with IOCP and WinSock, especially regarding how to best optimize this scenario and avoid redundant callbacks when operations complete synchronously.

Thanks in advance!

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,704 questions
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 10,356 Reputation points Microsoft Vendor
    2024-12-17T05:36:01.9566667+00:00

    The FILE_SKIP_COMPLETION_PORT_ON_SUCCESS flag specifies that a completion is not queued to the associated I/O completion port in the case the call completes synchronously.

    In my opinion, you could use FILE_SKIP_COMPLETION_PORT_ON_SUCCESS with SetFileCompletionNotificationModes to prevent callbacks from the IOCP on successful synchronous completion of WSASendTo/WSASend operations.

    You could refer to the Blog: https://devblogs.microsoft.com/oldnewthing/20190719-00/?p=102722

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.