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 unlocked

This 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:

  1. The lock is never refreshed. flufl.lock only extends a lock's lifetime when the holder calls refresh() or checks is_locked, and nothing does either while update_index(remove=True) runs. Once the lifetime (plus flufl.lock's 10s clock slop) has passed, the next update_index job (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 raises NotLockedError. Meanwhile two processes were updating the index at once. The same applies to update_index itself, which uses the default 15-second lifetime.
  2. The "already running" check never fires. if lock.is_locked: asks whether this Lock object holds the lock. A freshly created Lock never does, so the warning branch is dead code. Every caller goes on to with lock:, which waits with no timeout. While the monthly job runs, each hourly runjobs 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_LIFE keeps its default, but no longer has to cover the whole run.
  • The is_locked check is replaced by lock.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 NotLockedError escape.
  • New tests cover skipping when the lock is held, refreshing past the lifetime, and a lock broken mid-run. The existing run_with_lock tests are unchanged.
  • Updated the comment in example_project/settings.py and added a news.rst entry.

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).

🤖 Generated with Claude Code

https://claude.ai/code/session_01EuYPdLdEPHAxVMFSVf1gxD

Merge request reports

Loading
Loading