PrivCopyFileExW error on Windows?

I make many concurrent robocopy calls to copy files from one network share to one directory. Since the files are read only, I tell robocopy to disable the read-only attribute in the target directory via / A -: R. It seems that on some many main machines (12 or more) the target directory! locks up to 16 seconds.

This problem occurs when parallel MSBuild tasks are executed, and the CopyFile task is executed in read-only files. This also happens when robocopy runs to load dependencies for building TFS in parallel with a network share. Since all these problems point to kernel32 CopyFile (or its private implementation), I suspect that the problem is related to the way Windows copies files.

This does not seem to be a common problem in the kernel, as the temporary folder lives because directory access is possible in parallel. But the user mode implementation inside kernel32.dll CopyFile seems erroneous.

Update 2 When playing below, this happens regardless of whether the file is read-only or not.

Update 3 This registry also shows the same problem in Windows 8.

Traces of the procmon stack have shown that magic happens in kernel32.dll inside PrivCopyFileExW, which seems pretty undocumented. IRP_MJ_CREATE opens there to open the directory, and a little later the directory is closed. This, apparently, is the main cause of the race condition, when many concurrent robocopy processes try to copy files to a single directory.

Here are some procmon to deduce how this problem looks.

Why does PrivCopyFileExW actually manage to lock the directory? The file system must support copying files to a single directory. I am using Windows Server 2008 R2 and some of the latest multi-core machines with RAID arrays, solid state drives, etc.

This is similar to reporting problems with CopyFile in the kernel32.dll file that have not yet been resolved. I can exclude antivirus scanners because this also happens on computers that don't have them.

Update 1

Another robocopy process seems to be trying to copy the file to the destination directory, which opens the directory

Date & Time: 20.03.2012 08:30:06 Event Class: File System Operation: CreateFile Result: SUCCESS Path: C:\temp\dest TID: 11672 Duration: 0.0000150 Desired Access: Read Data/List Directory, Write Data/Add File, Write EA, Read Attributes, Write Attributes, Delete, Synchronize Disposition: OpenIf Options: Directory, Synchronous IO Non-Alert, Open For Backup Attributes: D ShareMode: None <---- No sharing AllocationSize: 0 OpenResult: Opened 0 fltmgr.sys FltpPerformPreCallbacks + 0x2f7 0xfffff88001045027 C:\Windows\system32\drivers\fltmgr.sys 1 fltmgr.sys FltpPassThroughInternal + 0x4a 0xfffff880010478ca C:\Windows\system32\drivers\fltmgr.sys 2 fltmgr.sys FltpCreate + 0x293 0xfffff880010652a3 C:\Windows\system32\drivers\fltmgr.sys 3 ntoskrnl.exe IopParseDevice + 0x5a7 0xfffff800031cb537 C:\Windows\system32\ntoskrnl.exe 4 ntoskrnl.exe ObpLookupObjectName + 0x585 0xfffff800031c1ba4 C:\Windows\system32\ntoskrnl.exe 5 ntoskrnl.exe ObOpenObjectByName + 0x1cd 0xfffff800031c6b7d C:\Windows\system32\ntoskrnl.exe 6 ntoskrnl.exe IopCreateFile + 0x2b7 0xfffff800031cd647 C:\Windows\system32\ntoskrnl.exe 7 ntoskrnl.exe NtCreateFile + 0x78 0xfffff800031d7398 C:\Windows\system32\ntoskrnl.exe 8 ntoskrnl.exe KiSystemServiceCopyEnd + 0x13 0xfffff80002eca813 C:\Windows\system32\ntoskrnl.exe 9 ntdll.dll NtCreateFile + 0xa 0x7718fc0a C:\Windows\System32\ntdll.dll 10 kernel32.dll BaseCopyStream + 0x11a9 0x77034b89 C:\Windows\System32\kernel32.dll 11 kernel32.dll BasepCopyFileExW + 0x545 0x77033d85 C:\Windows\System32\kernel32.dll 12 kernel32.dll PrivCopyFileExW + 0xb6 0x770b5296 C:\Windows\System32\kernel32.dll 13 Robocopy.exe CZDir::CopyData + 0xb5 0xff8623a9 C:\Windows\System32\Robocopy.exe 14 Robocopy.exe RoboCopyDir + 0xe4 0xff85af00 C:\Windows\System32\Robocopy.exe 15 Robocopy.exe Walk + 0x12a 0xff85c6b6 C:\Windows\System32\Robocopy.exe 16 Robocopy.exe wmain + 0x4f4 0xff85de78 C:\Windows\System32\Robocopy.exe 17 Robocopy.exe operator+ + 0x19b 0xff867be5 C:\Windows\System32\Robocopy.exe 18 kernel32.dll BaseThreadInitThunk + 0xd 0x7703f33d C:\Windows\System32\kernel32.dll 19 ntdll.dll RtlUserThreadStart + 0x1d 0x77172ca1 C:\Windows\System32\ntdll.dll 

