Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with database cron job locks #243

Open
akjackson67 opened this issue Nov 14, 2023 · 2 comments
Open

Error with database cron job locks #243

akjackson67 opened this issue Nov 14, 2023 · 2 comments

Comments

@akjackson67
Copy link

akjackson67 commented Nov 14, 2023

Occasionally this error is being logged when running cron loop on multiple containers with PostgreSQL database.

Traceback (most recent call last):
  File "/opt/app-root/lib64/python3.9/site-packages/django_cron/management/commands/runcrons.py", line 82, in run_cron_with_cache_check
    manager.run(force)
  File "/opt/app-root/lib64/python3.9/site-packages/django_cron/__init__.py", line 292, in run
    self.stdout.write(u"[ ] {0}\n".format(self.cron_job.code))
  File "/opt/app-root/lib64/python3.9/site-packages/django_cron/backends/lock/base.py", line 66, in __exit__
    self.release()
  File "/usr/lib64/python3.9/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/opt/app-root/lib64/python3.9/site-packages/django_cron/backends/lock/database.py", line 25, in release
    lock.locked = False
AttributeError: 'NoneType' object has no attribute 'locked'
@akjackson67
Copy link
Author

akjackson67 commented Nov 14, 2023

I believe this problem is caused because the lock method of database.py allows two or more threads to set the locked attribute to true. If both threads select the record before it is locked, then both threads will believe that it has obtained an exclusive lock on the cron job. This could be corrected by adding a select_for_update to the get_or_create. Like follows:

        lock, created = CronJobLock.objects.select_for_update().get_or_create(job_name=self.job_name)

That will force the second thread to wait until update has been saved.

@guillempuey
Copy link

+1

Please, fix this with select_for_update()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants