After calling CancelIo, wait for all IO operations to finish before closing handles and freeing OVERLAPPED structures.

This fixes a bug found by Kusti, where a stack corruption could occur when an OVERLAPPED structure is released before all IO operations are finished.