简体   繁体   中英

Exclusive file lock on windows, slowness

I am trying to use exclusive file locking as a lock among processes running on a different hosts. Most of the time it works fine. It is either succeed or returns ERROR_SHARING_VIOLATION In a loop I do:

CreateFileW(name,
    FILE_APPEND_DATA,
    FILE_SHARE_DELETE,
    NULL,
    OPEN_ALWAYS,      // open or create
    FILE_FLAG_DELETE_ON_CLOSE,  // delete at close file
    NULL);

Periodically it returns ERROR_ACCESS_DENIED but retries succeeded.

Issue: On rare occasions: Started processes stack on opening file: Let say a few succeeded but the rest(20 processes) a stuck for some significant amount of time (50 minutes) Lock file is visible and time stamp of it got updated.

Then one process got through, and at some later moment all of reminder processes through successfully in relatively short time (like a few seconds)

So 2 questions

  1. How to fix such behavior?
  2. Minor question: Why ERROR_ACCESS_DENIED returned?

Thank you.

Env:

  • File on Windows Server 2012 R2
  • Programs on Windows 2008 R2 Protocol
  • SMB 2.1

ACCESS_DENIED is likely this:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx

If you call CreateFile on a file that is pending deletion as a result of a previous call to DeleteFile, the function fails. The operating system delays file deletion until all handles to the file are closed. GetLastError returns ERROR_ACCESS_DENIED.

You don't call DeleteFile, but you do an equivalent thing.

You did not quite point out what is the undesirable part of the behavior you observe. I would guess that Creates getting stuck -- in which case I'd recommend you try adding FILE_COMPLETE_IF_OPLOCKED flag.

Another thing you may be affected with is tunneling and the delete-on-close. Check this https://support.microsoft.com/en-us/kb/172190 . So disable tunneling and see what it brings you.

To further diagnose, you may want to run a procmon trace on both affected clients and the server that contains the affected SMB share.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM