Yanshof
Yanshof

Reputation: 9926

Is it possible to call WriteFile and the application will wait for the callback forever?

I call the method win32::WriteFile and then I call

WaitForSingleObject( handle, INFINITE )

with the same handle that I used in the WriteFile call.

Is it possible to have some scenario so that I will wait forever... and WriteFile won't finish his writing? I am going to write 512 kB and I'm assuming my hardware has no problem.

I expect to get FAIL and not to wait forever.

Upvotes: 2

Views: 630

Answers (2)

Hans Passant
Hans Passant

Reputation: 942099

When you make an overlapped I/O call then you delegate responsibility to the driver to ensure it completes it in a timely manner. There's nothing reasonable you can do if the driver has a bug or the hardware is dysfunctional, no program can continue to operate in useful way when such low-level operations fail.

Beware that you've passed a pointer to a buffer that the driver is supposed to fill, continuing to run after the timeout expires leaves a dangling pointer, one that the driver may use to spray bytes into your process when it actually does manage to complete the request. You must at least call CancelIo() to solve that. And terminate the process if it returns false.

This all makes little sense, you need a minimum service guarantee from the operating system. You might as well call WriteFile() without OVERLAPPED. That same buggy driver will now hang your program. Just as it is not unlikely that CancelIo() will hang. Which is okay, it is the driver that has the problem, not you. It's not like the user won't notice that there's something seriously wrong with the machine, other programs will suffer from this kind of mishap as well.

Don't write code that deals with a very unlikely corner-case. And most of all, don't write code that you can't effectively test.

Upvotes: 1

Martin James
Martin James

Reputation: 24897

This should not happen, but it aparrently does with some drivers that are 'less than optimal'. Network operations can take ages before they return anyway, so I suggest you apply a long timeout, maybe 2 minutes, (1000*60*2), and if timed out, close/reopen and retry the writeFile().

Upvotes: 1

Related Questions