3

There are a number of reasons to call CancelIo, but in my particular case I'm calling it in order to know that the system is no longer writing into a buffer. Once I know that, I can safely free the buffer.

But what if CancelIo fails? What I do now is explicitly leak the buffer and throw an exception. Are there better ways to deal with this?

P.S. Analogous calls for Europa, Ganymede, and Callisto seem to be missing. Should I file a bug?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Integer Poet
  • 747
  • 5
  • 19

4 Answers4

0

My answer maybe not related to your question. but could be help..

In async IO..CancelIoEx cancel pending IO. If there is pending IO it returns 1 and generate completion message or call completion code. If there is no pending IO it returns 0 with errcode 1168..no completion message and no call completion code. But actually. there could arrive schedulled IO just after cancelioex called. This is all root of evil. I thought canceled all IO. but there is still remaining IO..

Mark Yang
  • 226
  • 3
  • 16
0

The MSDN docs are not very clear on what errors could be returned. I imagine (since CancelIo is asynchronous anyway) that this means you used a bad handle, or something major like that. By asynchronous I mean that just because CancelIo returns OK, you cannot immediately release the buffer for any pending I/O.

It is stated in the docs that pending I/Os will be returned with ERROR_OPERATION_ABORTED. I would think you already track pending I/O state such that you could safely release the buffer if and only if all pending I/Os return this error. If a pending I/O is left hanging after CancelIo, releasing the buffer could cause a cascade of undesirable side effects.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

You shouldn't immediately delete your buffers after issuing a cancel request.

Taken from the CancelIoEx documentation :

If there are any pending I/O operations in progress for the specified file handle, the CancelIoEx function marks them for cancellation. Most types of operations can be canceled immediately; other operations can continue toward completion before they are actually canceled and the caller is notified. The CancelIoEx function does not wait for all canceled operations to complete.

So CancelIo does not 'release' the io operation, but marks it only as 'cancelled'. You have to wait on the result of your async-Read/Write to finish (either successfully or with ERROR_OPERATION_ABORTED.

Christopher
  • 8,912
  • 3
  • 33
  • 38
  • No worries. I wait on the relevant event before freeing the buffer. – Integer Poet Oct 17 '10 at 23:07
  • Note that this restriction applies to `CancelIoEx`, with which you still have to call `GetOverlappedResult`. `CancelIO`, seems to cancel synchronously (probably since it's called on the issuing thread anyway) – peterchen Oct 28 '15 at 10:52
0

I don't think there is an answer here any more satisfying than the one I already found:

Leak the buffer (and throw an exception (or do the equivalent for your environment)).

Integer Poet
  • 747
  • 5
  • 19