1

I am calling CreateFile to open a file for reading and writing on an external Windows volume running over VMWare. However, if the disk is disconnected for any reason (after having been previously connected), CreateFile will hang indefinitely. How can I terminate the CreateFile attempt safely?

I've tried starting a thread to do the CreateFile and then using CancelSynchronousIO, but this call also seems to block indefinitely. As far as I can tell, the only way to abort the CreateFile attempt is with TerminateThread, which I know is a messy option that is never recommended. Is TerminateThread safe to do in this instance?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
user2563087
  • 151
  • 1
  • 8
  • try this answer https://stackoverflow.com/a/14638926/47733 – lsalamon Feb 24 '22 at 00:47
  • 1
    *"Is TerminateThread safe to do in this instance?"* - No. At best this will orphan a synchronization object the system created on your behalf. If you get unlucky, you'll wind up with a lock held indefinitely, meaning that you by solving one deadlock you introduced the opportunity for another deadlock. – IInspectable Feb 24 '22 at 09:05
  • @IInspectable Are there any alternatives? I'm not seeing any, unfortunately. – user2563087 Feb 24 '22 at 18:25
  • @Isalamon That won't work. In this case, the disk is physically disconnected by some kind of hardware problem. – user2563087 Feb 24 '22 at 18:26
  • You *could* try to call into the Native API ([`NtOpenFile`](https://learn.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntopenfile)) and see if that makes any difference. If it does (which I doubt) then the issue is somewhere in the Win32 subsystem implementation. If it doesn't, I suspect a bug, either in the OS, its drivers, or VMWare's virtualization or drivers. This won't solve your immediate issue but might help you understand whom to file a defect report with. – IInspectable Feb 24 '22 at 19:42

0 Answers0