当并发MSBuild任务正在运行且CopyFile任务在只读文件上执行时,此问题确实浮出水面.当执行robocopy以从网络共享并行下载TFS构建的依赖项时,也会发生这种情况.由于所有这些问题都指向kernel32 CopyFile(或其私有实现),我怀疑该问题与Windows如何复制文件有关.
这似乎不是内核中的一般问题,因为临时文件夹源于必须可以并发访问目录这一事实.但是CopyFile的kernel32.dll里面的用户模式实现似乎有缺陷.
更新2
使用下面的repro无论文件是否只读,都会发生这种情况.
更新3
此repro也在Windows 8上显示相同的问题.
procmon堆栈跟踪确实表明魔术发生在kernel32.dll里面
PrivCopyFileExW似乎没有记录.发出一个IRP_MJ_CREATE调用来打开目录,稍后该目录被关闭.当许多并行robocopy进程尝试将文件复制到一个目录中时,这似乎是竞争条件的根本原因.
为什么PrivCopyFileExW会设法锁定目录?文件系统应该能够支持将文件复制到一个目录中.我正在使用Windows Server 2008 R2和一些带有RAID阵列,SSD等的最新多核机器.
这似乎与CopyFile in kernel32.dll报告的问题有关,直到今天才解决.我可以排除病毒扫描程序,因为这也发生在没有安装的机器上.
更新1
似乎另一个robocopy进程会尝试将文件复制到打开目录的目标目录
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
另一个robocopy想检查文件是否已经存在并调用FindFirstFile,这导致打开目录以及完全共享.
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
我也可以在Windows 7上轻松地重现这一点.您只需将两个并行robocopy调用中的只读文件复制到循环中的同一目录中,并等到它发生(大约30秒).
for /L %i in (1,1,1000) do robocopy /E /XO /COPY:DAT /A-:R C:\ReadOnlySource1 c:\temp\dest for /L %i in (1,1000) do robocopy /E /XO /COPY:DAT /A-:R C:\ReadOnlySource2 c:\temp\dest
您只能将一个只读文件放入源目录中以获得快速复制和许多并发目录访问.这是Windows的一个已知限制,在文件复制到目录时不允许访问目录吗?