Loading
Refresh the update index lock while the job runs
On our site (Debian, Whoosh backend) the monthly update_and_clean_index job fails every month:
File "/usr/lib/python3/dist-packages/hyperkitty/jobs/update_and_clean_index.py", line 37, in execute
run_with_lock(update_index, remove=True)
File "/usr/lib/python3/dist-packages/hyperkitty/lib/utils.py", line 177, in run_with_lock
with lock:
File "/usr/lib/python3/dist-packages/flufl/lock/_lockfile.py", line 447, in __exit__
self.unlock()
File "/usr/lib/python3/dist-packages/flufl/lock/_lockfile.py", line 398, in unlock
raise NotLockedError('Already unlocked')
flufl.lock._lockfile.NotLockedError: Already unlockedThis is the failure from #300 (closed), which was fixed by adding HYPERKITTY_JOBS_UPDATE_INDEX_LOCK_LIFE (default 900s). #488 (closed) reports it again on a large installation. A longer fixed lifetime only works until the archive grows enough for the job to outlast it again.
Cause
hyperkitty.lib.utils.run_with_lock() has two problems:
- The lock is never refreshed. flufl.lock only extends a lock's lifetime when the holder calls
refresh()or checksis_locked, and nothing does either whileupdate_index(remove=True)runs. Once the lifetime (plus flufl.lock's 10s clock slop) has passed, the nextupdate_indexjob (hourly cron, or the django-q task) breaks the lock and takes it over. When the monthly job exits,Lock.__exit__finds it no longer holds the lock and raisesNotLockedError. Meanwhile two processes were updating the index at once. The same applies toupdate_indexitself, which uses the default 15-second lifetime. - The "already running" check never fires.
if lock.is_locked:asks whether thisLockobject holds the lock. A freshly createdLocknever does, so the warning branch is dead code. Every caller goes on towith lock:, which waits with no timeout. While the monthly job runs, each hourlyrunjobs hourly(and any later jobs in the same run) blocks behind it.
Changes
- While the job runs, a daemon thread calls
lock.refresh()every third of the lock's lifetime. A long run keeps its lock, and a lock left behind by a crashed process still expires after one lifetime.HYPERKITTY_JOBS_UPDATE_INDEX_LOCK_LIFEkeeps its default, but no longer has to cover the whole run. - The
is_lockedcheck is replaced bylock.lock(timeout=10s). A job that can't get the lock logs who holds it and skips the run instead of queueing behind a long one. The short wait still lets flufl.lock break a lock whose lifetime has expired. - If the lock is broken anyway, log an error and remove our claim file instead of letting
NotLockedErrorescape. - New tests cover skipping when the lock is held, refreshing past the lifetime, and a lock broken mid-run. The existing
run_with_locktests are unchanged. - Updated the comment in
example_project/settings.pyand added anews.rstentry.
Note the behaviour change: a job that finds the lock held now skips the run rather than waiting for it. The next scheduled run catches up.
See #300 (closed) and #488 (closed).