tags:

views:

53

answers:

1

Hi Guys,

I have a caching system build within php that stores the results of a mysql query in xml. I lock the cache when building by creating a lock file with an exclusive write handle, and then remove it once the cache file is completed.

However, there are times when the script either times out or halts mid execution, leaving the lock file in place, making any further executions of the script think that the cache is always updating.

I have tried checking for the file being a number of minutes old and attempting to get an exclusive write access to the file to refresh the lock and begin execution, however it appears that the file is still open with the previous handle and I cannot open the new handle to ensure the current process is the only one with access and relock the file.

Is there a way to ensure that if the script is halted mid execution, any open file handles get closed and the files involved are available for future processes to access?

Thanks

+3  A: 

When the PHP interpreter exits it calls fclose() on all open files, thus releasing any locks. Maybe you haven't found the right problem yet.

However, when you need to clean up something before the script terminates - use register_shutdown_function(). The registered function will be called even when the script terminates with an error.

Emil Ivanov
Thanks for the reponse Emil, I have discovered that I was using the wrong mode on my fopen when checking for stuck lock files, and it was always going to return false no matter what else I did.I am working now to change this and ensure that any result I get is a correct result and not a false resultSometimes finding out actual problem is harder than fixing it once you know what is wrong.