php-lock / lock

Lock library to provide serialized execution of PHP code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RedisMutex unnecessarily generating new tokens

matt-allan opened this issue · comments

I'm having an issue I can't reliably reproduce. It seems like the lock is allowing concurrent access but I'm not 100% sure.

I noticed the RedisMutex generates a new token each time it tries to acquire the lock.

Looking at Antirez's implementation it looks like it keeps retrying with the same token.

Generating a new token each time increases the likelihood of deleting the key belonging to another process, since you now have multiple tries to generate a collision instead of just one.

I think this issue combined with rand not being very random is causing collisions fairly often.

Update: I added a call to $mutex->seedRandom(random_int(PHP_INT_MIN, PHP_INT_MAX)) and haven't seen a collision since. I'm not using a forked process; these are separate PHP processes.

Thanks for reporting. With the improved token generation, it won't matter to reuse or to generate new tokens for every try.