How long can a file be locked in windows after closing a program?

In several scenarios that I use, I have a problem that is intermittent.
Sometimes the script does not work when trying to delete a file. According to the error log due to file access by another process. I assume that the windows did not manage to release the file after completing the previous operation on the file.

How long would it be a good expectation, after which windows should have had time to release the file again?

+4
source share
2 answers

If the Windows application is finished working with the file, it should be closed immediately, because presumably they have closed their file descriptors. There is no delay in unlocking the file while closing the file.

If the program forgets to close its file descriptors, but ends, Windows will release them for them (just not right away). Usually it is not long, but it can be any amount of time, I have not seen it take more than two seconds. But proper cleaning must be done to avoid blocking it.

It is also worth noting that not all programs open files in a locked way. They can open a file that determines the type of access that they would like to grant to other processes, and they can also block parts of the file. They can open a file with full read and write permissions to other processes.

If you do not have control over a process that does not close its file descriptors, but you need to execute it, you can write some loop to continue working on the file for several seconds.

+2
source

As another user wrote, this should be done immediately if the file was closed correctly - with an unplanned delay, until the OS sorts it otherwise ...

Always, always properly manage resources.

0
source

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


All Articles