Garbage collector suspends the current process, if there is a long-running process with a lock in background
fabik opened this issue · 6 comments
I have a long-running script that runs in background and I use FileStorage::lock() to prevent multiple instances of the script to be running at the same time.
But there is a problem when the garbage collector runs the FileStorage::readMetaAndLock() method on the lock file, because it takes a long time to be released. The current process is suspended, until the long-running script releases the lock.
FileStorage::lock() is not intended for user-land thread safety.
Then I think, it should be mentioned in the PHP doc of method lock().
Do you really think that it does make sense to write to phpDoc of all method all possible ways how it should not be used?
I think it should be mentioned somewhere in the documentation. It can make the application unusable, because with 1:1000 probabilty the page was loading infinitely without any error message in log. And if there were thousands of requests to the application for a while, all apache processes were blocked and waiting for the release of the lock.
@fabik Don't use Caching FileStorage for user-land locking it is intended purely for cache related locking done by this library and nothing else hence there is no need to document it more. You are abusing internal class for own stuff (user-land).
If you are concerned by generic caching and chance of 1 in 1000 requests you should use kdyby/redis and cache via redis which is much faster.
Locking is very complex issue with headache edge cases if you need just one script I would suggest using different tools. If you need help go to nette jabber channel or we can discuss it on forum.
I've already switched to a different approach. I agree, that it should be used only for cache related locking and I used it incorrectly. I was just confused, that the application was down and there was no error in the error log.