diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index f1e5ec9a1e1..f4b0a98ad82 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -39,12 +39,16 @@ module Email def process! return if is_blacklisted? - @from_email, @from_display_name = parse_from_field(@mail) - @incoming_email = find_or_create_incoming_email - process_internal - rescue => e - @incoming_email.update_columns(error: e.to_s) if @incoming_email - raise + DistributedMutex.synchronize(@message_id) do + begin + @from_email, @from_display_name = parse_from_field(@mail) + @incoming_email = find_or_create_incoming_email + process_internal + rescue => e + @incoming_email.update_columns(error: e.to_s) if @incoming_email + raise + end + end end def is_blacklisted?