chatwoot/app/jobs
Muhsin Keloth 05dd31389e
fix(whatsapp): Prevent duplicate conversations from concurrent uploads (#14060)
When a WhatsApp contact starts a new conversation by sending multiple
images at once (an album), each image arrives as a separate webhook.
Because no conversation exists yet, the concurrent workers each pass the
"does a conversation exist?" check and each create their own
conversation — producing one conversation per image instead of one
grouped conversation.

This fix serializes webhook processing per `(inbox, contact)` using a
Redis lock at the job level, so only one webhook at a time can create
the initial conversation for a given contact. Concurrent workers retry
with backoff and append to the same conversation once the lock is
released.

## Closes

- Closes #13261

## How to test

1. On a WhatsApp inbox, ensure there is no active (open) conversation
with a specific test contact — resolve or delete any existing one.
2. From a phone, select 6+ images in the WhatsApp gallery and send them
as a single album to the Chatwoot-connected number.
3. Open the Chatwoot dashboard and confirm exactly **one** new
conversation is created, with all images grouped under it.
4. Repeat the test with a mix of attachment types (XMLs, PDFs, images)
sent in rapid succession — still one conversation.

## What changed

- New Redis key `WHATSAPP_MESSAGE_CREATE_LOCK::<inbox_id>::<sender_id>`
in `lib/redis/redis_keys.rb`.
- `Webhooks::WhatsappEventsJob` now inherits from `MutexApplicationJob`
and wraps event processing in `with_lock(key)`, matching the pattern
already used by `FacebookEventsJob`, `InstagramEventsJob`, and
`TiktokEventsJob`.
- Uses `retry_on LockAcquisitionError, wait: 1.second, attempts: 8` so
concurrent webhooks retry until the lock is free instead of poll-waiting
inside the service.
- Sender ID is derived from the webhook payload (contact's `from`, or
`to` for SMB echo events); status-only webhooks bypass the lock.
- Issue 1 from the report (same `source_id` redelivery) was already
handled previously by `Whatsapp::MessageDedupLock` (atomic `SET NX EX`);
no changes needed there.

---------

Co-authored-by: Muhsin <12408980+muhsin-k@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-28 10:30:27 +04:00
..
account fix: Prepend UTF-8 BOM to contact CSV export for non-ASCII character support (#14123) 2026-04-24 18:06:25 +05:30
agent_bots fix: [CW-6940] Fix SSRF issue for webhook trigger used by macros and automations (#14155) 2026-04-27 20:30:59 +05:30
agents chore: Reorganize Sidekiq Queues (#6976) 2023-05-04 15:44:16 +05:30
auto_assignment feat: Assignment service (v2) (#12320) 2025-11-17 10:08:25 +05:30
avatar fix: [CW-6931] Harden external downloads against SSRF [avatar from url job] (#14153) 2026-04-24 18:59:45 +05:30
campaigns chore: one off SMS campaign APIs (#2589) 2021-07-14 12:24:09 +05:30
channels feat: Added the backend support for twilio content templates (#12272) 2025-08-24 10:05:15 +05:30
companies feat: Add automatic favicon fetching for companies (#13013) 2026-03-05 18:51:28 -08:00
contacts feat: Bulk actions for contacts (#12763) 2025-10-30 15:28:28 +05:30
conversations fix: prevent NoMethodError in mute helpers when contact is nil (#13277) 2026-01-15 22:00:09 -08:00
crm feat: integrate LeadSquared CRM (#11284) 2025-04-29 09:14:00 +05:30
inboxes fix: handle ioerror in imap fetch (#13960) 2026-04-10 13:31:28 +05:30
internal feat: distributed scheduling for version check job (#13042) 2026-03-17 02:27:49 -07:00
labels fix: Update associations when a label is updated (#3046) 2021-09-21 10:16:32 +05:30
migration feat: Backend - Companies API endpoint with pagination and search (#12840) 2025-11-18 14:28:56 +05:30
notification perf: limit the number of notifications per user to 300 (#13234) 2026-01-28 17:35:13 +05:30
webhooks fix(whatsapp): Prevent duplicate conversations from concurrent uploads (#14060) 2026-04-28 10:30:27 +04:00
action_cable_broadcast_job.rb chore: Add more conversation events for reload (#10877) 2025-02-11 00:33:45 -08:00
application_job.rb chore: Improve active job error logs for deserialization error (#8742) 2024-01-18 19:27:18 +04:00
bulk_actions_job.rb feat: Add support for bulk snooze until (#9360) 2024-05-08 08:55:31 +05:30
contact_ip_lookup_job.rb feat(poc): Disable widget based on country (#6658) 2023-03-14 09:09:57 -07:00
conversation_reply_email_job.rb feat: add per-account daily rate limit for outbound emails (#13411) 2026-02-03 02:06:51 +05:30
data_import_job.rb fix: Strip UTF-8 BOM in DataImportJob#csv_reader before parsing CSV (#14126) 2026-04-24 17:03:03 +05:30
delete_object_job.rb fix: Prevent SLA deletion timeouts by moving to async job (#12944) 2025-12-10 12:28:47 +05:30
event_dispatcher_job.rb chore: Reorganize Sidekiq Queues (#6976) 2023-05-04 15:44:16 +05:30
hook_job.rb fix(slack): Sync bot interactive responses (#14076) 2026-04-28 10:29:03 +04:00
macros_execution_job.rb feat: Execute macro actions, for the conversation (#5066) 2022-07-26 12:41:22 +05:30
mutex_application_job.rb fix: mutex timeout and error handling (#8770) 2024-01-24 14:18:21 +04:00
send_on_slack_job.rb fix: mutex timeout and error handling (#8770) 2024-01-24 14:18:21 +04:00
send_reply_job.rb feat: TikTok channel (#12741) 2025-12-17 07:54:50 -08:00
slack_unfurl_job.rb feat: Support link unfurling for all the channels within the same connected channel account. (#8033) 2023-10-08 17:55:03 +05:30
trigger_scheduled_items_job.rb perf: limit the number of notifications per user to 300 (#13234) 2026-01-28 17:35:13 +05:30
update_slack_message_job.rb fix(slack): Sync bot interactive responses (#14076) 2026-04-28 10:29:03 +04:00
webhook_job.rb feat: add per-webhook secret with backfill migration (#13573) 2026-02-26 17:26:12 +05:30