asynchronouswinapiwritefileiocp

does WriteFile post a completion packet on finishing immediately when using io completion ports


I know that when WSARecv is used with it may finishes immediately and in such case a completion packet will still be posted and this behavior can be altered using SetFileCompletionNotificationModes

but I want to know the case for files and pipes with WriteFile , if I used iocp to make async reads and writes and an operation has finished immediately will the same behavior take place ? or I have to handle this ?


Solution

  • exist common behavior for all asynchronous api, it implicit described in CancelThreadpoolIo function

    note, by design we need call CancelThreadpoolIo when and only when will not notify to the I/O completion port and the associated I/O callback function will not be called.

    you must call the CancelThreadpoolIo function for either of the following scenarios:

    • An overlapped (asynchronous) I/O operation fails (that is, the asynchronous I/O function call returns failure with an error code
      other than ERROR_IO_PENDING).
    • An asynchronous I/O operation returns immediately with success and the file handle associated with the I/O completion object has the
      notification mode FILE_SKIP_COMPLETION_PORT_ON_SUCCESS. The file
      handle will not notify the I/O completion port and the associated I/O callback function will not be called.

    so let dwErrorCode error code returned by api (NOERROR if win32 api return TRUE)

    the next function say you - will be or not notify to IOCP

    bool IsWillBeNotification(ULONG dwErrorCode, BOOL bSkipCompletionPortMode)
    {
        switch (dwErrorCode)
        {
        case ERROR_IO_PENDING:
            return true;
        case NOERROR:
            return !bSkipCompletionPortMode;
        default:
            return false;
        }
    }
    

    in case we use native API and NTSTATUS

    bool IsWillBeNotification(NTSTATUS status, BOOL bSkipCompletionPortMode)
    {
        return (status == STATUS_PENDING) || (!NT_ERROR(status) && !bSkipCompletionPortMode);
    }
    

    so:

    note that this is general rule for any asynchronous io api. only NtLockFile/LockFileEx - exception from this rule - can be IOCP notification even if api just fail, due to error inside NtLockFile implementation