Wednesday, July 24, 2013

Windows File Locking Troubleshooting

A common problem for computer users, and Windows developers in particular, is the errant locked file. Some process, often a misbehaving application or development project, refuses to release a file it is 'using' at the time. In development these could be files that need to be modified in order to test a change in source.

From Wikipedia
File locking is a mechanism that restricts access to a computer file by allowing only one user or process access at any specific time. Systems implement locking to prevent the classic interceding update scenario.
The most common way a user may try to solve this issue is to close programs, end tasks via the Windows Task Manager, and delete other files in a folder you are trying to delete to identify the actual problem. When these methods fail, I'm happy to report there are plenty of good utilities to tackle this problem.

LockHunter is a Windows Explorer context menu extension that allows you to quickly see what processes are locking a file and unlock the file with a streamlined GUI. While it may not be as powerful as some of the other lock utilities, it is dead simple to use. This is actually my choice for handling locking issues.

Handle is a console based application that gives Windows users a lot of the tools a Unix terminal user might take for granted. The obvious utility here is that you can use Handle to script automatic process killing. This might be particularly useful if part of your automation toolchain is consistently not closing processes cleanly. An example I encountered at work was an integration test framework that failed to close the created browser instances if any tests threw an exception. Which they did, frequently and with opaque error messages. Scripting a simple process kill before starting a new run of our tests let us continue to work, while the framework developers ironed out the bug.

Process Explorer is the full featured GUI version of Handle. I know this is the go to for many of my Windows developer peers. The following description was copied from Process Explorer download page:
The Process Explorer display consists of two sub-windows. The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that Process Explorer is in: if it is in handle mode you'll see the handles that the process selected in the top window has opened; if Process Explorer is in DLL mode you'll see the DLLs and memory-mapped files that the process has loaded. Process Explorer also has a powerful search capability that will quickly show you which processes have particular handles opened or DLLs loaded.
Any of the tools listed above are good places to start exploring tools to handle file locking problems. Do you have a favorite tool to handle file locking? Don't know which tool is right for you? Write to me about it with the contact form show to the right (or below on mobile).