Duplicate Activities in Odoo: Why Checking the Status Is Not Enough
Duplicate Activities in Odoo: Why Checking the Status Is Not Enough
In Odoo, chained mail.activity records are a clean way to drive a business process: one activity is completed, the next one is created automatically. Until a user double-clicks Mark as Done and two follow-up activities appear.
This post covers the cause, two fixes that were not enough, and the one that worked.
The symptom
There is a chain of activities: A is completed, then B is created. With a fast double-click, B gets created twice. Nothing shows up in the log. The data is simply wrong, and you only find out when someone notices duplicated work.
The root cause: a race condition
Two clicks mean two requests processed almost at the same time, each in its own transaction:
- Request 1 reads activity A: still active.
- Request 2 reads activity A: still active (request 1 has not committed yet).
- Both pass the check, both complete A, and both create B.
The gap sits between the check and the action. This is the check-then-act pattern, and it is unsafe whenever more than one process can run at the same time.
First fix: patch the UI
The quickest step is to disable the button after the first click and avoid duplicate reloads in JavaScript. This is worth doing and it removes many cases.
But it is not enough, because the UI is not the only source of requests. Slow connections, clicks from two tabs, automatic retries, or direct RPC calls can still send two requests at once. The protection has to live on the server.
Second fix: check the status on the server
Next, the server checks whether the activity is still active before continuing. This reduces the problem, but the same check-then-act gap remains, because two requests can both read "still active" before either one writes.
The fix that worked: row locking
The solution is to make the second request wait until the first one finishes. PostgreSQL provides that with SELECT ... FOR UPDATE:
def _lock_feedback_rows(self): """Serialize concurrent mark-done requests.""" ids = sorted(self.ids) if not ids: return self.browse() self.env.cr.execute( "SELECT id FROM mail_activity WHERE id IN %s FOR UPDATE", [tuple(ids)], ) locked_ids = [row[0] for row in self.env.cr.fetchall()] return self.browse(locked_ids) def action_feedback(self, feedback=False, attachment_ids=None): # Idempotency guard: once the lock is acquired, an activity that # another request already completed is no longer active and is skipped. locked_active = self._lock_feedback_rows().filtered("active") if not locked_active: return False return super(MailActivity, locked_active).action_feedback( feedback=feedback, attachment_ids=attachment_ids )
(The snippet above is simplified from the real implementation.)
The flow is now:
- Request 1 locks the row of activity A and starts working.
- Request 2 tries to lock the same row and waits.
- Request 1 finishes and commits: A is no longer active, and B is created once.
- Request 2 gets the lock, sees that A is no longer active, and returns without creating a second B.
The result is a single follow-up activity, no matter how many times the button is pressed.
Two details that are easy to miss
Sort the ids before locking. If two transactions lock several rows in different orders, they can end up waiting on each other (a deadlock). With sorted(self.ids), every transaction locks in the same order.
Check after the lock, not before. The idempotency guard (filtered("active")) must run after the lock is acquired. Checking before locking brings back the same gap.
Why not a unique constraint?
A unique constraint in the database also protects against duplicates, and it is a good idea when a natural key exists (for example, one follow-up activity per source). Here, what needs to be serialized is the status transition of one record, so a row lock fits better. You can use both together as layers of defense.
Trade-offs to know about
- The second request waits, so a double-click causes a small delay.
- The row lock only protects code paths that go through this method. Other paths that create activities directly are not covered.
- This uses raw SQL. Use parameters (
%s) and never build the query with string formatting. - Any other place that uses "search first, then create" is exposed to the same problem.
How to test it
The simplest test calls action_feedback twice in a row on the same activity and asserts that only one follow-up activity exists. That tests the idempotency guard. Testing true concurrency needs two database connections running at once, which is harder to write. If you do not have that test yet, say so rather than claiming the fix is fully tested.
Takeaways
- A UI fix is only a mitigation. The real protection must be on the server.
- Check-then-act is unsafe when processes run concurrently. Close the gap with a lock, a unique constraint, or an atomic operation.
- Idempotency is a design property. One source should produce exactly one effect, however many times it is called.
- Sort when locking several rows to avoid deadlocks.
- Write a test, at least for sequential double calls.
If you have seen a similar bug in an activity chain or in a cron job that creates records automatically, check whether a check-then-act gap is hiding there.
Author: Aditya Oryza Mahendra
Software Engineer with expertise in backend systems, Odoo ERP development (v11–19), and Web3/dApp development. Experienced in building scalable applications using Node.js, Express.js, React, and Next.js, alongside deep customization of Odoo ERP across logistics, finance, CRM, and manufacturing domains. Currently advancing into Web3 with hands-on experience building and deploying smart contracts and decentralized applications using Solidity, Hardhat, and Ethers.js. Strong background in SQL/NoSQL databases, Docker, and VPS/Linux server administration. Contributed to the automation of 500+ daily shipments at a national logistics company.
Connect on LinkedInRelated Articles
Keep an Odoo Computed Field Value When the Customer Changes
A stored computed Many2one with readonly=False resets whenever its dependency changes. A keep-if-valid rule fixes it across CRM, sales and invoices.
Copy an Odoo Production Database Safely: Why a Plain Restore Is Not Enough
Restoring a production database also restores its live e-mail, cron and storage settings. Here is how to copy it to your laptop safely, in three layers.