We have a problem in production with a Windows application written in a mix of C and C++ where MoveFileEx occationally reports "The process cannot access the file because it is being used by another process.". The problem is rare but recently we have been able to reproduce the problem in our development environment. What happens is that process A receives data over the network and creates a file with that data using:
fd = _open(fileName, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, _S_IREAD | _S_IWRITE);
...
while (data on network) {
write(fd, buffer, count);
}
...
int close_result = _close(fd);
After this Process A opens the file for reading and closes it again. Process A then, without exiting, creates process B. Under very high load, with many threads in A and many B processes in parallel, each handling hundreds of MB, process B sometimes gets the MoveFileEx problem. When I run Process Monitor to capture file system activity I can see that when we get the problem the above mentioned _close(fd) is called and returns no error, but Process Monitor does not register any CloseFile operation from process A for that _close(fd) call. It seems as the _close(fd) call does not reach the operating system. Have anyone encountered a problem like this? Any ideas what to try to fix this? I have tried using fopen instead of _open and different kind of flush calls, but nothing seems to help.