diff --git a/enterprise/app/services/voice/inbound_call_builder.rb b/enterprise/app/services/voice/inbound_call_builder.rb index f3388d4f5..7b8b0e684 100644 --- a/enterprise/app/services/voice/inbound_call_builder.rb +++ b/enterprise/app/services/voice/inbound_call_builder.rb @@ -46,12 +46,16 @@ class Voice::InboundCallBuilder # Always look up by (inbox, source_id) first — that pair has a UNIQUE index, so # creating with a colliding source_id under a different contact would raise # RecordNotUnique. Reuse the existing ContactInbox (and its contact) when found. + # A concurrent message webhook for the same wa_id can win the (inbox_id, source_id) + # race; rescue and re-find so the call path doesn't drop the connect. def ensure_contact_inbox! sid = source_id_for_provider existing = inbox.contact_inboxes.find_by(source_id: sid) return existing if existing ContactInbox.create!(contact: ensure_contact!, inbox: inbox, source_id: sid) + rescue ActiveRecord::RecordNotUnique + inbox.contact_inboxes.find_by!(source_id: sid) end def ensure_contact! diff --git a/enterprise/app/services/whatsapp/incoming_call_service.rb b/enterprise/app/services/whatsapp/incoming_call_service.rb index 1bead1013..3abf0f4be 100644 --- a/enterprise/app/services/whatsapp/incoming_call_service.rb +++ b/enterprise/app/services/whatsapp/incoming_call_service.rb @@ -23,8 +23,6 @@ class Whatsapp::IncomingCallService return accept_outbound_call(call, payload) if call.outgoing? Rails.logger.info "[WHATSAPP CALL] Duplicate inbound connect for #{payload[:id]}; ignoring" - rescue ActiveRecord::RecordNotUnique - Rails.logger.warn "[WHATSAPP CALL] Duplicate provider_call_id received: #{payload[:id]}" end def create_inbound_call(payload)