Find what has locked/held a file

Is there a good way of finding out what has locked a file or what is holding it so that it can’t be deleted?

In my code I unzip a few to a temporary folder, read the header lines with readline, and then read the bulk of the file with Afterwards I delete the file, or at least try to but in Windows always get the error IOError: unlink: resource busy or locked (EBUSY). I do close the file when I’m done reading from it.

Once my code errors out I can delete the file with rm in the REPL. I’m assuming when the function returns all the open files are closed. Maybe I should put the reading part in a separate function so ensure that everything is closed? That feels like a hack though.

Windows 10, Julia 0.7, Pkg.test(“ClimateDataIO”) specifically lines 286-292 of runtests.jl which tests ghg_read.jl.

This doesn’t address the issue of “who’s causing it?” but you might consider GC.gc() before trying to delete the file. If the file is opened with Mmap.mmap, then you wouldn’t be able to delete the file until any temporaries have been garbage-collected.

EDIT: if the variable is still local you might have to redefine it before this works. E.g., if x comes from a mmapped file, you might have to say x = 0; GC.gc().


Try this, or the following (Process Explorer, but that’s a separate download):

Finally had a chance to test it out and it works! I had to place it soon after the commands and it now works in Windows.

@ihnorton, cool, I didn’t know you could do that. I don’t think it would have helped in this case but it’s good to know.