Another robocopy wants to check if the file already exists and calls FindFirstFile, which leads to the opening of the directory, as well as in full use.

 Date & Time: 20.03.2012 08:30:06 Event Class: File System Operation: CreateFile Result: SHARING VIOLATION Path: C:\temp\dest TID: 8280 Duration: 0.0000099 Desired Access: Read Data/List Directory, Synchronize Disposition: Open Options: Directory, Synchronous IO Non-Alert Attributes: n/a ShareMode: Read, Write, Delete <----- Full sharing AllocationSize: n/a 0 fltmgr.sys FltpPerformPreCallbacks + 0x2f7 0xfffff88001045027 C:\Windows\system32\drivers\fltmgr.sys 1 fltmgr.sys FltpPassThroughInternal + 0x4a 0xfffff880010478ca C:\Windows\system32\drivers\fltmgr.sys 2 fltmgr.sys FltpCreate + 0x293 0xfffff880010652a3 C:\Windows\system32\drivers\fltmgr.sys 3 ntoskrnl.exe IopParseDevice + 0x5a7 0xfffff800031cb537 C:\Windows\system32\ntoskrnl.exe 4 ntoskrnl.exe ObpLookupObjectName + 0x585 0xfffff800031c1ba4 C:\Windows\system32\ntoskrnl.exe 5 ntoskrnl.exe ObOpenObjectByName + 0x1cd 0xfffff800031c6b7d C:\Windows\system32\ntoskrnl.exe 6 ntoskrnl.exe IopCreateFile + 0x2b7 0xfffff800031cd647 C:\Windows\system32\ntoskrnl.exe 7 ntoskrnl.exe NtOpenFile + 0x58 0xfffff800031e64a8 C:\Windows\system32\ntoskrnl.exe 8 ntoskrnl.exe KiSystemServiceCopyEnd + 0x13 0xfffff80002eca813 C:\Windows\system32\ntoskrnl.exe 9 ntdll.dll NtOpenFile + 0xa 0x7718f9ea C:\Windows\System32\ntdll.dll 10 KernelBase.dll FindFirstFileExW + 0x1ee 0x7fefd3a560e C:\Windows\System32\KernelBase.dll 11 KernelBase.dll FindFirstFileW + 0x1c 0x7fefd3a58dc C:\Windows\System32\KernelBase.dll 12 Robocopy.exe CZDir::Exists + 0xf7 0xff861bb7 C:\Windows\System32\Robocopy.exe 13 Robocopy.exe RoboCopyDir + 0x58 0xff85ae74 C:\Windows\System32\Robocopy.exe 14 Robocopy.exe Walk + 0x12a 0xff85c6b6 C:\Windows\System32\Robocopy.exe 15 Robocopy.exe wmain + 0x4f4 0xff85de78 C:\Windows\System32\Robocopy.exe 16 Robocopy.exe operator+ + 0x19b 0xff867be5 C:\Windows\System32\Robocopy.exe 17 kernel32.dll BaseThreadInitThunk + 0xd 0x7703f33d C:\Windows\System32\kernel32.dll 18 ntdll.dll RtlUserThreadStart + 0x1d 0x77172ca1 C:\Windows\System32\ntdll.dll 

I can reproduce this easily on Windows 7. You only need to copy read-only files from two parallel robocopy calls to the same directory in a loop and wait until this happens (about 30 seconds).

 for /L %i in (1,1,1000) do robocopy /E /XO /COPY:DAT /A-:RC:\ReadOnlySource1 c:\temp\dest for /L %i in (1,1,1000) do robocopy /E /XO /COPY:DAT /A-:RC:\ReadOnlySource2 c:\temp\dest 

You can only put one read-only file in the source directories to get a quick copy and many directories accessed simultaneously. Is this a known limitation of Windows to not allow access to a directory when a file is copied to it?

My uneducated opinion is that this is a mistake, and it can become quite unpleasant if you want a reliable way to share files.

+4
source share
1 answer

It looks like we will finally fix MS for this problem. They found and understood the problem. But it will take some time until the correction is officially prepared. It will currently be installed only for Windows 7.

+1
source

Source: https://habr.com/ru/post/1402250/


All Articles