-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interrupts on Windows #29
Comments
Thanks! Why not create a GitHub account, and add this as an issue? You did a As for the issue, you are right, and freeing the cancelled handles can Waiting for the cancellation is not great, btw. Whenever you try to Anyway, if you don't want to register to GitHub, then can I add your Thanks again, |
Thanks for your reply! I'm sure I will create a GitHub account, sooner If it simplifies management and discussion of issues for you and your
Another good reason to call GetOverlappedResult(), besides the Indeed, in case of the race condition, i.e. the locking IO request
Cancelled, but nonetheless successfully completed reads (for which Yes, all this doesn't look trivial. With short lock timeouts of --Florian |
From Florian Balmer
Hello Gábor
Please apologize for contacting you directly via e-mail, but I don't
have a GitHub account to submit an issue, and your address was
immediately revealed by Googling.
While researching about how to acquire file locks, and abort after a
specified timeout on already-locked files, I've come across your
[r-lib/filelock] repository on GitHub.
First of all, thank you very much for sharing your code!
In my supplementary resources [0-4] besides MSDN [5-8], I found the
following pattern:
OVERLAPPED structure allocated on local function stack
Issue asynchronous request
...
if (ERROR_IO_PENDING)
{
Wait for IO completion, or timeout elapse
if (timeout elapsed)
{
CancelIo()
GetOverlappedResult() --or-- WaitForSingleObject(OVERLAPPED.hEvent)
...
The emphasize here is that it's necessary to wait for the canceled IO
operation to complete, before returning from the function, or the
completion routine may access the implicitly free'd stack-allocated
OVERLAPPED structure, and cause potential havoc.
The documentation for LockFileEx() gives the hint to call
GetOverlappedResult() in case of ERROR_IO_PENDING. However, the
documentation for GetOverlappedResult() does not mention this case
explicitly, just Read/WriteFile(), DeviceIoControl(), and a few
others. And I'm not sure if file locking goes down the
DeviceIoControl() route, or is implemented above/besides the file
system layer, so the documentation leaves some ambiguity, if I get
everything correct.
Anyway, I tend to believe it's a good idea to wait for IO completion
after calling CancelIo(), and to be overly correct, maybe only after
CancelIo() returned a nonzero value (but neither of the code samples
from the supplementary resources care about the CancelIo() return
value).
This is just meant to be an input, feel free to do whatever you like
with this feedback. I don't even expect you to reply to this message
(I can't check my e-mails daily, anyway, at the moment). But if you
have the knowledge, or know some expert to confirm that calling
GetOverlappedResult() is wrong after canceling an asynchronous lock
request, I'd appreciate a short note.
Best wishes
--Florian
[0] You can use a file as a synchronization object, too | The Old New Thing
https://devblogs.microsoft.com/oldnewthing/20140905-00/?p=63
[1] Ready... cancel... wait for it! (part 1) | The Old New Thing
https://devblogs.microsoft.com/oldnewthing/20110202-00/?p=11613
[2] Ready... cancel... wait for it! (part 2) | The Old New Thing
https://devblogs.microsoft.com/oldnewthing/20110203-00/?p=11603
[3] Ready... cancel... wait for it! (part 3) | The Old New Thing
https://devblogs.microsoft.com/oldnewthing/20110204-00/?p=11583
[4] Asynchronous I/O | Newcomer J. M.
http://flounder.com/asynchexplorer.htm
[5] LockFileEx function
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex
[6] CancelIo function | MSDN
https://docs.microsoft.com/en-us/windows/win32/fileio/cancelio
[7] GetOverlappedResult function | MSDN
https://docs.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-getoverlappedresult
[8] DeviceIoControl function
https://docs.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-deviceiocontrol
The text was updated successfully, but these errors were encountered: