summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntony Lesuisse <al@openerp.com>2014-02-28 17:15:24 +0100
committerAntony Lesuisse <al@openerp.com>2014-02-28 17:15:24 +0100
commited7ee4df522fd71b63e809f62a07db8ea30210ce (patch)
tree997c1156925d4b916346dad7b71502f7a97fd88b
parent1d227c0b242009888a3749d41f0df9a1b6aaa5a1 (diff)
parentef974c7fe5d221c3b1de68f2b6eee2a30b2c01d7 (diff)
[MERGE] cron race condition leading to unneeded executions, courtesy of acsone
When mulitple cron workers get their list of jobs to process at the same time, some jobs might be executed multiple times. We fix this by keeping the listing filter when taking the lock. bzr revid: al@openerp.com-20140228161524-y8nyq5uw9yq9rcc3
-rw-r--r--openerp/addons/base/ir/ir_cron.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/openerp/addons/base/ir/ir_cron.py b/openerp/addons/base/ir/ir_cron.py
index 60a69cd6b38..e5a5e5f024b 100644
--- a/openerp/addons/base/ir/ir_cron.py
+++ b/openerp/addons/base/ir/ir_cron.py
@@ -225,12 +225,21 @@ class ir_cron(osv.osv):
lock_cr = db.cursor()
try:
# Try to grab an exclusive lock on the job row from within the task transaction
+ # Restrict to the same conditions as for the search since the job may have already
+ # been run by an other thread when cron is running in multi thread
lock_cr.execute("""SELECT *
FROM ir_cron
- WHERE id=%s
+ WHERE numbercall != 0
+ AND active
+ AND nextcall <= (now() at time zone 'UTC')
+ AND id=%s
FOR UPDATE NOWAIT""",
(job['id'],), log_exceptions=False)
+ locked_job = lock_cr.fetchone()
+ if not locked_job:
+ # job was already executed by another parallel process/thread, skipping it.
+ continue
# Got the lock on the job row, run its code
_logger.debug('Starting job `%s`.', job['name'])
job_cr = db.cursor()